You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoreServiceServices.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\services\message\service;
  3. use app\dao\message\service\StoreServiceDao;
  4. use app\services\BaseServices;
  5. use app\services\user\UserServices;
  6. use crmeb\exceptions\AdminException;
  7. use crmeb\services\FormBuilder;
  8. use crmeb\traits\ServicesTrait;
  9. use think\exception\ValidateException;
  10. /**
  11. * 客服
  12. * Class StoreServiceServices
  13. * @package app\services\message\service
  14. * @method getStoreServiceOrderNotice() 获取接受通知的客服
  15. */
  16. class StoreServiceServices extends BaseServices
  17. {
  18. use ServicesTrait;
  19. /**
  20. * 创建form表单
  21. * @var Form
  22. */
  23. protected $builder;
  24. /**
  25. * 构造方法
  26. * StoreServiceServices constructor.
  27. * @param StoreServiceDao $dao
  28. * @param FormBuilder $builder
  29. */
  30. public function __construct(StoreServiceDao $dao, FormBuilder $builder)
  31. {
  32. $this->dao = $dao;
  33. $this->builder = $builder;
  34. }
  35. /**
  36. * 获取客服列表
  37. * @param array $where
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getServiceList(array $where)
  44. {
  45. [$page, $limit] = $this->getPageValue();
  46. $list = $this->dao->getServiceList($where, $page, $limit);
  47. foreach ($list as &$item) {
  48. if (!isset($item['workMember'])) {
  49. $item['workMember'] = [];
  50. }
  51. }
  52. $this->updateNonExistentService(array_column($list, 'uid'));
  53. $count = $this->dao->count($where);
  54. return compact('list', 'count');
  55. }
  56. /**
  57. * @param array $uids
  58. * @return bool
  59. */
  60. public function updateNonExistentService(array $uids = [])
  61. {
  62. if (!$uids) {
  63. return true;
  64. }
  65. /** @var UserServices $services */
  66. $services = app()->make(UserServices::class);
  67. $userUids = $services->getColumn([['uid', 'in', $uids]], 'uid');
  68. $unUids = array_diff($uids, $userUids);
  69. return $this->dao->deleteNonExistentService($unUids);
  70. }
  71. /**
  72. * 创建客服表单
  73. * @param array $formData
  74. * @return mixed
  75. * @throws \FormBuilder\Exception\FormBuilderException
  76. */
  77. public function createServiceForm(array $formData = [])
  78. {
  79. if ($formData) {
  80. $field[] = $this->builder->frameImage('avatar', '客服头像', $this->url('admin/widget.images/index', ['fodder' => 'avatar'], true), $formData['avatar'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  81. } else {
  82. $field[] = $this->builder->frameImage('image', '商城用户', $this->url('admin/system.user/list', ['fodder' => 'image'], true))->icon('ios-add')->width('960px')->height('550px')->modal(['footer-hide' => true])->Props(['srcKey' => 'image']);
  83. $field[] = $this->builder->hidden('uid', 0);
  84. $field[] = $this->builder->hidden('avatar', '');
  85. }
  86. $field[] = $this->builder->input('nickname', '客服名称', $formData['nickname'] ?? '')->col(24)->required();
  87. $field[] = $this->builder->input('phone', '手机号码', $formData['phone'] ?? '')->col(24)->required();
  88. if ($formData) {
  89. $field[] = $this->builder->input('account', '客服账号', $formData['account'] ?? '')->col(24)->required();
  90. $field[] = $this->builder->input('password', '客服密码')->type('password')->col(24);
  91. $field[] = $this->builder->input('true_password', '确认密码')->type('password')->col(24);
  92. } else {
  93. $field[] = $this->builder->input('account', '客服账号')->col(24)->required();
  94. $field[] = $this->builder->input('password', '客服密码')->type('password')->col(24)->required();
  95. $field[] = $this->builder->input('true_password', '确认密码')->type('password')->col(24)->required();
  96. }
  97. $field[] = $this->builder->switches('account_status', '账号状态', (int)($formData['account_status'] ?? 0))->appendControl(1, [
  98. $this->builder->switches('status', '客服状态', (int)($formData['status'] ?? 0))->falseValue(0)->trueValue(1)->openStr('打开')->closeStr('关闭')->size('large'),
  99. $this->builder->switches('customer', '手机订单管理', $formData['customer'] ?? 0)->falseValue(0)->trueValue(1)->openStr('打开')->closeStr('关闭')->size('large'),
  100. $this->builder->switches('notify', '订单通知', $formData['notify'] ?? 0)->falseValue(0)->trueValue(1)->openStr('打开')->closeStr('关闭')->size('large'),
  101. ])->falseValue(0)->trueValue(1)->openStr('开启')->closeStr('关闭')->size('large');
  102. return $field;
  103. }
  104. /**
  105. * 创建客服获取表单
  106. * @return array
  107. * @throws \FormBuilder\Exception\FormBuilderException
  108. */
  109. public function create()
  110. {
  111. return create_form('添加客服', $this->createServiceForm(), $this->url('/app/wechat/kefu'), 'POST');
  112. }
  113. /**
  114. * 编辑获取表单
  115. * @param int $id
  116. * @return array
  117. * @throws \FormBuilder\Exception\FormBuilderException
  118. */
  119. public function edit(int $id)
  120. {
  121. $serviceInfo = $this->dao->get($id);
  122. if (!$serviceInfo) {
  123. throw new AdminException('数据不存在!');
  124. }
  125. return create_form('编辑客服', $this->createServiceForm($serviceInfo->toArray()), $this->url('/app/wechat/kefu/' . $id), 'PUT');
  126. }
  127. /**
  128. * 获取某人的聊天记录用户列表
  129. * @param int $uid
  130. * @return array|array[]
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. */
  135. public function getChatUser(int $uid)
  136. {
  137. /** @var StoreServiceLogServices $serviceLog */
  138. $serviceLog = app()->make(StoreServiceLogServices::class);
  139. /** @var UserServices $serviceUser */
  140. $serviceUser = app()->make(UserServices::class);
  141. $uids = $serviceLog->getChatUserIds($uid);
  142. if (!$uids) {
  143. return [];
  144. }
  145. return $serviceUser->getUserList(['uid' => $uids], 'nickname,uid,avatar as headimgurl');
  146. }
  147. /**
  148. * 检查用户是否是客服
  149. * @param array $where
  150. * @return bool
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. */
  155. public function checkoutIsService(array $where)
  156. {
  157. return $this->dao->count($where) ? true : false;
  158. }
  159. /**
  160. * 查询聊天记录和获取客服uid
  161. * @param int $uid 当前用户uid
  162. * @param int $uidTo 上翻页id
  163. * @param int $limit 展示条数
  164. * @param int $toUid 客服uid
  165. * @return array
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. public function getRecord(int $uid, int $uidTo, int $limit = 10, int $toUid = 0)
  171. {
  172. if (!$toUid) {
  173. $serviceInfoList = $this->getServiceList(['noId' => [$uid], 'status' => 1, 'account_status' => 1, 'online' => 1]);
  174. if (!count($serviceInfoList)) {
  175. throw new ValidateException('暂无客服人员在线,请稍后联系');
  176. }
  177. $uids = array_column($serviceInfoList['list'], 'uid');
  178. if (!$uids) {
  179. throw new ValidateException('暂无客服人员在线,请稍后联系');
  180. }
  181. /** @var StoreServiceRecordServices $recordServices */
  182. $recordServices = app()->make(StoreServiceRecordServices::class);
  183. //上次聊天客服优先对话
  184. $toUid = $recordServices->getLatelyMsgUid(['to_uid' => $uid], 'user_id');
  185. //如果上次聊天的客不在当前客服中从新
  186. if (!in_array($toUid, $uids)) {
  187. $toUid = 0;
  188. }
  189. if (!$toUid) {
  190. mt_srand();
  191. $toUid = $uids[array_rand($uids)] ?? 0;
  192. }
  193. if (!$toUid) {
  194. throw new ValidateException('暂无客服人员在线,请稍后联系');
  195. }
  196. }
  197. $userInfo = $this->dao->get(['uid' => $toUid], ['nickname', 'avatar']);
  198. if (!$userInfo) {
  199. /** @var UserServices $userServices */
  200. $userServices = app()->make(UserServices::class);
  201. $userInfo = $userServices->get(['uid' => $toUid], ['nickname', 'avatar']);
  202. if (!$userInfo) {
  203. $userInfo['nickname'] = '';
  204. $userInfo['avatar'] = '';
  205. }
  206. }
  207. /** @var StoreServiceLogServices $logServices */
  208. $logServices = app()->make(StoreServiceLogServices::class);
  209. $result = ['serviceList' => [], 'uid' => $toUid, 'nickname' => $userInfo['nickname'], 'avatar' => $userInfo['avatar']];
  210. $serviceLogList = $logServices->getServiceChatList(['chat' => [$uid, $toUid], 'is_tourist' => 0], $limit, $uidTo);
  211. $result['serviceList'] = array_reverse($logServices->tidyChat($serviceLogList));
  212. return $result;
  213. }
  214. }