Explorar o código

聊天列表接口

shizhongqi hai 11 meses
pai
achega
38b601c731

+ 9 - 1
app-hd/controllers/ChatController.php

@@ -14,7 +14,6 @@ use GatewayClient\Gateway;
 
 class ChatController extends BaseController
 {
-
     //聊天 ssh 2019.12.28
     public function actionIndex()
     {
@@ -144,6 +143,15 @@ class ChatController extends BaseController
         util::success($respond);
     }
 
+
+    // ----------------------------  新聊天功能的接口  ----------------------------------
+    //最近聊天人
+    public function actionLatestUsers()
+    {
+        $respond = ChatService::getLatestUsers($this->shopId);
+        util::success($respond);
+    }
+
     //聊天的内容
     public function actionChatHistory()
     {

+ 1 - 0
app-mall/controllers/BaseController.php

@@ -31,6 +31,7 @@ class BaseController extends PublicController
                 util::fail('请先登录哈');
             }
         }
+        $userId = intval($userId);
         $this->userId = $userId;
         $user = UserClass::getById($userId, true);
         if (empty($user)) {

+ 7 - 0
app-mall/controllers/ChatController.php

@@ -110,6 +110,13 @@ class ChatController extends BaseController
         util::success($respond);
     }
 
+    // ----------------------------  新聊天功能的接口  ----------------------------------
+    //最近聊天人
+    public function actionLatestUsers()
+    {
+        $respond = ChatService::getLatestUsers($this->userId);
+        util::success($respond);
+    }
 
     //聊天的内容
     public function actionChatHistory()

+ 9 - 1
biz-hd/message/classes/ChatClass.php

@@ -5,9 +5,9 @@ namespace bizHd\message\classes;
 use Yii;
 use bizHd\base\classes\BaseClass;
 
+// 只是 redis 模型(与 mysql 无关)
 class ChatClass extends BaseClass
 {
-
     //取有新消息客户的缓存键名 2019.12.29
     public static function getUnReplyUserKey($mainId)
     {
@@ -26,6 +26,7 @@ class ChatClass extends BaseClass
         return "merchantRecentlyUser" . $mainId;
     }
 
+    //获取消息的键
     public static function getMessageKey($useId)
     {
         return 'MerchantMessage' . $useId;
@@ -39,4 +40,11 @@ class ChatClass extends BaseClass
         return is_numeric($respond) ? $respond : 0;
     }
 
+    // ----------------- 新建的 获取键的方法(参考上面的得来)----------------------
+    //最近联系人列表的键
+    public static function getLatestUsersKey($shopId)
+    {
+        return "shop_chats:" . $shopId;
+    }
+
 }

+ 44 - 0
biz-hd/message/services/ChatService.php

@@ -3,7 +3,9 @@
 namespace bizHd\message\services;
 
 use bizHd\base\services\BaseService;
+use bizHd\custom\classes\CustomClass;
 use bizHd\message\classes\ChatClass;
+use bizHd\user\classes\UserClass;
 use bizHd\user\services\UserService;
 use common\components\arrayUtil;
 use Yii;
@@ -153,6 +155,48 @@ class ChatService extends BaseService
         return $data;
     }
 
