redis->executeCommand('INCRBY', [$key, 1]); } //新增未回复客户 shish 2019.12.29 public static function addUnReplyUser($merchantId, $userId) { $now = time(); $cacheKey = ChatClass::getUnReplyUserKey($merchantId); Yii::$app->redis->executeCommand('ZADD', [$cacheKey, $now, "{$userId}"]); } //取未回复客户列表 shish 2019.12.29 public static function getUnReplyUserList($merchantId) { $cacheKey = ChatClass::getUnReplyUserKey($merchantId); $return = Yii::$app->redis->executeCommand('ZREVRANGE', [$cacheKey, 0, -1, 'WITHSCORES']);//返回有序集合,从高分到低分排序 if (empty($return)) { return []; } $list = []; $i = 0; $arr = []; foreach ($return as $key => $val) { if ($key % 2 == 0) { $arr[$i]['value'] = $val; } else { $arr[$i]['key'] = $val; $i++; } } foreach ($arr as $key => $val) { $list[$val['key']] = $val['value']; } return $list; } //新增最近联系人 public static function addRecentlyUser($merchantId, $userId) { $now = time(); $cacheKey = ChatClass::getRecentlyUserKey($merchantId); //删除10天前的联系人 $max = $now - 86400 * 10; Yii::$app->redis->executeCommand('ZREMRANGEBYSCORE', [$cacheKey, 0, $max]); Yii::$app->redis->executeCommand('ZADD', [$cacheKey, $now, "{$userId}"]); } //取最近联系人 shish 2019.12.29 public static function getRecentlyUserList($merchantId) { $userIdList = [12536630, 12536631, 12536632, 12536633, 12536634, 12536635]; $userId = $userIdList[array_rand($userIdList)]; self::addRecentlyUser($merchantId, $userId); $cacheKey = ChatClass::getRecentlyUserKey($merchantId); //返回有序集合,从高分到低分排序 $return = Yii::$app->redis->executeCommand('ZREVRANGE', [$cacheKey, 0, -1, 'WITHSCORES']); if (empty($return)) { return []; } $i = 0; $arr = []; foreach ($return as $key => $val) { if ($key % 2 == 0) { $arr[$i]['userId'] = $val; } else { $arr[$i]['time'] = $val; $i++; } } $userData = []; //组合出最后聊天时间和未读消息数 foreach ($arr as $val) { $userId = $val['userId']; $time = $val['time']; $num = ChatClass::getUnreadNum($userId); $date = date("m-d H:i", $time); if (date("Ymd") == date("Ymd", $time)) { $date = date("H:i", $time); } $userData[$userId] = ['time' => $date, 'unReadNum' => $num]; } $ids = array_column($arr, 'userId'); $infoList = UserService::getUserByIds($ids); foreach ($infoList as $userId => $info) { $infoList[$userId]['unReadNum'] = isset($userData[$userId]['unReadNum']) ? $userData[$userId]['unReadNum'] : 0; $infoList[$userId]['chatTime'] = isset($userData[$userId]['time']) ? $userData[$userId]['time'] : date("m-d H:i", $info['visitTime']); } return array_values($infoList); } //保存消息记录 shish 2019.12.29 public static function saveMessage($messageList, $userId) { //商家的消息,ourSelf表示自己的消息 themSelf表示对方的消息 $now = time(); $cacheKey = ChatClass::getMessageKey($userId); //删除15天前的已读消息 $max = $now - 86400 * 16; Yii::$app->redis->executeCommand('ZREMRANGEBYSCORE', [$cacheKey, 0, $max]); foreach ($messageList as $message) { Yii::$app->redis->executeCommand('ZADD', [$cacheKey, $now, json_encode($message)]); } //self::getMessageList($userId); } //取消息记录 shish 2019.12.29 public static function getMessageList($userId) { $cacheKey = ChatClass::getMessageKey($userId); //返回有序集合,从高分到低分排序 $return = Yii::$app->redis->executeCommand('ZREVRANGE', [$cacheKey, 0, -1, 'WITHSCORES']); if (empty($return)) { return []; } $i = 0; $arr = []; foreach ($return as $key => $val) { if ($key % 2 == 0) { $arr[$i]['message'] = $val; } else { $arr[$i]['time'] = $val; $i++; } } $arr = arrayUtil::arraySort($arr, 'time'); $data = []; foreach ($arr as $val) { $message = $val['message']; $data[] = json_decode($message); } return $data; } }