Explorar el Código

新消息提醒

shish hace 6 años
padre
commit
d9e7f5f305

+ 19 - 7
app/business/controllers/ChatController.php

@@ -25,7 +25,7 @@ class ChatController extends BaseController
 	}
 	
 	//绑定 shish 2019.12.28
-	public function actionBind()
+	public function actionBindMerchant()
 	{
 		$post = Yii::$app->request->post();
 		$clientId = isset($post['clientId']) ? $post['clientId'] : '';
@@ -33,6 +33,22 @@ class ChatController extends BaseController
 		Gateway::bindUid($clientId, $id);
 	}
 	
+	//关闭当前链接
+	public function actionClose()
+	{
+		$post = Yii::$app->request->post();
+		$clientId = isset($post['clientId']) ? $post['clientId'] : '';
+		Gateway::closeClient($clientId);
+	}
+	
+	public function actionBindConsole()
+	{
+		$post = Yii::$app->request->post();
+		$clientId = isset($post['clientId']) ? $post['clientId'] : '';
+		$id = 'merchantConsole_' . $this->merchantId;
+		Gateway::bindUid($clientId, $id);
+	}
+	
 	//发消息给客户 shish 2019.12.28
 	public function actionSend()
 	{
@@ -76,18 +92,14 @@ class ChatController extends BaseController
 				$arr['price'] = isset($info['price']) ? $info['price'] : 300;
 				break;
 		}
-		
 		$data[] = $arr;
-		
 		//放入缓存
 		ChatService::saveMessage($data, $userId);
-		
 		$online = Gateway::getClientIdByUid($userId);
 		if ($online) {
 			//直接发送
 			Gateway::sendToUid($userId, json_encode($data));
-		} else {
-			//保存
+			util::success($data);
 		}
 		util::success($data);
 	}
@@ -109,5 +121,5 @@ class ChatController extends BaseController
 		$respond = ChatService::getMessageList($id);
 		util::success($respond);
 	}
-
+	
 }

+ 10 - 6
app/business/views/chat/index.php

@@ -1,9 +1,9 @@
 <butto onclick="openMsg()">打开聊天</butto>
-<butto onclick="open()">打开</butto>
 <butto onclick="send()">发消息</butto>
+<butto onclick="closeMsg()">关闭</butto>
 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 <script>
-    var clientId;
+    var clientId='';
     var ws;
     function openMsg() {
         ws = new WebSocket("ws://127.0.0.1:8282");
@@ -14,15 +14,19 @@
             switch (type) {
                 case 'init':
                     clientId = data.client_id;
-                    console.log(clientId);
+                    //当前发起的聊天会员与当前登陆用户绑定关系
+                    $.post('http://api.b.hhb.com/chat/bind-merchant', {clientId: clientId}, function (data) {
+
+                    }, 'json');
                 default :
             }
         };
     }
 
-    function open() {
-        //当前发起的聊天会员与当前登陆用户绑定关系
-        $.post('http://api.b.hhb.com/chat/bind', {clientId: clientId}, function (data) {
+    function closeMsg()
+    {
+        console.log(clientId);
+        $.post('http://api.b.hhb.com/chat/close', {clientId: clientId}, function (data) {
 
         }, 'json');
     }

+ 7 - 37
app/business/views/main/index.php

@@ -1,6 +1,4 @@
-<butto onclick="openMsg()">打开聊天</butto>
-<butto onclick="open()">打开</butto>
-<butto onclick="send()">发消息</butto>
+<butto onclick="openMsg()">打开系统</butto>
 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 <script>
     var clientId;
@@ -14,43 +12,15 @@
             switch (type) {
                 case 'init':
                     clientId = data.client_id;
-                    console.log(clientId);
+                    //当前发起的会话与平台绑定关系
+                    $.post('http://api.b.hhb.com/chat/bind-console', {clientId: clientId}, function (data) {
+                    }, 'json');
+                case 'newMessage':
+                    console.log('您有新消息,请注意查收');
+                    break;
                 default :
             }
         };
     }
 
-    function open() {
-        //当前发起的聊天会员与当前登陆用户绑定关系
-        $.post('http://api.b.hhb.com/chat/bind', {clientId: clientId}, function (data) {
-
-        }, 'json');
-    }
-
-    function send() {
-        var type = 'text';
-        var data = {type: type, userId:<?= $userId ?>}
-        switch (type) {
-            case 'text':
-                //发文字
-                data.content = '您好,有什么可以为你服务?';
-                break;
-            case 'img':
-                //发图片
-                data.shortUrl = '/uploads/201905/25/jpg/19052591518_12358.jpg';
-                break;
-            case 'link':
-                data.id = 1;
-                //发链接
-                break;
-            case 'goods':
-                data.id = 160;
-                //发商品
-                break;
-            default:
-        }
-        $.post('http://api.b.hhb.com/chat/send', data, function (data) {
-        }, 'json');
-    }
-
 </script>

+ 53 - 17
app/client/controllers/ChatController.php

@@ -2,7 +2,10 @@
 
 namespace client\controllers;
 
+use biz\goods\services\GoodsService;
+use biz\merchant\services\MerchantService;
 use biz\MQ\services\ChatService;
+use common\components\business;
 use common\components\util;
 use Yii;
 use GatewayClient\Gateway;
@@ -22,7 +25,7 @@ class ChatController extends BaseController
 	}
 	
 	//绑定 shish 2019.12.28
