ChatController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace mall\controllers;
  3. use bizMall\goods\services\GoodsService;
  4. use bizMall\merchant\services\MerchantService;
  5. use bizMall\message\services\ChatService;
  6. use common\components\business;
  7. use common\components\util;
  8. use Yii;
  9. use GatewayClient\Gateway;
  10. class ChatController extends BaseController
  11. {
  12. public $guestAccess = ['message-server-call'];
  13. //聊天首页,前端使用
  14. public function actionIndex()
  15. {
  16. return $this->renderPartial('index');
  17. }
  18. //绑定 ssh 2019.12.28
  19. public function actionBindUser()
  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. //聊天双方的用户信息 ssh 2019.12.30
  27. public function actionBegin()
  28. {
  29. //新增最近联系人
  30. ChatService::addRecentlyUser($this->sjId, $this->userId);
  31. util::success(['merchant' => $this->sj->attributes, 'user' => $this->user]);
  32. }
  33. //发消息给老板 ssh 2019.12.28
  34. public function actionSend()
  35. {
  36. $post = Yii::$app->request->post();
  37. $type = isset($post['type']) ? $post['type'] : 'text';
  38. $arr = [];
  39. $arr['type'] = $type;
  40. $arr['source'] = 'user';
  41. $arr['time'] = '12:51';
  42. switch ($type) {
  43. case 'text':
  44. //文字
  45. $content = $post['content'];
  46. $arr['content'] = $content;
  47. break;
  48. case 'img':
  49. //图片
  50. $shortUrl = $post['shortUrl'];
  51. $return = business::formatUploadImg($shortUrl);
  52. $arr['url'] = $return['url'];
  53. $arr['smallUrl'] = $return['smallUrl'];
  54. break;
  55. case 'link':
  56. //常用链接
  57. $id = $post['linkId'];
  58. $respond = MerchantService::getCommonUrl($id);
  59. $arr['name'] = $respond['name'];
  60. $arr['img'] = $respond['img'];
  61. $arr['url'] = $respond['url'];
  62. break;
  63. case 'goods':
  64. //商品
  65. $id = $post['goodsId'];
  66. $info = GoodsService::getGoodsInfo($id);
  67. GoodsService::valid($info, $this->mainId);
  68. $arr['name'] = $info['name'];
  69. $arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : '';
  70. $arr['price'] = isset($info['price']) ? $info['price'] : 300;
  71. break;
  72. }
  73. $data[] = $arr;
  74. $id = 'merchantChat_' . $this->sjId;
  75. $chatOnline = Gateway::getClientIdByUid($id);
  76. if ($chatOnline) {
  77. //直接发送
  78. Gateway::sendToUid($id, json_encode($data));
  79. util::success($data);
  80. }
  81. //有打开后台但没打开聊天框,通知有新消息
  82. $consoleId = 'merchantConsole_' . $this->sjId;
  83. $consoleOnline = Gateway::getClientIdByUid($consoleId);
  84. if ($consoleOnline) {
  85. $data = ['type' => 'newMessage', 'status' => 1];
  86. Gateway::sendToUid($consoleId, json_encode($data));
  87. }
  88. util::success($data);
  89. }
  90. //关闭当前链接
  91. public function actionClose()
  92. {
  93. $post = Yii::$app->request->post();
  94. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  95. Gateway::closeClient($clientId);
  96. }
  97. //最近聊天的内容 ssh 2019.12.29
  98. public function actionRecentlyMessage()
  99. {
  100. $respond = ChatService::getMessageList($this->userId);
  101. util::success($respond);
  102. }
  103. // ---------------------------- 新聊天功能的接口 ----------------------------------
  104. //最近聊天人
  105. public function actionLatestUsers()
  106. {
  107. $respond = ChatService::getLatestUsers(intval($this->userId));
  108. util::success($respond);
  109. }
  110. //聊天的内容
  111. public function actionChatHistory()
  112. {
  113. $roomName = Yii::$app->request->post('roomName');
  114. // 校验 $roomeName 的值,防止越权访问他人数据
  115. $roomName = 'room_messages:' . $roomName;
  116. $respond = ChatService::getChatHistory($roomName);
  117. util::success($respond);
  118. }
  119. //客户的未读消息总数
  120. public function actionUnReadMsgCount()
  121. {
  122. $c = ChatService::getUnReadMsgCount(intval($this->userId), 'message');
  123. util::success(['msg_count'=>$c]);
  124. }
  125. //用 redis key 获取商品报价
  126. public function actionGetQuotedPrice()
  127. {
  128. $key = Yii::$app->request->post('key');
  129. $keyArr = explode('-', $key);
  130. $userId = intval($keyArr[1]);
  131. if ($userId != $this->userId) {
  132. util::error(-1, '校验失败');
  133. }
  134. $val = Yii::$app->redis->executeCommand('GET', [$key]);
  135. if (empty($val)) {
  136. util::success(['price'=>-1]);
  137. }
  138. util::success(['price'=>$val]);
  139. }
  140. // message_server 远程调用专用方法,可用于:1.有未读消息的通知(可以额外添加触发条件)2.
  141. public function actionMessageServerCall()
  142. {
  143. Yii::info('message_server 远程调用');
  144. $callbackData = Yii::$app->request->post();
  145. if (empty($callbackData)) {
  146. $postStr = file_get_contents('php://input');
  147. // 处理转义字符,将JSON字符串正确解析为数组
  148. //$postStr = stripslashes($postStr);
  149. $callbackData = json_decode($postStr, true);
  150. if (empty($callbackData)) {
  151. util::fail('回调请求的数据为空');
  152. }
  153. }
  154. Yii::info(json_encode($callbackData));
  155. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']); // 成功状态的返回数据
  156. }
  157. }