Sender.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace addons\shopro\library\chat;
  3. use GatewayWorker\Lib\Gateway;
  4. use addons\shopro\library\chat\traits\SenderTrait;
  5. use addons\shopro\library\chat\traits\GetLinkerTrait;
  6. /**
  7. * 通知类
  8. */
  9. class Sender
  10. {
  11. use SenderTrait, GetLinkerTrait;
  12. /**
  13. * 通知客服用户上线
  14. *
  15. * @param string $session_id 上线用户
  16. * @return void
  17. */
  18. public static function userOnline($session_id, $chatUser) {
  19. // 获取所有在线客服的 client_ids
  20. $onlineCustomerServiceClientIds = self::onlineCustomerServiceClientIds();
  21. Sender::successAll([
  22. 'type' => 'user_online',
  23. 'msg' => '用户上线',
  24. 'data' => [
  25. 'session_id' => $session_id,
  26. 'user' => $chatUser,
  27. 'customer_service' => $_SESSION['customer_service'] ? : null,
  28. ]
  29. ], $onlineCustomerServiceClientIds);
  30. }
  31. /**
  32. * 通知在线客服用户下线
  33. *
  34. * @param string $session_id 下线用户
  35. * @return void
  36. */
  37. public static function userOffline($session_id) {
  38. // 获取所有在线客服的 client_ids
  39. $onlineCustomerServiceClientIds = self::onlineCustomerServiceClientIds();
  40. // 通知在线客服,用户下线
  41. Sender::successAll([
  42. 'type' => 'user_offline',
  43. 'msg' => '用户下线',
  44. 'data' => [
  45. 'session_id' => $session_id,
  46. ]
  47. ], $onlineCustomerServiceClientIds);
  48. }
  49. /**
  50. * 通知所有客服用户已被接入
  51. *
  52. * @return void
  53. */
  54. public static function userAccessed($session_id, $customerService) {
  55. // 获取所有在线客服的 client_ids
  56. $onlineCustomerServiceClientIds = self::onlineCustomerServiceClientIds();
  57. // 通知在线客服,用户被接入
  58. Sender::successAll([
  59. 'type' => 'user_accessed',
  60. 'msg' => '用户被接入',
  61. 'data' => [
  62. 'session_id' => $session_id,
  63. 'customer_service' => $customerService
  64. ]
  65. ], $onlineCustomerServiceClientIds);
  66. }
  67. public static function userAccess($customerService, $session_id, $oper_type = 'user') {
  68. $customer_service_id = $customerService['id'] ?? 0;
  69. if ($oper_type == 'user') {
  70. $chatUser = $_SESSION['chat_user'];
  71. } else {
  72. $chatUser = Online::getChatUserBySessionId($session_id);
  73. }
  74. Sender::successByCustomerServiceId($customer_service_id, [
  75. 'type' => 'user_access',
  76. 'msg' => '有新的用户接入',
  77. 'data' => [
  78. 'session_id' => $session_id,
  79. 'chat_user' => Online::userSetMessage([$chatUser])[0] // 额外获取最后一条消息
  80. ]
  81. ]);
  82. }
  83. /**
  84. * 客服初始化通知
  85. *
  86. * @param string $client_id
  87. * @param array $customerService
  88. * @return void
  89. */
  90. public static function customerServiceInit($client_id, $customerService) {
  91. // 客服正在服务的用户, 并且追加最后一条 message
  92. $onlines = Online::userSetMessage(self::customerServiceOnlineUsers($customerService));
  93. // 在线用户 的 session_id
  94. $onlineSessionIds = array_column($onlines, 'session_id');
  95. // 历史服务客户, 并且追加最后一条 message
  96. $histories = Online::userSetMessage(self::customerServiceHistoryUsers($customerService, $onlineSessionIds));
  97. // 等待接待用户, 并且追加最后一条 message
  98. $waitings = Online::userSetMessage(self::onlineWaitingUsers());
  99. Sender::success($client_id, [
  100. 'type' => 'init',
  101. 'msg' => '连接成功',
  102. 'data' => [
  103. 'client_id' => $client_id,
  104. 'onlines' => $onlines,
  105. 'histories' => $histories,
  106. 'waitings' => $waitings,
  107. 'customer_service' => $_SESSION['user'] ?? null
  108. ]
  109. ]);
  110. }
  111. /**
  112. * 通知正在连接的用户,客服上线了
  113. *
  114. * @param array $customerService
  115. * @return void
  116. */
  117. public static function customerServiceOnline($customerService) {
  118. // 通知正在连接的用户,客服上线了
  119. $userClientIds = self::customerServiceUserClientIds($customerService);
  120. Sender::successAll([
  121. 'type' => 'customer_service_online',
  122. 'msg' => '客服 ' . $customerService['name'] . ' 上线',
  123. 'data' => [
  124. 'customer_service' => $customerService
  125. ]
  126. ], $userClientIds);
  127. }
  128. /**
  129. * 通知正在连接的用户,客服下线
  130. *
  131. * @param array $customerService
  132. * @return void
  133. */
  134. public static function customerServiceOffline($customerService) {
  135. // 通知正在连接的用户,客服下线了
  136. $userClientIds = self::customerServiceUserClientIds($customerService);
  137. Sender::successAll([
  138. 'type' => 'customer_service_offline',
  139. 'msg' => '客服 ' . $customerService['name'] . ' 离线',
  140. 'data' => [
  141. 'customer_service' => $customerService
  142. ]
  143. ], $userClientIds);
  144. }
  145. /**
  146. * 通知所有在线客服,更新在线客服列表
  147. *
  148. * @return void
  149. */
  150. public static function customerServiceOnlineList() {
  151. // 通知所有在线客服,更新当前在线客服列表
  152. $onlineCustomerServiceClientIds = self::onlineCustomerServiceClientIds();
  153. // 获取所有在线客服信息,自动过滤重复
  154. $customerServices = self::onlineCustomerServices();
  155. // 通知在线客服,更新当前在线客服列表
  156. Sender::successAll([
  157. 'type' => 'customer_service_update',
  158. 'msg' => '更新客服列表',
  159. 'data' => [
  160. 'customer_services' => $customerServices,
  161. ]
  162. ], $onlineCustomerServiceClientIds);
  163. }
  164. /**
  165. * 问题快速回答
  166. *
  167. * @param array $question
  168. * @param array $receives
  169. * @return void
  170. */
  171. public static function questionReply($question, $receives) {
  172. $client_id = $receives['client_id'] ?? '';
  173. $session_id = $receives['session_id'] ?? '';
  174. $user_id = $receives['user_id'] ?? '';
  175. $customer_service_id = $receives['customer_service_id'] ?? 0;
  176. $customerService = $receives['customer_service'] ?? [];
  177. $result = null;
  178. if ($session_id) {
  179. // 通知用户
  180. $result = Sender::messageBySessionId($session_id, [
  181. 'type' => 'message',
  182. 'msg' => '收到消息',
  183. 'data' => [
  184. 'message' => [
  185. 'message_type' => 'text',
  186. 'message' => $question['content']
  187. ],
  188. 'customer_service' => $customerService
  189. ]
  190. ], [
  191. 'sender' => array_merge([
  192. 'sender_identify' => 'customer_service',
  193. 'sender_id' => $customer_service_id
  194. ], $receives)
  195. ]
  196. );
  197. }
  198. if ($customer_service_id && $result && isset($result['data']['message'])){
  199. // 通知客服
  200. Sender::successByCustomerServiceId($customer_service_id, [
  201. 'type' => 'message',
  202. 'msg' => '收到消息',
  203. 'data' => [
  204. 'message' => $result['data']['message'],
  205. 'session_id' => $session_id
  206. ]
  207. ]
  208. );
  209. }
  210. }
  211. /**
  212. * 删除用户
  213. *
  214. * @param string $client_id
  215. * @param int $result
  216. * @param array $data
  217. * @return void
  218. */
  219. public static function delUser($client_id, $data = []) {
  220. Sender::success($client_id, [
  221. 'type' => 'del_success',
  222. 'msg' => '删除成功',
  223. 'data' => $data
  224. ]);
  225. }
  226. }