-	public function actionBind()
+	public function actionBindUser()
 	{
 		$post = Yii::$app->request->post();
 		$clientId = isset($post['clientId']) ? $post['clientId'] : '';
@@ -34,36 +37,69 @@ class ChatController extends BaseController
 	public function actionSend()
 	{
 		$post = Yii::$app->request->post();
-		$msg = isset($post['msg']) ? $post['msg'] : '';
+		$type = isset($post['type']) ? $post['type'] : 'text';
+		$arr = [];
+		$arr['type'] = $type;
+		$arr['source'] = 'user';
+		$arr['time'] = '12:51';
+		switch ($type) {
+			case 'text':
+				//文字
+				$content = $post['content'];
+				$arr['content'] = $content;
+				break;
+			case 'img':
+				//图片
+				$shortUrl = $post['shortUrl'];
+				$return = business::formatUploadImg($shortUrl);
+				$arr['url'] = $return['url'];
+				$arr['smallUrl'] = $return['smallUrl'];
+				break;
+			case 'link':
+				//常用链接
+				$id = $post['linkId'];
+				$respond = MerchantService::getCommonUrl($id);
+				$arr['name'] = $respond['name'];
+				$arr['img'] = $respond['img'];
+				$arr['url'] = $respond['url'];
+				break;
+			case 'goods':
+				//商品
+				$id = $post['goodsId'];
+				$info = GoodsService::getGoodsInfo($id);
+				GoodsService::valid($info, $this->merchantId);
+				$arr['goodsName'] = $info['goodsName'];
+				$arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : '';
+				$arr['price'] = isset($info['price']) ? $info['price'] : 300;
+				break;
+		}
+		$data[] = $arr;
+		
 		$id = 'merchantChat_' . $this->merchantId;
-		$data = json_encode(array(
-			'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) {
 			//直接发送
 			Gateway::sendToUid($id, json_encode($data));
 			util::success($data);
 		}
+		//有打开后台但没打开聊天框,通知有新消息
 		$consoleId = 'merchantConsole_' . $this->merchantId;
 		$consoleOnline = Gateway::getClientIdByUid($consoleId);
 		if ($consoleOnline) {
-			//通知打开的控制台有新消息
-			$data = json_encode(array(
-				'type' => 'msg',
-				'msg' => $msg,
-			));
+			$data = ['type' => 'newMessage', 'status' => 1];
 			Gateway::sendToUid($consoleId, json_encode($data));
 		}
-		
+		util::success($data);
+	}
+	
+	//关闭当前链接
+	public function actionClose()
+	{
+		$post = Yii::$app->request->post();
+		$clientId = isset($post['clientId']) ? $post['clientId'] : '';
+		Gateway::closeClient($clientId);
 	}
 	
 }

+ 57 - 16
app/client/views/chat/index.php

@@ -1,24 +1,65 @@
+<butto onclick="openMsg()">打开聊天</butto>
 <butto onclick="send()">发消息</butto>
+<butto onclick="closeMsg()">关闭</butto>
 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 <script>
-    ws = new WebSocket("ws://127.0.0.1:8282");
-    //服务端主动推送消息时会触发这里
-    ws.onmessage = function(e){
-        var data = eval("("+e.data+")");
-        var type = data.type || '';
-        switch(type){
-            case 'init':
-                //当前发起的聊天会员与当前登陆用户绑定关系
-                $.post('http://api.m.hhb.com/chat/bind', {clientId: data.client_id}, function(data){}, 'json');
-                break;
-            default :
-                console.log(e.data);
-        }
-    };
+    var clientId;
+    var ws;
+    function openMsg() {
+        ws = new WebSocket("ws://127.0.0.1:8282");
+        //服务端主动推送消息时会触发这里
+        ws.onmessage = function (e) {
+            var data = eval("(" + e.data + ")");
+            var type = data.type || '';
+            switch (type) {
+                case 'init':
+                    clientId = data.client_id;
+                    $.post('http://api.m.hhb.com/chat/bind-user', {clientId: clientId}, function (data) {
+
+                    }, 'json');
+                default :
+                    console.log(e.data);
+            }
+        };
+    }
 
-    function send()
+    function closeMsg()
     {
-        $.post('http://api.m.hhb.com/chat/send', {msg: '您好'}, function(data){}, 'json');
+        console.log(clientId);
+        $.post('http://api.m.hhb.com/chat/close', {clientId: clientId}, function (data) {
+
+        }, 'json');
+    }
+
+    function send() {
+
+        var arr = ['text', 'img', 'link', 'goods'];
+        var index = Math.floor((Math.random() * arr.length));
+        var type = arr[index];
+
+        //var type = 'text';
+        var data = {type: type}
+        switch (type) {
+            case 'text':
+                //发文字
+                data.content = '您好,有什么可以为你服务?';
+                break;
+            case 'img':
+                //发图片
+                data.shortUrl = '/uploads/201905/25/jpg/19052591518_12358.jpg';
+                break;
+            case 'link':
+                data.linkId = 1;
+                //发链接
+                break;
+            case 'goods':
+                data.goodsId = 160;
+                //发商品
+                break;
+            default:
+        }
+        $.post('http://api.m.hhb.com/chat/send', data, function (data) {
+        }, 'json');
     }
 
 </script>