+    // ------------------------------------------------  以下是新增的聊天方法  ------------------------------------------------
+    //最近聊天人(人:可能是客户,也可能是商家)
+    public static function getLatestUsers($shopId)
+    {
+        $cacheKey = ChatClass::getLatestUsersKey($shopId);
+        //返回有序集合,从高分到低分排序
+        $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]['cId'] = $val;
+            } else {
+                $arr[$i]['time'] = $val;
+                $i++;
+            }
+        }
+        $userData = [];
+        //组合出最后聊天时间和未读消息数
+        foreach ($arr as $val) {
+            $customId = $val['cId'];
+            $float = floatval($val['time']);
+            $decimalPart = $float - floor($float);
+            $unReadNum = intval($decimalPart * 10000);
+            $time = intval($val['time']);
+            $date = date("Y-m-d H:i", $time);
+
+            $userData[$customId] = ['time' => $date, 'unReadNum' => $unReadNum];
+        }
+        $ids = array_column($arr, 'cId');
+        $infoList = CustomClass::getByIds($ids);
+        foreach ($infoList as $customId => $info) {
+            $infoList[$customId]['unReadNum'] = isset($userData[$info['id']]['unReadNum']) ? $userData[$info['id']]['unReadNum'] : 0;
+            $infoList[$customId]['chatTime'] = isset($userData[$info['id']]['time']) ? $userData[$info['id']]['time'] : 0;
+        }
+        $infoList = arrayUtil::arraySort($infoList, 'chatTime', SORT_DESC);
+        return array_values($infoList);
+    }
+
     public static function getChatHistory($roomName)
     {
         if (empty($roomName)) {

+ 7 - 0
biz-mall/message/classes/ChatClass.php

@@ -38,5 +38,12 @@ class ChatClass extends BaseClass
 		$respond = Yii::$app->redis->executeCommand('GET', [$key]);
 		return is_numeric($respond) ? $respond : 0;
 	}
+
+    // ----------------- 新建的 获取键的方法(参考上面的得来)----------------------
+    //最近联系人列表的键
+    public static function getLatestUsersKey($userId)
+    {
+        return "customer_chats:" . $userId;
+    }
 	
 }

+ 52 - 0
biz-mall/message/services/ChatService.php

@@ -2,11 +2,14 @@
 
 namespace bizMall\message\services;
 
+use bizHd\custom\classes\HdClass;
 use bizMall\base\services\BaseService;
 use bizMall\message\classes\ChatClass;
+use bizMall\shop\classes\ShopClass;
 use bizMall\user\services\UserService;
 use common\components\arrayUtil;
 use Yii;
+use yii\helpers\ArrayHelper;
 
 class ChatService extends BaseService
 {
@@ -179,4 +182,53 @@ class ChatService extends BaseService
 		}
     }
 
+	// ------------------------------------------------  以下是新增的聊天方法  ------------------------------------------------
+    //最近聊天人(人:可能是客户,也可能是商家)
+    public static function getLatestUsers($userId)
+    {
+        $cacheKey = ChatClass::getLatestUsersKey($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]['shopId'] = $val;
+            } else {
+                $arr[$i]['time'] = $val;
+                $i++;
+            }
+        }
+        $userData = [];
+        //组合出最后聊天时间和未读消息数
+        foreach ($arr as $val) {
+            $shopId = $val['shopId'];
+            $float = floatval($val['time']);
+            $decimalPart = $float - floor($float);
+            $unReadNum = intval($decimalPart * 10000);
+            $time = intval($val['time']);
+            $date = date("Y-m-d H:i", $time);
+
+            $userData[$shopId] = ['time' => $date, 'unReadNum' => $unReadNum];
+        }
+        $ids = array_column($arr, 'shopId');
+        $where = [];
+        $where['userId'] = $userId;
+        $where['delStatus'] = 0;
+        $hdList = HdClass::getAllByCondition($where,'inTurn DESC, addTime DESC', 'customId, shopId',);
+        // 生成以 shopId 为键的新数组
+        $hdListByShopId = ArrayHelper::index($hdList, 'shopId');
+        $shopList = ShopClass::getByIds($ids, null, null, 'id, avatar, shopName, merchantName');
+        foreach ($shopList as $shopId => $info) {
+            $shopList[$shopId]['unReadNum'] = isset($userData[$info['id']]['unReadNum']) ? $userData[$info['id']]['unReadNum'] : 0;
+            $shopList[$shopId]['chatTime'] = isset($userData[$info['id']]['time']) ? $userData[$info['id']]['time'] : 0;
+			$shopList[$shopId]['customId'] = isset($hdListByShopId[$info['id']]['customId']) ? $hdListByShopId[$info['id']]['customId'] : 0;
+        }
+        $shopList = arrayUtil::arraySort($shopList, 'chatTime', SORT_DESC);
+        return array_values($shopList);
+    }
+
 }