GetLinkerTrait.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace addons\shopro\library\chat\traits;
  3. use GatewayWorker\Lib\Gateway;
  4. use addons\shopro\library\chat\Online;
  5. use addons\shopro\model\chat\User as ChatUser;
  6. /**
  7. * 获取各种连接者身份列表
  8. */
  9. trait GetLinkerTrait
  10. {
  11. /**
  12. * 获取所有在线客服的 client_ids
  13. *
  14. * @return array
  15. */
  16. public static function onlineCustomerServiceClientIds() {
  17. $onlineCustomerServiceClientIds = Gateway::getClientIdListByGroup(Online::getGrouponName('online_customer_service'));
  18. return $onlineCustomerServiceClientIds ? : [];
  19. }
  20. /**
  21. * 获取所有在线客服 的sessions,一个客服多浏览器登录会重复,因为是session这里都保留
  22. *
  23. * @return array
  24. */
  25. public static function onlineCustomerServiceSessions()
  26. {
  27. // 获取所有在线客服 session
  28. $customerServiceUserSessions = Gateway::getClientSessionsByGroup(Online::getGrouponName('online_customer_service'));
  29. return $customerServiceUserSessions ? : [];
  30. }
  31. /**
  32. * 获取所有在线客服的信息,自动过滤重复的信息,因为一个客服可以多浏览器登录
  33. *
  34. * @return array
  35. */
  36. public static function onlineCustomerServices() {
  37. $customerServiceSessions = self::onlineCustomerServiceSessions();
  38. $customerServices = array_column($customerServiceSessions, 'user');
  39. // 过滤重复
  40. $newServices = [];
  41. foreach ($customerServices as $customerService) {
  42. $newServices[$customerService['id']] = $customerService;
  43. }
  44. return array_values($newServices);
  45. }
  46. /**
  47. * 通过 客服 id 获取客服 session, 一个客服多浏览器登录会重复,因为是session这里都保留,当前的客服的session 是老的,不会更新
  48. *
  49. * @param int $customer_service_id
  50. * @return array
  51. */
  52. public static function onlineCustomerServiceSessionById($customer_service_id)
  53. {
  54. $client_ids = Gateway::getClientIdByUid(Online::getUId($customer_service_id, 'customer_service'));
  55. $customerServiceSessions = [];
  56. // 当前客服绑定的所有session
  57. foreach ($client_ids as $client_id) {
  58. $customerServiceSessions[] = Gateway::getSession($client_id);
  59. }
  60. return $customerServiceSessions;
  61. }
  62. /**
  63. * 通过 客服 id 获取客服 session, 一个客服多浏览器登录会重复,因为是session这里都保留,通过 client_id 判断并拿到当前客服最新session
  64. *
  65. * @param int $customer_service_id
  66. * @return array
  67. */
  68. public static function onlineCustomerServiceNewSessionById($customer_service_id, $current_client_id)
  69. {
  70. $client_ids = Gateway::getClientIdByUid(Online::getUId($customer_service_id, 'customer_service'));
  71. $customerServiceSessions = [];
  72. // 当前客服绑定的所有session
  73. foreach ($client_ids as $client_id) {
  74. if ($client_id == $current_client_id) {
  75. $customerServiceSession = $_SESSION;
  76. } else {
  77. // 如果不是自己
  78. $customerServiceSession = Gateway::getSession($client_id);
  79. }
  80. $customerServiceSessions[] = $customerServiceSession;
  81. }
  82. return $customerServiceSessions;
  83. }
  84. /**
  85. * 通过客服 id 获取 客服信息
  86. *
  87. * @param int $customer_service_id
  88. * @return array
  89. */
  90. public static function onlineCustomerServiceById($customer_service_id)
  91. {
  92. // 客服多浏览器登录的时候是多个 session
  93. $customerServiceSessions = self::onlineCustomerServiceSessionById($customer_service_id);
  94. // 只需要获取第一条的 user 信息
  95. $customerService = (isset($customerServiceSessions[0]) && isset($customerServiceSessions[0]['user'])) ? $customerServiceSessions[0]['user'] : [];
  96. return $customerService;
  97. }
  98. /**
  99. * 当前正在等待接入的用户 session
  100. *
  101. * @return array
  102. */
  103. public static function onlineWaitingUserSessions () {
  104. $onlineWaitingUserSessions = Gateway::getClientSessionsByGroup(Online::getGrouponName('online_waiting'));
  105. return $onlineWaitingUserSessions ? : [];
  106. }
  107. /**
  108. * 当前正在等待接入的用户
  109. *
  110. * @return array
  111. */
  112. public static function onlineWaitingUsers() {
  113. $waitingSessionUsers = self::onlineWaitingUserSessions();
  114. $waitings = array_column($waitingSessionUsers, 'chat_user');
  115. // 过滤重复
  116. $waitingUsers = [];
  117. foreach ($waitings as $waiting) {
  118. $waitingUsers[$waiting['id']] = $waiting;
  119. }
  120. return array_values($waitingUsers);
  121. }
  122. /**
  123. * 获取当前客服正在服务的用户 session, 这里同一个用户可能会存在两个 session(多端登录)
  124. *
  125. * @param array $customerService
  126. * @return array
  127. */
  128. public static function customerServiceUserSessions($customerService) {
  129. // 获取当前客服 group_name
  130. $customerServiceGroupName = Online::getGrouponName('customer_service_user', ['customer_service_id' => $customerService['id']]);
  131. // 获取当前客服正在服务的用户列表 client_id
  132. $customerServiceUserSessions = Gateway::getClientSessionsByGroup($customerServiceGroupName);
  133. return $customerServiceUserSessions ? : [];
  134. }
  135. /**
  136. * 获取当前客服正在服务的用户 (去重)
  137. *
  138. * @param array $customerService
  139. * @return array
  140. */
  141. public static function customerServiceOnlineUsers($customerService) {
  142. // 当前客服服务的所有用户 session
  143. $customerServiceUserSessions = self::customerServiceUserSessions($customerService);
  144. // 在线用户列表
  145. $onlines = array_column($customerServiceUserSessions, 'chat_user');
  146. // 过滤重复
  147. $newUsers = [];
  148. foreach ($onlines as $online) {
  149. $newUsers[$online['id']] = $online;
  150. }
  151. return array_values($newUsers);
  152. }
  153. /**
  154. * 获取当前客服正在服务的用户 client_ids
  155. *
  156. * @param array $customerService
  157. * @return array
  158. */
  159. public static function customerServiceUserClientIds($customerService) {
  160. // 获取当前客服 group_name
  161. $customerServiceGroupName = Online::getGrouponName('customer_service_user', ['customer_service_id' => $customerService['id']]);
  162. // 获取当前客服正在服务的用户列表 client_id
  163. $userClientIds = Gateway::getClientIdListByGroup($customerServiceGroupName);
  164. return $userClientIds ? : [];
  165. }
  166. /**
  167. * 获取当前客服服务的历史用户
  168. *
  169. * @param array $customerService
  170. * @param array $exceptSessionIds
  171. * @return array
  172. */
  173. public static function customerServiceHistoryUsers($customerService, Array $exceptSessionIds = []) {
  174. $histories = Online::historyByCustomerServiceId($customerService, ['except' => $exceptSessionIds]);
  175. foreach ($histories as $key => &$history) {
  176. $status = Gateway::isUidOnline(Online::getUId($history['session_id'], 'user'));
  177. $history['status'] = $status; // 在线状态
  178. $history['customer_service'] = null; // 如果在线,并且已经接入客服,当前客服信息
  179. if ($status) {
  180. // 在线,判断用户是否正在被客服接入
  181. $client_ids = Gateway::getClientIdByUid(Online::getUId($history['session_id'], 'user'));
  182. $client_id = current($client_ids);
  183. $historySession = Gateway::getSession($client_id);
  184. $customerService = $historySession['customer_service'] ? : null;
  185. $history['customer_service'] = $customerService;
  186. }
  187. $history['status'] = $status;
  188. }
  189. return $histories;
  190. }
  191. /**
  192. * 通过 session_id 获取连接的用户对应的 chatUser
  193. *
  194. * @param string $session_id
  195. * @return object
  196. */
  197. public static function getChatUserBySessionId($session_id)
  198. {
  199. $chatUser = ChatUser::where('session_id', $session_id)->find();
  200. return $chatUser;
  201. }
  202. /**
  203. * 通过 user_id 获取连接的用户对应的 chatUser
  204. *
  205. * @param string $user_id
  206. * @return object
  207. */
  208. public static function getChatUserByUserId($user_id)
  209. {
  210. $chatUser = ChatUser::where('user_id', $user_id)->find();
  211. return $chatUser;
  212. }
  213. }