ChatService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace bizHd\message\services;
  3. use bizHd\base\services\BaseService;
  4. use bizHd\message\classes\ChatClass;
  5. use bizHd\user\services\UserService;
  6. use common\components\arrayUtil;
  7. use Yii;
  8. class ChatService extends BaseService
  9. {
  10. //新增未读消息数
  11. public static function addUnreadNum($userId)
  12. {
  13. $key = ChatClass::getUnreadNumKey($userId);
  14. Yii::$app->redis->executeCommand('INCRBY', [$key, 1]);
  15. }
  16. //新增未回复客户 ssh 2019.12.29
  17. public static function addUnReplyUser($sjId, $userId)
  18. {
  19. $now = time();
  20. $cacheKey = ChatClass::getUnReplyUserKey($sjId);
  21. Yii::$app->redis->executeCommand('ZADD', [$cacheKey, $now, "{$userId}"]);
  22. }
  23. //取未回复客户列表 ssh 2019.12.29
  24. public static function getUnReplyUserList($sjId)
  25. {
  26. $cacheKey = ChatClass::getUnReplyUserKey($sjId);
  27. $return = Yii::$app->redis->executeCommand('ZREVRANGE', [$cacheKey, 0, -1, 'WITHSCORES']);//返回有序集合,从高分到低分排序
  28. if (empty($return)) {
  29. return [];
  30. }
  31. $list = [];
  32. $i = 0;
  33. $arr = [];
  34. foreach ($return as $key => $val) {
  35. if ($key % 2 == 0) {
  36. $arr[$i]['value'] = $val;
  37. } else {
  38. $arr[$i]['key'] = $val;
  39. $i++;
  40. }
  41. }
  42. foreach ($arr as $key => $val) {
  43. $list[$val['key']] = $val['value'];
  44. }
  45. return $list;
  46. }
  47. //新增最近联系人
  48. public static function addRecentlyUser($sjId, $userId)
  49. {
  50. $now = time();
  51. $cacheKey = ChatClass::getRecentlyUserKey($sjId);
  52. //删除10天前的联系人
  53. $max = $now - 86400 * 10;
  54. Yii::$app->redis->executeCommand('ZREMRANGEBYSCORE', [$cacheKey, 0, $max]);
  55. Yii::$app->redis->executeCommand('ZADD', [$cacheKey, $now, "{$userId}"]);
  56. }
  57. //取最近联系人 ssh 2019.12.29
  58. public static function getRecentlyUserList($mainId)
  59. {
  60. $userIdList = [12536630, 12536631, 12536632, 12536633, 12536634, 12536635];
  61. $userId = $userIdList[array_rand($userIdList)];
  62. self::addRecentlyUser($mainId, $userId);
  63. $cacheKey = ChatClass::getRecentlyUserKey($mainId);
  64. //返回有序集合,从高分到低分排序
  65. $return = Yii::$app->redis->executeCommand('ZREVRANGE', [$cacheKey, 0, -1, 'WITHSCORES']);
  66. if (empty($return)) {
  67. return [];
  68. }
  69. $i = 0;
  70. $arr = [];
  71. foreach ($return as $key => $val) {
  72. if ($key % 2 == 0) {
  73. $arr[$i]['userId'] = $val;
  74. } else {
  75. $arr[$i]['time'] = $val;
  76. $i++;
  77. }
  78. }
  79. $userData = [];
  80. //组合出最后聊天时间和未读消息数
  81. foreach ($arr as $val) {
  82. $userId = $val['userId'];
  83. $time = $val['time'];
  84. $num = ChatClass::getUnreadNum($userId);
  85. $date = date("m-d H:i", $time);
  86. if (date("Ymd") == date("Ymd", $time)) {
  87. $date = date("H:i", $time);
  88. }
  89. $userData[$userId] = ['time' => $date, 'unReadNum' => $num];
  90. }
  91. $ids = array_column($arr, 'userId');
  92. $infoList = UserService::getUserByIds($ids);
  93. foreach ($infoList as $userId => $info) {
  94. $infoList[$userId]['unReadNum'] = isset($userData[$userId]['unReadNum']) ? $userData[$userId]['unReadNum'] : 0;
  95. $infoList[$userId]['chatTime'] = isset($userData[$userId]['time']) ? $userData[$userId]['time'] : date("m-d H:i", $info['visitTime']);
  96. }
  97. return array_values($infoList);
  98. }
  99. //保存消息记录 ssh 2019.12.29
  100. public static function saveMessage($messageList, $userId)
  101. {
  102. //商家的消息,ourSelf表示自己的消息 themSelf表示对方的消息
  103. $now = time();
  104. $cacheKey = ChatClass::getMessageKey($userId);
  105. //删除15天前的已读消息
  106. $max = $now - 86400 * 16;
  107. Yii::$app->redis->executeCommand('ZREMRANGEBYSCORE', [$cacheKey, 0, $max]);
  108. foreach ($messageList as $message) {
  109. Yii::$app->redis->executeCommand('ZADD', [$cacheKey, $now, json_encode($message)]);
  110. }
  111. //self::getMessageList($userId);
  112. }
  113. //取消息记录 ssh 2019.12.29
  114. public static function getMessageList($userId)
  115. {
  116. $cacheKey = ChatClass::getMessageKey($userId);
  117. //返回有序集合,从高分到低分排序
  118. $return = Yii::$app->redis->executeCommand('ZREVRANGE', [$cacheKey, 0, -1, 'WITHSCORES']);
  119. if (empty($return)) {
  120. return [];
  121. }
  122. $i = 0;
  123. $arr = [];
  124. foreach ($return as $key => $val) {
  125. if ($key % 2 == 0) {
  126. $arr[$i]['message'] = $val;
  127. } else {
  128. $arr[$i]['time'] = $val;
  129. $i++;
  130. }
  131. }
  132. $arr = arrayUtil::arraySort($arr, 'time');
  133. $data = [];
  134. foreach ($arr as $val) {
  135. $message = $val['message'];
  136. $data[] = json_decode($message);
  137. }
  138. return $data;
  139. }
  140. }