StoreServiceServices.php 9.4 KB

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