Browse Source

最近联系人

shish 6 years ago
parent
commit
ff286fe230

+ 10 - 8
app/business/controllers/ChatController.php

@@ -4,6 +4,7 @@ namespace business\controllers;
 
 use biz\goods\services\GoodsService;
 use biz\merchant\services\MerchantService;
+use biz\MQ\services\ChatService;
 use biz\user\services\UserService;
 use common\components\business;
 use Yii;
@@ -12,7 +13,7 @@ use GatewayClient\Gateway;
 
 class ChatController extends BaseController
 {
-
+	
 	//聊天 shish 2019.12.28
 	public function actionIndex()
 	{
@@ -22,7 +23,7 @@ class ChatController extends BaseController
 		UserService::valid($user, $this->merchantId);
 		return $this->renderPartial('index', ['userId' => $userId]);
 	}
-
+	
 	//绑定 shish 2019.12.28
 	public function actionBind()
 	{
@@ -31,7 +32,7 @@ class ChatController extends BaseController
 		$id = 'merchantChat_' . $this->merchantId;
 		Gateway::bindUid($clientId, $id);
 	}
-
+	
 	//发消息给客户 shish 2019.12.28
 	public function actionSend()
 	{
@@ -42,7 +43,7 @@ class ChatController extends BaseController
 		UserService::valid($user, $this->merchantId);
 		$arr = [];
 		$arr['type'] = $type;
-		
+		$arr['source'] = 'merchant';
 		switch ($type) {
 			case 'text':
 				//文字
@@ -81,7 +82,7 @@ class ChatController extends BaseController
 		$minute = (int)date("i");
 		if ($minute % 3 == 0) {
 			$time = date("i:s");
-			$data[] = ['type' => 'time', 'time' => $time];
+			$data[] = ['type' => 'time', 'time' => $time, 'source' => ''];
 		}
 		
 		$online = Gateway::getClientIdByUid($userId);
@@ -93,11 +94,12 @@ class ChatController extends BaseController
 		}
 		util::success($data);
 	}
-
+	
 	//最近联系人 shish 2019.12.29
 	public function actionRecentlyUser()
 	{
-		$ids = [12536630,12536638,12536637,12536641,12536635];
+		$respond = ChatService::getRecentlyUserList($this->merchantId);
+		util::success($respond);
 	}
 
 	//最近聊天的内容 shish 2019.12.29
@@ -106,5 +108,5 @@ class ChatController extends BaseController
 		//商品 图片 快捷链接 文字
 		$id = Yii::$app->request->get('id');
 	}
-
+	
 }

+ 8 - 0
biz/MQ/classes/ChatClass.php

@@ -31,4 +31,12 @@ class ChatClass extends BaseClass
 		return 'MerchantRead' . $useId;
 	}
 	
+	//获取未读消息数
+	public static function getUnreadNum($userId)
+	{
+		$key = self::getUnreadNumKey($userId);
+		$respond = Yii::$app->redis->executeCommand('GET', [$key]);
+		return is_numeric($respond) ? $respond : 0;
+	}
+	
 }

+ 30 - 19
biz/MQ/services/ChatService.php

@@ -4,6 +4,7 @@ namespace biz\MQ\services;
 
 use biz\base\services\BaseService;
 use biz\MQ\classes\ChatClass;
+use biz\user\services\UserService;
 use Yii;
 
 class ChatService extends BaseService
@@ -16,14 +17,6 @@ class ChatService extends BaseService
 		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)
 	{
@@ -62,40 +55,58 @@ class ChatService extends BaseService
 	{
 		$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 [];
 		}
-		$list = [];
 		$i = 0;
 		$arr = [];
 		foreach ($return as $key => $val) {
 			if ($key % 2 == 0) {
-				$arr[$i]['value'] = $val;
+				$arr[$i]['userId'] = $val;
 			} else {
-				$arr[$i]['key'] = $val;
+				$arr[$i]['time'] = $val;
 				$i++;
 			}
 		}
-		foreach ($arr as $key => $val) {
-			$list[$val['key']] = $val['value'];
+		$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];
 		}
-		return $list;
+		$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 saveRead($merchantId, $userId)
 	{
@@ -113,7 +124,7 @@ class ChatService extends BaseService
 	}
 	
 	//取已读消息记录 shish 2019.12.29
-	public static function getRead($userId)
+	public static function getReadList($userId)
 	{
 		$cacheKey = ChatClass::getReadKey($userId);
 		//返回有序集合,从高分到低分排序
@@ -137,5 +148,5 @@ class ChatService extends BaseService
 		}
 		return $list;
 	}
-
+	
 }

