|
|
@@ -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);
|
|
|
+ }
|
|
|
+
|
|
|
}
|