Session.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace addons\shopro\library\chat\traits;
  3. /**
  4. * 客服 session 工具类
  5. */
  6. trait Session
  7. {
  8. /**
  9. * 获取存储的数据,当前 socket
  10. *
  11. * @param string $name
  12. * @param mixed $value
  13. * @return mixed
  14. */
  15. public function session($name = null, $value = '')
  16. {
  17. $data = $this->socket->session ?? [];
  18. if (is_null($name)) {
  19. // 获取全部数据
  20. return $data;
  21. }
  22. if ('' === $value) {
  23. // 获取缓存
  24. return 0 === strpos($name, '?') ? isset($data[$name]) : ($data[$name] ?? null);
  25. } elseif (is_null($value)) {
  26. // 删除缓存
  27. unset($data[$name]);
  28. $this->socket->session = $data;
  29. }
  30. // 存数据
  31. $data[$name] = $value;
  32. $this->socket->session = $data;
  33. }
  34. /**
  35. * 通过 client_id 获取指定 client_id 的 session 数据
  36. *
  37. * @param string $client_id 要获取 session 的 client_id
  38. * @param string $name 要获取 session 中的特定 name
  39. * @return string|array
  40. */
  41. public function getSession($client_id, $name = null)
  42. {
  43. $client = $this->nsp->sockets[$client_id] ?? null;
  44. $session = is_null($client) ? null : (isset($client->session) && $client->session ? $client->session : []);
  45. return $name ? ($session[$name] ?? null) : $session;
  46. }
  47. /**
  48. * 调用 getSession,别名
  49. *
  50. * @param string $client_id 要获取 session 的 client_id
  51. * @param string $name 要获取 session 中的特定 name
  52. * @return string|array
  53. */
  54. public function getSessionByClientId($client_id, $name = null)
  55. {
  56. return $this->getSession($client_id, $name);
  57. }
  58. /**
  59. * 通过 client_ids 获取指定 client_ids 的 session 数据集合
  60. *
  61. * @param string $client_id 要获取 session 的 client_id
  62. * @param string $name 要获取 session 中的特定 name
  63. * @return array
  64. */
  65. public function getSessionByClientIds($clientIds, $name = null)
  66. {
  67. $customerServices = [];
  68. foreach ($clientIds as $client_id) {
  69. $currentCustomerService = $this->getSessionByClientId($client_id, $name);
  70. if ($currentCustomerService) {
  71. $customerServices[] = $currentCustomerService;
  72. }
  73. }
  74. return $customerServices;
  75. }
  76. /**
  77. * 通过 client_id 更新指定 client_id 的 session 数据
  78. *
  79. * @param string $client_id 要获取 session 的 client_id
  80. * @param array $data 要更新 session 中的特定 name
  81. * @return boolean
  82. */
  83. public function updateSession($client_id, $data = [])
  84. {
  85. $client = $this->nsp->sockets[$client_id] ?? null;
  86. if (!$client) {
  87. // client 没有,可能下线了,直接返回
  88. return false;
  89. }
  90. // session 不存在,初始化
  91. if (!isset($client->session)) {
  92. $this->nsp->sockets[$client_id]->session = [];
  93. }
  94. // 更新对应的键
  95. foreach ($data as $key => $value) {
  96. $current = $this->nsp->sockets[$client_id]->session[$key] ?? null;
  97. if (is_array($value) && $current) {
  98. // 合并数组,比如修改 session 中 customer_service 客服信息 中的 status 在线状态(注意,如果value 为空,则覆盖,如果有值,则合并)
  99. $this->nsp->sockets[$client_id]->session[$key] = $value ? array_merge($current, $value) : $value;
  100. } else {
  101. $this->nsp->sockets[$client_id]->session[$key] = $value;
  102. }
  103. }
  104. return true;
  105. }
  106. /**
  107. * 调用 updateSession,别名
  108. *
  109. * @param string $client_id 要获取 session 的 client_id
  110. * @param array $data 要更新 session 中的特定 name
  111. * @return boolean
  112. */
  113. public function updateSessionByClientId($client_id, $data = [])
  114. {
  115. return $this->updateSession($client_id, $data);
  116. }
  117. /**
  118. * 通过 client_id 更新指定 client_id 的 session 数据集合
  119. *
  120. * @param string $client_id 要获取 session 的 client_id
  121. * @param string $name 要获取 session 中的特定 name
  122. * @return array
  123. */
  124. public function updateSessionByClientIds($clientIds, $data = [])
  125. {
  126. foreach ($clientIds as $client_id) {
  127. $this->updateSessionByClientId($client_id, $data);
  128. }
  129. return true;
  130. }
  131. }