ChatController.php 5.7 KB

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