Ver código fonte

删除15天前的联系人

shish 6 anos atrás
pai
commit
03eb0b09d0

+ 2 - 1
app/business/controllers/ChatController.php

@@ -97,12 +97,13 @@ class ChatController extends BaseController
 	//最近联系人 shish 2019.12.29
 	public function actionRecentlyUser()
 	{
-	
+		$ids = [12536630,12536638,12536637,12536641,12536635];
 	}
 
 	//最近聊天的内容 shish 2019.12.29
 	public function actionRecentlyChat()
 	{
+		//商品 图片 快捷链接 文字
 		$id = Yii::$app->request->get('id');
 	}
 

+ 10 - 2
app/client/controllers/ChatController.php

@@ -2,6 +2,7 @@
 
 namespace client\controllers;
 
+use biz\MQ\services\ChatService;
 use common\components\util;
 use Yii;
 use GatewayClient\Gateway;
@@ -39,6 +40,13 @@ class ChatController extends BaseController
 			'type' => 'msg',
 			'msg' => $msg,
 		));
+		$merchantId = $this->merchantId;
+		$userId = $this->userId;
+		$userId = rand(111, 99999);
+		ChatService::addUnReplyUser($merchantId,$userId);
+		$r = ChatService::getUnReplyUserList($merchantId);
+		dd($r);
+		die;
 		$chatOnline = Gateway::getClientIdByUid($id);
 		if ($chatOnline) {
 			//直接发送
@@ -55,7 +63,7 @@ class ChatController extends BaseController
 			));
 			Gateway::sendToUid($consoleId, json_encode($data));
 		}
-		//保存起来
+		
 	}
-
+	
 }

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

@@ -0,0 +1,34 @@
+<?php
+
+namespace biz\MQ\classes;
+
+use Yii;
+use biz\base\classes\BaseClass;
+
+class ChatClass extends BaseClass
+{
+	
+	//取有新消息客户的缓存键名 2019.12.29
+	public static function getUnReplyUserKey($merchantId)
+	{
+		return 'merchantUnReplyUserList' . $merchantId;
+	}
+	
+	//获取未读消息的键 2019.12.29
+	public static function getUnreadNumKey($useId)
+	{
+		return 'MerchantUnreadNum' . $useId;
+	}
+	
+	//最近联系人的键 shish 2019.12.29
+	public static function getRecentlyUserKey($merchantId)
+	{
+		return "merchantRecentlyUser" . $merchantId;
+	}
+	
+	public static function getReadKey($useId)
+	{
+		return 'MerchantRead' . $useId;
+	}
+	
+}

+ 141 - 0
biz/MQ/services/ChatService.php

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

+ 3 - 2
vendor/workerman/GatewayWorker/Applications/retail/Events.php

@@ -13,6 +13,7 @@ use \GatewayWorker\Lib\Gateway;
  */
 class Events
 {
+
 	// 当有客户端连接时,将client_id返回,让mvc框架判断当前uid并执行绑定
 	public static function onConnect($client_id)
 	{
@@ -21,7 +22,7 @@ class Events
 			'client_id' => $client_id
 		)));
 	}
-	
+
 	// GatewayWorker建议不做任何业务逻辑,onMessage留空即可
 	public static function onMessage($client_id, $message)
 	{
@@ -33,5 +34,5 @@ class Events
 	{
 
 	}
-	
+
 }