Getter.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace addons\shopro\library\chat;
  3. use addons\shopro\exception\ShoproException;
  4. use addons\shopro\library\chat\traits\Helper;
  5. use addons\shopro\library\chat\traits\Session;
  6. use addons\shopro\library\chat\traits\NspData;
  7. use addons\shopro\library\chat\provider\getter\Db;
  8. use addons\shopro\library\chat\provider\getter\Socket;
  9. use PHPSocketIO\SocketIO;
  10. use PHPSocketIO\Socket as PhpSocket;
  11. use PHPSocketIO\Nsp;
  12. /**
  13. * 客服 getter 获取各种用户类
  14. */
  15. class Getter
  16. {
  17. /**
  18. * session 存储助手
  19. */
  20. use Session;
  21. /**
  22. * helper 助手
  23. */
  24. use Helper;
  25. /**
  26. * 在 nsp 上存储数据
  27. */
  28. use NspData;
  29. /**
  30. * 当前 phpsocket.io 实例
  31. *
  32. * @var SocketIo
  33. */
  34. protected $io = null;
  35. /**
  36. * 当前socket 连接
  37. *
  38. * @var PhpSocket
  39. */
  40. protected $socket = null;
  41. /**
  42. * 当前 命名空间实例
  43. *
  44. * @var Nsp
  45. */
  46. protected $nsp = null;
  47. /**
  48. * 数据驱动
  49. *
  50. * @var string
  51. */
  52. protected $driver = null;
  53. protected $dbProvider;
  54. protected $socketProvider;
  55. public function __construct(PhpSocket $socket = null, SocketIo $io, Nsp $nsp)
  56. {
  57. $this->socket = $socket;
  58. $this->io = $io;
  59. $this->nsp = $nsp;
  60. }
  61. /**
  62. * 设置 getter 驱动
  63. *
  64. * @param string $driver
  65. * @return self
  66. */
  67. public function driver($driver) {
  68. $this->driver = $driver;
  69. return $this;
  70. }
  71. /**
  72. * 通过auth id 获取 session_id
  73. *
  74. * @param string $auth_id
  75. * @param string $auth
  76. * @return string|null
  77. */
  78. public function getSessionIdByAuth($auth_id, $auth)
  79. {
  80. $chatUser = $this->driver('db')->getChatUserByAuth($auth_id, $auth);
  81. return $chatUser->session_id ?? null;
  82. }
  83. /**
  84. * 更新客服信息,数据库和session 都更新
  85. *
  86. * @param integer $id 要更新的 客服id
  87. * @param array $data 要更新的数据
  88. * @return void
  89. */
  90. public function updateCustomerServiceInfo($id, $data)
  91. {
  92. // 更新当前客服的所有连接,比如 last_time
  93. $this->driver('socket')->updateSessionsById($id, 'customer_service', ['customer_service' => $data]);
  94. $this->driver('db')->updateCustomerService($id, $data);
  95. }
  96. /**
  97. * 更新客服信息,数据库和session 都更新
  98. *
  99. * @param integer $id 要更新的 客服id
  100. * @param string $status 状态 online|offline|busy
  101. * @return void
  102. */
  103. public function updateCustomerServiceStatus($id, $status)
  104. {
  105. $data = ['status' => $status];
  106. // 只更新当前连接,别的当前客服的连接不更新,比如 status
  107. $this->updateSessionByClientId($this->socket->id, ['customer_service' => $data]);
  108. // 不是 (改为离线,并且还有当前客服的其他链接在线),则修改数据库状态
  109. if (!($status == 'offline' && $this->driver('socket')->isOnLineCustomerServiceById($id))) {
  110. $this->driver('db')->updateCustomerService($id, $data);
  111. }
  112. }
  113. /**
  114. * 更新客服忙碌杜
  115. *
  116. * @return void
  117. */
  118. public function updateCustomerServiceBusyPercent()
  119. {
  120. // 所有客服的房间
  121. $roomIds = $this->nspData('room_ids');
  122. $roomIds = $roomIds ? : [];
  123. foreach ($roomIds as $room_id) {
  124. // 当前房间在线客服组的所有客户端
  125. $customer_service_room = $this->getRoomName('customer_service_room', ['room_id' => $room_id]);
  126. $clientIds = $this->driver('socket')->getClientIdsByRoom($customer_service_room);
  127. foreach ($clientIds as $client_id) {
  128. $customerService = $this->getSession($client_id, 'customer_service');
  129. if ($customerService) {
  130. // 客服服务的对应用户的房间名
  131. $customer_service_room_user = $this->getRoomName('customer_service_room_user', ['room_id' => $room_id, 'customer_service_id' => $customerService['id']]);
  132. $customerService['current_num'] = count($this->driver('socket')->getSessionsByRoom($customer_service_room_user, 'session_id')); // 真实用户数量,不是客户端数量
  133. $max_num = $customerService['max_num'] > 0 ? $customerService['max_num'] : 1; // 避免除数为 0
  134. $customerService['busy_percent'] = $customerService['current_num'] / $max_num;
  135. // 只更新当前连接,别的当前客服的连接不更新,比如 status
  136. $this->updateSessionByClientId($client_id, ['customer_service' => $customerService]);
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * 获取客服服务中的用户
  143. *
  144. * @param integer $room_id 房间号
  145. * @param integer $customer_service_id 客服 id
  146. * @param array $exceptIds 要排除的ids (正在服务的用户)
  147. * @return array
  148. */
  149. public function getCustomersIngFormatByCustomerService($room_id, $customer_service_id)
  150. {
  151. // 获取数据库历史
  152. $ings = $this->driver('socket')->getCustomersIngByCustomerService($room_id, $customer_service_id);
  153. // 格式化数据
  154. $ings = $this->chatUsersFormat($room_id, $ings);
  155. return $ings;
  156. }
  157. /**
  158. * 获取等待中的用户
  159. *
  160. * @param integer $room_id 房间号
  161. * @param integer $customer_service_id 客服 id
  162. * @param array $exceptIds 要排除的ids (正在服务的用户)
  163. * @return array
  164. */
  165. public function getCustomersFormatWaiting($room_id)
  166. {
  167. // 获取数据库历史
  168. $waitings = $this->driver('socket')->getCustomersWaiting($room_id);
  169. // 格式化数据
  170. $waitings = $this->chatUsersFormat($room_id, $waitings);
  171. return $waitings;
  172. }
  173. /**
  174. * 获取客服服务的历史用户
  175. *
  176. * @param integer $room_id 房间号
  177. * @param integer $customer_service_id 客服 id
  178. * @param array $exceptIds 要排除的ids (正在服务的用户)
  179. * @return array
  180. */
  181. public function getCustomersHistoryFormatByCustomerService($room_id, $customer_service_id, $exceptIds = [])
  182. {
  183. // 获取数据库历史
  184. $histories = $this->driver('db')->getCustomersHistoryByCustomerService($room_id, $customer_service_id, $exceptIds = []);
  185. // 格式化数据
  186. $histories = $this->chatUsersFormat($room_id, $histories, ['select_identify' => 'customer', 'is_customer_service' => true]);
  187. return $histories;
  188. }
  189. /**
  190. * 设置最后一条消息和未读消息数
  191. *
  192. * @param string $room_id
  193. * @param array $chatUsers
  194. * @param string $select_identify 查询对象,默认客服查用户的,用户查客服的
  195. * @return array
  196. */
  197. public function chatUsersFormat($room_id, $chatUsers, $params = [])
  198. {
  199. $select_identify = $params['select_identify'] ?? 'customer';
  200. $is_customer_service = $params['is_customer_service'] ?? false;
  201. $first = $params['first'] ?? false;
  202. foreach ($chatUsers as &$chatUser) {
  203. // 获取在线状态
  204. $status = $this->driver('socket')->isUIdOnline($chatUser['session_id'], 'customer');
  205. $chatUser['status'] = $status; // 在线状态
  206. // 最后一条消息,未读消息条数
  207. $chatUser['last_message'] = $this->driver('db')->getMessageLastByChatUser($room_id, $chatUser['id']);
  208. $chatUser['unread_num'] = $this->driver('db')->getMessageUnReadNumByChatUserAndIndentify($room_id, $chatUser['id'], $select_identify);
  209. // 当前的客服
  210. $chatUser['customer_service'] = null;
  211. if ($is_customer_service) { // 需要客户的客服信息,【历史用户中】
  212. $chatUser['customer_service'] = $this->driver('socket')->getCustomerServiceByCustomerSessionId($chatUser['session_id']); // 如果在线,并且已经接入客服,当前客服信息
  213. }
  214. }
  215. return $first ? current($chatUsers) : $chatUsers;
  216. }
  217. /**
  218. * auth 实例方法注入
  219. *
  220. * @return Db|Socket
  221. */
  222. public function provider()
  223. {
  224. $classProt = $this->driver . 'Provider';
  225. if ($this->$classProt) {
  226. return $this->$classProt;
  227. }
  228. $class = "\\addons\\shopro\\library\\chat\\provider\\getter\\" . ucfirst($this->driver);
  229. if (class_exists($class)) {
  230. $this->$classProt = new $class($this, $this->socket, $this->io, $this->nsp);
  231. return $this->$classProt;
  232. }
  233. throw new ShoproException('getter 驱动不支持');
  234. }
  235. /**
  236. * 静态调用
  237. *
  238. * @param string $funcname
  239. * @param array $arguments
  240. * @return mixed
  241. */
  242. public function __call($funcname, $arguments)
  243. {
  244. return $this->provider()->{$funcname}(...$arguments);
  245. }
  246. }