StoreServiceServices.php 9.9 KB

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