+ 8 - 11
biz/user/classes/UserClass.php

@@ -3,7 +3,9 @@
 namespace biz\user\classes;
 
 use biz\merchant\classes\MerchantAssetClass;
+use biz\MQ\classes\ChatClass;
 use biz\user\services\UserIntegralService;
+use common\components\business;
 use common\components\configDict;
 use common\components\util;
 use common\services\ImageService;
@@ -48,7 +50,6 @@ class UserClass extends BaseClass
 		$ids = array_column($list, 'id');
 		$ids = array_unique($ids);
 		$asset = UserAssetClass::getByUserIds($ids, 'userId');
-		$imgUrl = Yii::$app->params['imgUrl'];
 		foreach ($list as $key => $val) {
 			$userId = $val['id'];
 			//访问时间
@@ -57,10 +58,10 @@ class UserClass extends BaseClass
 				$visitTime = '今天 ' . date("H:i", $val['visitTime']);
 			}
 			$list[$key]['visitTimeFormat'] = $visitTime;
-			$list[$key]['avatarUrl'] = $imgUrl . $val['avatar'];
 			$list[$key]['userAsset'] = isset($asset[$userId]) ? $asset[$userId] : [];
 			//客户发来新消息数
-			$list[$key]['messageNum'] = rand(0, 19);
+			$list[$key]['unReadNum'] = ChatClass::getUnreadNum($userId);
+			$list[$key] = business::formatUserAvatar($list[$key]);
 		}
 		return $list;
 	}
@@ -74,7 +75,7 @@ class UserClass extends BaseClass
 	}
 	
 	//根据手机号查询客户 shish 2019.12.1
-	public static function getByMobile($mobile,$merchantId)
+	public static function getByMobile($mobile, $merchantId)
 	{
 		return self::getByCondition(['merchantId' => $merchantId, 'mobile' => $mobile]);
 	}
@@ -404,7 +405,7 @@ class UserClass extends BaseClass
 			util::fail('这个客户您没有权限操作');
 		}
 	}
-
+	
 	//根据消费次数和有没领优惠券来判断是不是新人
 	public static function newUser($userInfo)
 	{
@@ -426,12 +427,8 @@ class UserClass extends BaseClass
 		if (empty($info)) {
 			return [];
 		}
-		$imgUrl = Yii::$app->params['imgUrl'];
-		foreach ($info as $userId => $user) {
-			$info[$userId]['avatar'] = !empty($user['avatar']) ? $imgUrl . $user['avatar'] : '';
-			$info[$userId]['avatarUrl'] = !empty($user['avatar']) ? $imgUrl . $user['avatar'] : '';
-		}
-		return $info;
+		$data = self::groupUserBaseInfo($info);
+		return $data;
 	}
 
 }

+ 5 - 5
common/components/business.php

@@ -117,13 +117,13 @@ class business
 		$imgUrl = isset($info['avatar']) ? $info['avatar'] : '';
 		if (empty($imgUrl)) {
 			$info['avatarUrl'] = '';
-			$info['avatarSmallUrl'] = '';
+			$info['smallAvatarUrl'] = '';
 			return $info;
 		}
 		$prefixImgUrl = Yii::$app->params['imgUrl'];
 		$smallImg = self::getSmallImg($imgUrl);
 		$small = $prefixImgUrl . $smallImg;
-		$info['avatarSmallUrl'] = $small;
+		$info['smallAvatarUrl'] = $small;
 		$info['avatarUrl'] = $prefixImgUrl . $imgUrl;
 		return $info;
 	}
@@ -134,13 +134,13 @@ class business
 		$imgUrl = isset($info['logo']) ? $info['logo'] : '';
 		if (empty($imgUrl)) {
 			$info['logoUrl'] = '';
-			$info['logoSmallUrl'] = '';
+			$info['smallLogoUrl'] = '';
 			return $info;
 		}
 		$prefixImgUrl = Yii::$app->params['imgUrl'];
 		$smallImg = self::getSmallImg($imgUrl);
 		$small = $prefixImgUrl . $smallImg;
-		$info['logoSmallUrl'] = $small;
+		$info['smallLogoUrl'] = $small;
 		$info['logoUrl'] = $prefixImgUrl . $imgUrl;
 		return $info;
 	}
@@ -151,7 +151,7 @@ class business
 		$prefix = Yii::$app->params['imgUrl'];
 		$small = self::getSmallImg($imgUrl);
 		$url = $prefix . $imgUrl;
-		$smallUrl = $prefixImgUrl . $small;
+		$smallUrl = $prefix . $small;
 		return ['url' => $url, 'smallUrl' => $smallUrl];
 	}