|
@@ -162,7 +162,6 @@ class ChatService extends BaseService
|
|
|
return [];
|
|
return [];
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- $re = Yii::$app->redis;
|
|
|
|
|
$list = Yii::$app->redis->executeCommand('LRANGE', [$roomName, 0, -1]);
|
|
$list = Yii::$app->redis->executeCommand('LRANGE', [$roomName, 0, -1]);
|
|
|
if (empty($list)) {
|
|
if (empty($list)) {
|
|
|
return [];
|
|
return [];
|
|
@@ -183,7 +182,12 @@ class ChatService extends BaseService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------ 以下是新增的聊天方法 ------------------------------------------------
|
|
// ------------------------------------------------ 以下是新增的聊天方法 ------------------------------------------------
|
|
|
- //最近聊天人(人:可能是客户,也可能是商家)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 最近聊天人(人:可能是客户,也可能是商家)
|
|
|
|
|
+ * @param int $userId
|
|
|
|
|
+ * @return array|\yii\db\ActiveRecord[]
|
|
|
|
|
+ */
|
|
|
public static function getLatestUsers($userId)
|
|
public static function getLatestUsers($userId)
|
|
|
{
|
|
{
|
|
|
$cacheKey = ChatClass::getLatestUsersKey($userId);
|
|
$cacheKey = ChatClass::getLatestUsersKey($userId);
|
|
@@ -231,4 +235,45 @@ class ChatService extends BaseService
|
|
|
return array_values($shopList);
|
|
return array_values($shopList);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param int $userId 用户id
|
|
|
|
|
+ * @param string $unit 单位分:个数(person)消息(message)
|
|
|
|
|
+ * @return int
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function getUnReadMsgCount($userId, $unit='person')
|
|
|
|
|
+ {
|
|
|
|
|
+ $cacheKey = ChatClass::getLatestUsersKey($userId);
|
|
|
|
|
+ //返回有序集合,从高分到低分排序
|
|
|
|
|
+ $return = Yii::$app->redis->executeCommand('ZREVRANGE', [$cacheKey, 0, -1, 'WITHSCORES']);
|
|
|
|
|
+ if (empty($return)) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $i = 0;
|
|
|
|
|
+ $arr = [];
|
|
|
|
|
+ foreach ($return as $key => $val) {
|
|
|
|
|
+ if ($key % 2 == 0) {
|
|
|
|
|
+ $arr[$i]['sId'] = $val;//shopId
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $arr[$i]['score'] = $val;
|
|
|
|
|
+ $i++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $msgCount = 0;
|
|
|
|
|
+ foreach ($arr as $val) {
|
|
|
|
|
+ $score = floatval($val['score']);
|
|
|
|
|
+ $decimalPart = $score - floor($score);
|
|
|
|
|
+ $unReadNum = intval($decimalPart * 10000 + 0.5); // 加0.5 -- 使用四舍五入来处理浮点数精度问题
|
|
|
|
|
+
|
|
|
|
|
+ //根据未读消息数的单位进行计算总数
|
|
|
|
|
+ if ($unit == 'person'){
|
|
|
|
|
+ $msgCount += ($unReadNum >= 1 ? 1 : 0);
|
|
|
|
|
+ } else if ($unit == 'message'){
|
|
|
|
|
+ $msgCount += $unReadNum;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $msgCount;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|