ChatController.php 6.5 KB

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