|
|
@@ -0,0 +1,141 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace biz\MQ\services;
|
|
|
+
|
|
|
+use biz\base\services\BaseService;
|
|
|
+use biz\MQ\classes\ChatClass;
|
|
|
+use Yii;
|
|
|
+
|
|
|
+class ChatService extends BaseService
|
|
|
+{
|
|
|
+
|
|
|
+ //新增未读消息数
|
|
|
+ public static function addUnreadNum($userId)
|
|
|
+ {
|
|
|
+ $key = ChatClass::getUnreadNumKey($userId);
|
|
|
+ Yii::$app->redis->executeCommand('INCRBY', [$key, 1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取未读消息数
|
|
|
+ public static function getUnreadNum($userId)
|
|
|
+ {
|
|
|
+ $key = ChatClass::getUnreadNumKey($userId);
|
|
|
+ $respond = Yii::$app->redis->executeCommand('GET', [$key]);
|
|
|
+ return is_numeric($respond) ? $respond : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增未回复客户 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)
|
|
|
+ {
|
|
|
+ $cacheKey = ChatClass::getRecentlyUserKey($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;
|
|
|
+ }
|
|
|
+
|
|
|
+ //保存已读消息记录 shish 2019.12.29
|
|
|
+ public static function saveRead($merchantId, $userId)
|
|
|
+ {
|
|
|
+ //商家的消息,ourSelf表示自己的消息 themSelf表示对方的消息
|
|
|
+ $now = time();
|
|
|
+ $cacheKey = ChatClass::getReadKey($userId);
|
|
|
+
|
|
|
+ //删除15天前的已读消息
|
|
|
+ $max = $now - 86400 * 16;
|
|
|
+ Yii::$app->redis->executeCommand('ZREMRANGEBYSCORE', [$cacheKey, 0, $max]);
|
|
|
+
|
|
|
+ $content = '{~CHAT~}themSelf{~CHAT~}您好';
|
|
|
+ Yii::$app->redis->executeCommand('ZADD', [$cacheKey, $now, $content]);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //取已读消息记录 shish 2019.12.29
|
|
|
+ public static function getRead($userId)
|
|
|
+ {
|
|
|
+ $cacheKey = ChatClass::getReadKey($userId);
|
|
|
+ //返回有序集合,从高分到低分排序
|
|
|
+ $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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|