KefuServices.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;
  12. use app\services\BaseServices;
  13. use app\dao\service\StoreServiceDao;
  14. use app\services\kefu\service\StoreServiceAuxiliaryServices;
  15. use app\services\kefu\service\StoreServiceServices;
  16. use app\services\user\UserServices;
  17. use app\services\wechat\WechatUserServices;
  18. use crmeb\exceptions\ApiException;
  19. use crmeb\services\workerman\ChannelService;
  20. use app\services\kefu\service\StoreServiceLogServices;
  21. use app\services\kefu\service\StoreServiceRecordServices;
  22. use think\facade\Log;
  23. /**
  24. * Class KefuServices
  25. * @package app\services\kefu
  26. */
  27. class KefuServices extends BaseServices
  28. {
  29. /**
  30. * KefuServices constructor.
  31. * @param StoreServiceDao $dao
  32. */
  33. public function __construct(StoreServiceDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 获取客服列表
  39. * @param array $where
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function getServiceList(array $where, array $noId)
  46. {
  47. $where['status'] = 1;
  48. $where['noId'] = $noId;
  49. $where['online'] = 1;
  50. [$page, $limit] = $this->getPageValue();
  51. $list = $this->dao->getServiceList($where, $page, $limit);
  52. $count = $this->dao->count($where);
  53. return compact('list', 'count');
  54. }
  55. /**
  56. * 获取聊天记录
  57. * @param int $uid
  58. * @param int $toUid
  59. * @param int $isUp
  60. * @return array
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function getChatList(int $uid, int $toUid, int $upperId, int $is_tourist = 0)
  66. {
  67. /** @var StoreServiceLogServices $service */
  68. $service = app()->make(StoreServiceLogServices::class);
  69. [$page, $limit] = $this->getPageValue();
  70. return array_reverse($service->tidyChat($service->getServiceChatList(['chat' => [$uid, $toUid], 'is_tourist' => $is_tourist], $limit, $upperId)));
  71. }
  72. /**
  73. * 转移客服
  74. * @param int $kfuUid
  75. * @param int $uid
  76. * @param int $toUid
  77. * @return mixed
  78. */
  79. public function setTransfer(int $kfuUid, int $uid, int $kfuToUid)
  80. {
  81. if ($uid === $kfuToUid) {
  82. throw new ApiException(410139);
  83. }
  84. /** @var StoreServiceAuxiliaryServices $auxiliaryServices */
  85. $auxiliaryServices = app()->make(StoreServiceAuxiliaryServices::class);
  86. /** @var StoreServiceLogServices $service */
  87. $service = app()->make(StoreServiceLogServices::class);
  88. $addTime = $auxiliaryServices->value(['binding_id' => $kfuUid, 'relation_id' => $uid], 'update_time');
  89. $list = $service->getMessageList(['chat' => [$kfuUid, $uid], 'add_time' => $addTime]);
  90. $data = [];
  91. foreach ($list as $item) {
  92. if ($item['to_uid'] == $kfuUid) {
  93. $item['to_uid'] = $kfuToUid;
  94. }
  95. if ($item['uid'] == $kfuUid) {
  96. $item['uid'] = $kfuToUid;
  97. }
  98. $item['add_time'] = time();
  99. unset($item['id']);
  100. $data[] = $item;
  101. }
  102. $record = $this->transaction(function () use ($data, $service, $kfuUid, $uid, $kfuToUid, $auxiliaryServices) {
  103. if ($data) {
  104. $num = count($data) - 1;
  105. $messageData = $data[$num] ?? [];
  106. $res = $service->saveAll($data);
  107. } else {
  108. $num = 0;
  109. $res = true;
  110. $messageData = [];
  111. }
  112. /** @var StoreServiceRecordServices $serviceRecord */
  113. $serviceRecord = app()->make(StoreServiceRecordServices::class);
  114. $info = $serviceRecord->get(['user_id' => $kfuUid, 'to_uid' => $uid], ['type', 'message_type', 'is_tourist', 'avatar', 'nickname']);
  115. $record = $serviceRecord->saveRecord($uid, $kfuToUid, $messageData['msn'] ?? '', $info['type'] ?? 1, $messageData['message_type'] ?? 1, $num, $info['is_tourist'] ?? 0, $info['nickname'] ?? "", $info['avatar'] ?? '');
  116. $res = $res && $auxiliaryServices->saveAuxliary(['binding_id' => $kfuUid, 'relation_id' => $uid]);
  117. if (!$res && !$record) {
  118. throw new ApiException(410140);
  119. }
  120. return $record;
  121. });
  122. try {
  123. if (!$record['is_tourist']) {
  124. /** @var UserServices $userService */
  125. $userService = app()->make(UserServices::class);
  126. $_userInfo = $userService->getUserInfo($uid, 'nickname,avatar');
  127. $record['nickname'] = $_userInfo['nickname'];
  128. $record['avatar'] = $_userInfo['avatar'];
  129. }
  130. $keufInfo = $this->dao->get(['uid' => $kfuUid], ['avatar', 'nickname']);
  131. if ($keufInfo) {
  132. $keufInfo = $keufInfo->toArray();
  133. } else {
  134. $keufInfo = (object)[];
  135. }
  136. //给转接的客服发送消息通知
  137. ChannelService::instance()
  138. ->setTrigger('crmeb_chat')
  139. ->send('transfer', ['recored' => $record, 'kefuInfo' => $keufInfo, 'fun' => true], [$kfuToUid]);
  140. //告知用户对接此用户聊天
  141. $keufToInfo = $this->dao->get(['uid' => $kfuToUid], ['avatar', 'nickname']);
  142. ChannelService::instance()
  143. ->setTrigger('crmeb_chat')
  144. ->send('to_transfer', ['toUid' => $kfuToUid, 'avatar' => $keufToInfo['avatar'] ?? '', 'nickname' => $keufToInfo['nickname'] ?? ''], [$uid]);
  145. } catch (\Exception $e) {
  146. }
  147. return true;
  148. }
  149. /**
  150. * 关键字回复,没有默认关键词会自动发送给客服
  151. * @param string $reply
  152. * @param string $openId
  153. * @return bool
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\DbException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. */
  158. public function replyTransferService(string $reply, string $openId)
  159. {
  160. /** @var WechatUserServices $userServices */
  161. $userServices = app()->make(WechatUserServices::class);
  162. $userInfo = $userServices->get(['openid' => $openId], ['uid', 'nickname', 'headimgurl as avatar']);
  163. if (!$userInfo) {
  164. return true;
  165. }
  166. /** @var StoreServiceServices $kfServices */
  167. $kfServices = app()->make(StoreServiceServices::class);
  168. $serviceInfoList = $kfServices->getServiceList(['status' => 1, 'online' => 1]);
  169. if (!count($serviceInfoList)) {
  170. return true;
  171. }
  172. $uids = array_column($serviceInfoList['list'], 'uid');
  173. if (!$uids) {
  174. return true;
  175. }
  176. /** @var StoreServiceRecordServices $recordServices */
  177. $recordServices = app()->make(StoreServiceRecordServices::class);
  178. //上次聊天客服优先对话
  179. $toUid = $recordServices->getLatelyMsgUid(['to_uid' => $userInfo['uid']], 'user_id');
  180. //如果上次聊天的客不在当前客服中从新获取新的客服人员
  181. if (!in_array($toUid, $uids)) {
  182. $toUid = 0;
  183. }
  184. if (!$toUid) {
  185. $toUid = $uids[array_rand($uids)] ?? 0;
  186. }
  187. if (!$toUid) {
  188. return true;
  189. }
  190. /** @var StoreServiceLogServices $logServices */
  191. $logServices = app()->make(StoreServiceLogServices::class);
  192. $num = $logServices->getMessageNum(['uid' => $userInfo['uid'], 'to_uid' => $toUid, 'type' => 0, 'is_tourist' => 0]);
  193. $record = $recordServices->saveRecord($userInfo['uid'], $toUid, $reply, 1, 1, $num, 0, $userInfo['nickname'] ?? "", $userInfo['avatar'] ?? '');
  194. $data = [
  195. 'add_time' => time(),
  196. 'is_tourist' => 0,
  197. 'to_uid' => $toUid,
  198. 'msn' => $reply,
  199. 'uid' => $userInfo['uid'],
  200. 'type' => 0
  201. ];
  202. $data = $logServices->save($data);
  203. $data = $data->toArray();
  204. $data['_add_time'] = $data['add_time'];
  205. $data['add_time'] = strtotime($data['add_time']);
  206. $data['record'] = $record;
  207. try {
  208. ChannelService::instance()
  209. ->setTrigger('crmeb_chat')->send('mssage_num', [
  210. 'uid' => $userInfo['uid'],
  211. 'num' => $num,
  212. 'recored' => $data['record']
  213. ], [$toUid]);
  214. ChannelService::instance()
  215. ->setTrigger('crmeb_chat')->send('reply', $data, [$toUid]);
  216. } catch (\Throwable $e) {
  217. Log::error('没有开启长连接无法推送消息,消息内容为:' . $reply);
  218. }
  219. }
  220. }