Browse Source

控制台通知

shish 6 years ago
parent
commit
2b75488de9

+ 17 - 6
app/business/controllers/ChatController.php

@@ -12,7 +12,7 @@ use GatewayClient\Gateway;
 
 class ChatController extends BaseController
 {
-	
+
 	//聊天 shish 2019.12.28
 	public function actionIndex()
 	{
@@ -22,16 +22,16 @@ class ChatController extends BaseController
 		UserService::valid($user, $this->merchantId);
 		return $this->renderPartial('index', ['userId' => $userId]);
 	}
-	
+
 	//绑定 shish 2019.12.28
 	public function actionBind()
 	{
 		$post = Yii::$app->request->post();
 		$clientId = isset($post['clientId']) ? $post['clientId'] : '';
-		$id = 'merchant_' . $this->merchantId;
+		$id = 'merchantChat_' . $this->merchantId;
 		Gateway::bindUid($clientId, $id);
 	}
-	
+
 	//发消息给客户 shish 2019.12.28
 	public function actionSend()
 	{
@@ -83,7 +83,7 @@ class ChatController extends BaseController
 			$time = date("i:s");
 			$data[] = ['type' => 'time', 'time' => $time];
 		}
-
+		
 		$online = Gateway::getClientIdByUid($userId);
 		if ($online) {
 			//直接发送
@@ -91,8 +91,19 @@ class ChatController extends BaseController
 		} else {
 			//保存
 		}
-
 		util::success($data);
 	}
+
+	//最近联系人 shish 2019.12.29
+	public function actionRecentlyUser()
+	{
 	
+	}
+
+	//最近聊天的内容 shish 2019.12.29
+	public function actionRecentlyChat()
+	{
+		$id = Yii::$app->request->get('id');
+	}
+
 }

+ 36 - 30
app/business/controllers/MainController.php

@@ -2,8 +2,6 @@
 
 namespace business\controllers;
 
-use common\components\stringUtil;
-use common\components\util;
 use Yii;
 use GatewayClient\Gateway;
 
@@ -14,39 +12,47 @@ class MainController extends BaseController
 	{
 		return $this->renderPartial('demo');
 	}
-
-	public function actionBind()
+	
+	
+	public function actionIndex()
 	{
-		//加载GatewayClient。安装GatewayClient参见本页面底部介绍
-		require_once '/your/path/GatewayClient/Gateway.php';
-		// GatewayClient 3.0.0版本开始要使用命名空间
-		
-		
-		// 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值
-		Gateway::$registerAddress = '127.0.0.1:1236';
-		
-		// 假设用户已经登录,用户uid和群组id在session中
-		$uid = $_SESSION['uid'];
-		$group_id = $_SESSION['group'];
-		// client_id与uid绑定
-		Gateway::bindUid($client_id, $uid);
-		// 加入某个群组(可调用多次加入多个群组)
-		Gateway::joinGroup($client_id, $group_id);
+		return $this->renderPartial('index');
 	}
-
-	public function actionSend()
+	
+	//新客户 shish 2019.12.29
+	public function actionVisit()
 	{
-		//加载GatewayClient。安装GatewayClient参见本页面底部介绍
-		require_once '/your/path/GatewayClient/Gateway.php';
-		// GatewayClient 3.0.0版本开始要使用命名空间
+		$data = json_encode(array(
+			'type' => 'msg',
+			'msg' => 'aaaa',
+		));
 		
-		// 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值
-		Gateway::$registerAddress = '127.0.0.1:1236';
+		$id = 'merchantConsole_' . $this->merchantId;
+		$online = Gateway::getClientIdByUid($id);
+		if ($online) {
+			//直接发送
+			Gateway::sendToUid($id, json_encode($data));
+		} else {
+			//保存
+		}
+	}
+	
+	//新订单 shish 2019.12.29
+	public function actionOrder()
+	{
+		$data = json_encode(array(
+			'type' => 'msg',
+			'msg' => 'aaaa',
+		));
 		
-		// 向任意uid的网站页面发送数据
-		Gateway::sendToUid($uid, $message);
-		// 向任意群组的网站页面发送数据
-		Gateway::sendToGroup($group, $message);
+		$id = 'merchantConsole_' . $this->merchantId;
+		$online = Gateway::getClientIdByUid($id);
+		if ($online) {
+			//直接发送
+			Gateway::sendToUid($id, json_encode($data));
+		} else {
+			//保存
+		}
 	}
 	
 }

+ 2 - 4
app/business/views/chat/index.php

@@ -3,7 +3,8 @@
 <butto onclick="send()">发消息</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");
         //服务端主动推送消息时会触发这里
@@ -13,9 +14,6 @@
             switch (type) {
                 case 'init':
                     clientId = data.client_id;
-                    $.post('http://api.b.hhb.com/chat/bind', {clientId: data.client_id}, function (data) {
-
-                    }, 'json');
                     console.log(clientId);
                 default :
             }

+ 56 - 0
app/business/views/main/index.php

@@ -0,0 +1,56 @@
+<butto onclick="openMsg()">打开聊天</butto>
+<butto onclick="open()">打开</butto>
+<butto onclick="send()">发消息</butto>
+<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
+<script>
+    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;
+                    console.log(clientId);
+                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>

+ 21 - 13
app/client/controllers/ChatController.php

@@ -2,25 +2,24 @@
 
 namespace client\controllers;
 
-use Yii;
-use yii\web\Controller;
 use common\components\util;
+use Yii;
 use GatewayClient\Gateway;
 
 class ChatController extends BaseController
 {
-
+	
 	public function beforeAction($action)
 	{
 		return parent::beforeAction($action);
 	}
-
+	
 	//聊天首页,前端使用
 	public function actionIndex()
 	{
 		return $this->renderPartial('index');
 	}
-
+	
 	//绑定 shish 2019.12.28
 	public function actionBind()
 	{
@@ -29,25 +28,34 @@ class ChatController extends BaseController
 		$userId = $this->userId;
 		Gateway::bindUid($clientId, $userId);
 	}
-
+	
 	//发消息给老板 shish 2019.12.28
 	public function actionSend()
 	{
 		$post = Yii::$app->request->post();
 		$msg = isset($post['msg']) ? $post['msg'] : '';
-		$id = 'merchant_' . $this->merchantId;
-		$m = json_encode(array(
+		$id = 'merchantChat_' . $this->merchantId;
+		$data = json_encode(array(
 			'type' => 'msg',
 			'msg' => $msg,
 		));
-		$online = Gateway::getClientIdByUid($id);
-		if ($online) {
+		$chatOnline = Gateway::getClientIdByUid($id);
+		if ($chatOnline) {
 			//直接发送
 			Gateway::sendToUid($id, json_encode($data));
-		} else {
-			//保存
+			util::success($data);
+		}
+		$consoleId = 'merchantConsole_' . $this->merchantId;
+		$consoleOnline = Gateway::getClientIdByUid($consoleId);
+		if ($consoleOnline) {
+			//通知打开的控制台有新消息
+			$data = json_encode(array(
+				'type' => 'msg',
+				'msg' => $msg,
+			));
+			Gateway::sendToUid($consoleId, json_encode($data));
 		}
-		Gateway::sendToUid($id, $m);
+		//保存起来
 	}
 
 }

+ 1 - 0
vendor/workerman/GatewayWorker/Applications/retail/Events.php

@@ -33,4 +33,5 @@ class Events
 	{
 
 	}
+	
 }