KefuServices.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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;
  12. use app\services\BaseServices;
  13. use app\dao\message\service\StoreServiceDao;
  14. use app\services\message\service\StoreServiceAuxiliaryServices;
  15. use app\services\message\service\StoreServiceServices;
  16. use app\services\user\UserServices;
  17. use app\services\wechat\WechatUserServices;
  18. use app\webscoket\SocketPush;
  19. use think\exception\ValidateException;
  20. use app\services\message\service\StoreServiceLogServices;
  21. use app\services\message\service\StoreServiceRecordServices;
  22. use think\facade\Log;
  23. /**
  24. * Class KefuServices
  25. * @package app\services\kefu
  26. * @mixin StoreServiceDao
  27. */
  28. class KefuServices extends BaseServices
  29. {
  30. const GECJUROK = 'gElnUk';
  31. /**
  32. * KefuServices constructor.
  33. * @param StoreServiceDao $dao
  34. */
  35. public function __construct(StoreServiceDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. /**
  40. * 获取客服列表
  41. * @param array $where
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function getServiceList(array $where, array $noId)
  48. {
  49. $where['status'] = 1;
  50. $where['account_status'] = 1;
  51. $where['noId'] = $noId;
  52. $where['online'] = 1;
  53. [$page, $limit] = $this->getPageValue();
  54. $list = $this->dao->getServiceList($where, $page, $limit);
  55. $count = $this->dao->count($where);
  56. return compact('list', 'count');
  57. }
  58. /**
  59. * 获取聊天记录
  60. * @param int $uid
  61. * @param int $toUid
  62. * @param int $upperId
  63. * @param int $is_tourist
  64. * @return array
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function getChatList(int $uid, int $toUid, int $upperId, int $is_tourist = 0)
  70. {
  71. /** @var StoreServiceLogServices $service */
  72. $service = app()->make(StoreServiceLogServices::class);
  73. [$page, $limit] = $this->getPageValue();
  74. return array_reverse($service->tidyChat($service->getServiceChatList(['chat' => [$uid, $toUid], 'is_tourist' => $is_tourist], $limit, $upperId)));
  75. }
  76. /**
  77. * 转移客服
  78. * @param int $kfuUid
  79. * @param int $uid
  80. * @param int $toUid
  81. * @return mixed
  82. */
  83. public function setTransfer(int $kfuUid, int $uid, int $kfuToUid)
  84. {
  85. if ($uid === $kfuToUid) {
  86. throw new ValidateException('自己不能转接给自己');
  87. }
  88. /** @var StoreServiceAuxiliaryServices $auxiliaryServices */
  89. $auxiliaryServices = app()->make(StoreServiceAuxiliaryServices::class);
  90. /** @var StoreServiceLogServices $service */
  91. $service = app()->make(StoreServiceLogServices::class);
  92. $addTime = $auxiliaryServices->value(['binding_id' => $kfuUid, 'relation_id' => $uid], 'update_time');
  93. $list = $service->getMessageList(['chat' => [$kfuUid, $uid], 'add_time' => $addTime]);
  94. $data = [];
  95. foreach ($list as $item) {
  96. if ($item['to_uid'] == $kfuUid) {
  97. $item['to_uid'] = $kfuToUid;
  98. }
  99. if ($item['uid'] == $kfuUid) {
  100. $item['uid'] = $kfuToUid;
  101. }
  102. $item['add_time'] = time();
  103. unset($item['id']);
  104. $data[] = $item;
  105. }
  106. $record = $this->transaction(function () use ($data, $service, $kfuUid, $uid, $kfuToUid, $auxiliaryServices) {
  107. if ($data) {
  108. $num = count($data) - 1;
  109. $messageData = $data[$num] ?? [];
  110. $res = $service->saveAll($data);
  111. } else {
  112. $num = 0;
  113. $res = true;
  114. $messageData = [];
  115. }
  116. /** @var StoreServiceRecordServices $serviceRecord */
  117. $serviceRecord = app()->make(StoreServiceRecordServices::class);
  118. $info = $serviceRecord->get(['user_id' => $kfuUid, 'to_uid' => $uid], ['type', 'message_type', 'is_tourist', 'avatar', 'nickname']);
  119. $record = $serviceRecord->saveRecord($uid, $kfuToUid, $messageData['msn'] ?? '', $info['type'] ?? 1, $messageData['message_type'] ?? 1, $num, $info['is_tourist'] ?? 0, $info['nickname'] ?? "", $info['avatar'] ?? '');
  120. $res = $res && $auxiliaryServices->saveAuxliary(['binding_id' => $kfuUid, 'relation_id' => $uid]);
  121. if (!$res && !$record) {
  122. throw new ValidateException('转接客服失败');
  123. }
  124. return $record;
  125. });
  126. try {
  127. if (!$record['is_tourist']) {
  128. /** @var UserServices $userService */
  129. $userService = app()->make(UserServices::class);
  130. $_userInfo = $userService->getUserInfo($uid, 'nickname,avatar');
  131. $record['nickname'] = $_userInfo['nickname'];
  132. $record['avatar'] = $_userInfo['avatar'];
  133. }
  134. $keufInfo = $this->dao->get(['uid' => $kfuUid], ['avatar', 'nickname']);
  135. if ($keufInfo) {
  136. $keufInfo = $keufInfo->toArray();
  137. } else {
  138. $keufInfo = (object)[];
  139. }
  140. //给转接的客服发送消息通知
  141. SocketPush::kefu()->type('transfer')->to($kfuToUid)->data(['recored' => $record, 'kefuInfo' => $keufInfo])->push();
  142. //告知用户对接此用户聊天
  143. $keufToInfo = $this->dao->get(['uid' => $kfuToUid], ['avatar', 'nickname']);
  144. SocketPush::user()->type('to_transfer')->to($uid)->data(['toUid' => $kfuToUid, 'avatar' => $keufToInfo['avatar'] ?? '', 'nickname' => $keufToInfo['nickname'] ?? ''])->push();
  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. mt_srand();
  186. $toUid = $uids[array_rand($uids)] ?? 0;
  187. }
  188. if (!$toUid) {
  189. return true;
  190. }
  191. /** @var StoreServiceLogServices $logServices */
  192. $logServices = app()->make(StoreServiceLogServices::class);
  193. $num = $logServices->getMessageNum(['uid' => $userInfo['uid'], 'to_uid' => $toUid, 'type' => 0, 'is_tourist' => 0]);
  194. $record = $recordServices->saveRecord($userInfo['uid'], $toUid, $reply, 1, 1, $num, 0, $userInfo['nickname'] ?? "", $userInfo['avatar'] ?? '');
  195. $data = [
  196. 'add_time' => time(),
  197. 'is_tourist' => 0,
  198. 'to_uid' => $toUid,
  199. 'msn' => $reply,
  200. 'uid' => $userInfo['uid'],
  201. 'type' => 0
  202. ];
  203. $data = $logServices->save($data);
  204. $data = $data->toArray();
  205. $data['_add_time'] = $data['add_time'];
  206. $data['add_time'] = strtotime($data['add_time']);
  207. $data['record'] = $record;
  208. try {
  209. SocketPush::kefu()->type('mssage_num')->to($toUid)->data([
  210. 'uid' => $userInfo['uid'],
  211. 'num' => $num,
  212. 'recored' => $data['record']
  213. ])->push();
  214. SocketPush::admin()->to($toUid)->data($data)->type('reply')->push();
  215. } catch (\Throwable $e) {
  216. Log::error('没有开启长连接无法推送消息,消息内容为:' . $reply);
  217. }
  218. }
  219. }