StoreServiceServices.php 9.2 KB

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