소스 검색

聊天系统打通

shish 6 년 전
부모
커밋
00115bdffc

+ 47 - 0
app/business/controllers/ChatController.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace business\controllers;
+
+use biz\user\services\UserService;
+use Yii;
+use common\components\util;
+use GatewayClient\Gateway;
+
+class ChatController extends BaseController
+{
+	
+	//聊天 shish 2019.12.28
+	public function actionIndex()
+	{
+		$get = Yii::$app->request->get();
+		$userId = isset($get['userId']) ? $get['userId'] : 0;
+		$user = UserService::getById($userId);
+		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;
+		Gateway::bindUid($clientId, $id);
+	}
+	
+	//发消息给客户 shish 2019.12.28
+	public function actionSend()
+	{
+		$post = Yii::$app->request->post();
+		$msg = isset($post['msg']) ? $post['msg'] : '';
+		$userId = isset($post['userId']) ? $post['userId'] : 0;
+		$user = UserService::getById($userId);
+		UserService::valid($user, $this->merchantId);
+		$m = json_encode(array(
+			'type' => 'msg',
+			'msg' => $msg,
+		));
+		Gateway::sendToUid($userId, $m);
+	}
+	
+}

+ 24 - 0
app/business/views/chat/index.php

@@ -0,0 +1,24 @@
+<butto onclick="send()">发消息</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.b.hhb.com/chat/bind', {clientId: data.client_id}, function(data){}, 'json');
+                break;
+            default :
+                console.log(e.data);
+        }
+    };
+
+    function send()
+    {
+        $.post('http://api.b.hhb.com/chat/send', {msg: '您好',userId:<?= $userId ?>}, function(data){}, 'json');
+    }
+
+</script>

+ 5 - 5
app/client/controllers/ChatController.php

@@ -29,18 +29,18 @@ class ChatController extends BaseController
 		$userId = $this->userId;
 		Gateway::bindUid($clientId, $userId);
 	}
-
-	//发消息 shish 2019.12.28
+	
+	//发消息给老板 shish 2019.12.28
 	public function actionSend()
 	{
 		$post = Yii::$app->request->post();
 		$msg = isset($post['msg']) ? $post['msg'] : '';
-		$userId = $this->userId;
+		$id = 'merchant_' . $this->merchantId;
 		$m = json_encode(array(
 			'type' => 'msg',
 			'msg' => $msg,
 		));
-		Gateway::sendToUid($userId, $m);
+		Gateway::sendToUid($id, $m);
 	}
-	
+
 }

+ 4 - 8
app/client/views/chat/index.php

@@ -1,4 +1,4 @@
-<butto onclick="get()">请求</butto>
+<butto onclick="send()">发消息</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");
@@ -9,20 +9,16 @@
         switch(type){
             case 'init':
                 //当前发起的聊天会员与当前登陆用户绑定关系
-                $.post('http://api.m.huaml.com/chat/bind', {clientId: data.client_id}, function(data){}, 'json');
+                $.post('http://api.m.hhb.com/chat/bind', {clientId: data.client_id}, function(data){}, 'json');
                 break;
             default :
                 console.log(e.data);
         }
     };
 
-    function get()
+    function send()
     {
-        var text = 'nihao';
-        var fromId = 0;
-        var toId = 0;
-        var message = '{"msg":"'+text+'","type":"say","fromid":"'+fromId+'","toid":"'+toId+'"}';
-        ws.send(message);
+        $.post('http://api.m.hhb.com/chat/send', {msg: '您好'}, function(data){}, 'json');
     }
 
 </script>

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

@@ -25,7 +25,7 @@ class Events
 	// GatewayWorker建议不做任何业务逻辑,onMessage留空即可
 	public static function onMessage($client_id, $message)
 	{
-	
+
 	}
 
 	//当用户断开连接时触发