ChatController.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace client\controllers;
  3. use biz\MQ\services\ChatService;
  4. use common\components\util;
  5. use Yii;
  6. use GatewayClient\Gateway;
  7. class ChatController extends BaseController
  8. {
  9. public function beforeAction($action)
  10. {
  11. return parent::beforeAction($action);
  12. }
  13. //聊天首页,前端使用
  14. public function actionIndex()
  15. {
  16. return $this->renderPartial('index');
  17. }
  18. //绑定 shish 2019.12.28
  19. public function actionBind()
  20. {
  21. $post = Yii::$app->request->post();
  22. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  23. $userId = $this->userId;
  24. Gateway::bindUid($clientId, $userId);
  25. }
  26. //发消息给老板 shish 2019.12.28
  27. public function actionSend()
  28. {
  29. $post = Yii::$app->request->post();
  30. $msg = isset($post['msg']) ? $post['msg'] : '';
  31. $id = 'merchantChat_' . $this->merchantId;
  32. $data = json_encode(array(
  33. 'type' => 'msg',
  34. 'msg' => $msg,
  35. ));
  36. $merchantId = $this->merchantId;
  37. $userId = $this->userId;
  38. $userId = rand(111, 99999);
  39. ChatService::addUnReplyUser($merchantId,$userId);
  40. $r = ChatService::getUnReplyUserList($merchantId);
  41. dd($r);
  42. die;
  43. $chatOnline = Gateway::getClientIdByUid($id);
  44. if ($chatOnline) {
  45. //直接发送
  46. Gateway::sendToUid($id, json_encode($data));
  47. util::success($data);
  48. }
  49. $consoleId = 'merchantConsole_' . $this->merchantId;
  50. $consoleOnline = Gateway::getClientIdByUid($consoleId);
  51. if ($consoleOnline) {
  52. //通知打开的控制台有新消息
  53. $data = json_encode(array(
  54. 'type' => 'msg',
  55. 'msg' => $msg,
  56. ));
  57. Gateway::sendToUid($consoleId, json_encode($data));
  58. }
  59. }
  60. }