ChatController.php 6.6 KB

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