ChatController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\goods\classes\GoodsClass;
  4. use bizHd\goods\services\GoodsService;
  5. use biz\sj\services\MerchantService;
  6. use bizHd\message\services\ChatService;
  7. use bizHd\user\services\UserService;
  8. use common\components\business;
  9. use Yii;
  10. use common\components\util;
  11. use GatewayClient\Gateway;
  12. class ChatController extends BaseController
  13. {
  14. public $guestAccess = ['message-server-call'];
  15. //聊天 ssh 2019.12.28
  16. public function actionIndex()
  17. {
  18. $get = Yii::$app->request->get();
  19. $userId = isset($get['userId']) ? $get['userId'] : 0;
  20. $user = UserService::getById($userId);
  21. UserService::valid($user, $this->sjId);
  22. return $this->renderPartial('index', ['userId' => $userId]);
  23. }
  24. //聊天双方的用户信息 ssh 2019.12.30
  25. public function actionBegin()
  26. {
  27. $id = Yii::$app->request->get('id', 0);
  28. $user = UserService::getUserInfo($id);
  29. UserService::valid($user, $this->sjId);
  30. //新增最近联系人
  31. ChatService::addRecentlyUser($this->sjId, $id);
  32. //获取最近联系人
  33. $recentlyUser = ChatService::getRecentlyUserList($this->sjId);
  34. util::success(['merchant' => $this->sj->attributes, 'user' => $user, 'recentlyUser' => $recentlyUser]);
  35. }
  36. //绑定 ssh 2019.12.28
  37. public function actionBindMerchant()
  38. {
  39. $post = Yii::$app->request->post();
  40. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  41. $id = 'merchantChat_' . $this->sjId;
  42. Gateway::bindUid($clientId, $id);
  43. //连接就将新消息置为已读
  44. $consoleId = 'merchantConsole_' . $this->sjId;
  45. $consoleOnline = Gateway::getClientIdByUid($consoleId);
  46. if ($consoleOnline) {
  47. $data = ['type' => 'newMessage', 'status' => 0];
  48. Gateway::sendToUid($consoleId, json_encode($data));
  49. }
  50. }
  51. //关闭当前链接
  52. public function actionClose()
  53. {
  54. $post = Yii::$app->request->post();
  55. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  56. Gateway::closeClient($clientId);
  57. }
  58. public function actionBindConsole()
  59. {
  60. $post = Yii::$app->request->post();
  61. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  62. $id = 'merchantConsole_' . $this->sjId;
  63. Gateway::bindUid($clientId, $id);
  64. }
  65. //发消息给客户 ssh 2019.12.28
  66. public function actionSend()
  67. {
  68. $post = Yii::$app->request->post();
  69. $type = isset($post['type']) ? $post['type'] : 'text';
  70. $userId = isset($post['userId']) ? $post['userId'] : 0;
  71. $user = UserService::getById($userId);
  72. UserService::valid($user, $this->sjId);
  73. $arr = [];
  74. $arr['type'] = $type;
  75. $arr['source'] = 'merchant';
  76. $arr['time'] = '12:51';
  77. switch ($type) {
  78. case 'text':
  79. //文字
  80. $content = $post['content'];
  81. $arr['content'] = $content;
  82. break;
  83. case 'img':
  84. //图片
  85. $shortUrl = $post['shortUrl'];
  86. $return = business::formatUploadImg($shortUrl);
  87. $arr['url'] = $return['url'];
  88. $arr['smallUrl'] = $return['smallUrl'];
  89. break;
  90. case 'link':
  91. //常用链接
  92. $id = $post['linkId'];
  93. $respond = MerchantService::getCommonUrl($id);
  94. $arr['name'] = $respond['name'];
  95. $arr['img'] = $respond['img'];
  96. $arr['url'] = $respond['url'];
  97. break;
  98. case 'goods':
  99. //商品
  100. $id = $post['goodsId'];
  101. $info = GoodsClass::getGoodsInfo($id);
  102. GoodsService::valid($info, $this->mainId);
  103. $arr['name'] = $info['name'];
  104. $arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : '';
  105. $arr['price'] = isset($info['price']) ? $info['price'] : 300;
  106. break;
  107. }
  108. $data[] = $arr;
  109. //放入缓存
  110. ChatService::saveMessage($data, $userId);
  111. $online = Gateway::getClientIdByUid($userId);
  112. if ($online) {
  113. //直接发送
  114. Gateway::sendToUid($userId, json_encode($data));
  115. util::success($data);
  116. }
  117. util::success($data);
  118. }
  119. //最近联系人 ssh 2019.12.29
  120. public function actionRecentlyUser()
  121. {
  122. $respond = ChatService::getRecentlyUserList($this->mainId);
  123. util::success($respond);
  124. }
  125. //最近聊天的内容 ssh 2019.12.29
  126. public function actionRecentlyMessage()
  127. {
  128. //商品 图片 快捷链接 文字
  129. $id = Yii::$app->request->get('id');
  130. $info = UserService::getById($id);
  131. UserService::valid($info, $this->sjId);
  132. $respond = ChatService::getMessageList($id);
  133. util::success($respond);
  134. }
  135. // ---------------------------- 新聊天功能的接口 ----------------------------------
  136. //最近聊天人
  137. public function actionLatestUsers()
  138. {
  139. $respond = ChatService::getLatestUsers($this->shopId);
  140. util::success($respond);
  141. }
  142. //聊天的内容
  143. public function actionChatHistory()
  144. {
  145. $roomName = Yii::$app->request->post('roomName');
  146. // 校验 $roomeName 的值,防止越权访问他人数据
  147. $roomName = 'room_messages:' . $roomName;
  148. $respond = \bizHd\message\services\ChatService::getChatHistory($roomName);
  149. util::success($respond);
  150. }
  151. //门店的未读消息总数
  152. public function actionUnReadMsgCount()
  153. {
  154. $c = ChatService::getUnReadMsgCount($this->shopId, 'message');
  155. util::success(['msg_count' => $c]);
  156. }
  157. // 给暂无价格商品报价
  158. public function actionCreateQuotedPrice()
  159. {
  160. $post = Yii::$app->request->post();
  161. $key = $post['key'];
  162. $price = floatval($post['price']);
  163. $keyArr = explode('-', $key);
  164. $shopId = intval($keyArr[0]);
  165. if ($this->shopId != $shopId) {
  166. util::error(-1, '校验失败,并不是你的客户');
  167. }
  168. $goodsId = $keyArr[2];
  169. $goods = GoodsClass::getById($goodsId, false, 'id, priceType');
  170. if ($goods['priceType'] != 0) {
  171. util::error(-1, '此商品无需走报价');
  172. }
  173. $userId = $keyArr[1] ?? 0;
  174. // 无价格报价,多处相关,请搜索关键词 shopId_userId_goodsId
  175. $currentKey = 'report_price:' . $shopId . '_' . $userId . '_' . $goodsId;
  176. $val = Yii::$app->redis->executeCommand('GET', [$currentKey]);
  177. if (!empty($saveAuthCode)) { // 修改报价
  178. Yii::info('修改报价:' . $currentKey . ' -- 旧价:' . $val . ', 新价:' . $price);
  179. }
  180. Yii::$app->redis->executeCommand('SETEX', [$currentKey, 864000, $price]);
  181. util::success(['oldPice' => $val, 'newPrice' => $price]);
  182. }
  183. //用 redis key 获取商品报价
  184. public function actionGetQuotedPrice()
  185. {
  186. $key = Yii::$app->request->post('key');
  187. $keyArr = explode('-', $key);
  188. $shopId = intval($keyArr[0]);
  189. if ($shopId != $this->shopId) {
  190. util::error(-1, '校验失败');
  191. }
  192. $val = Yii::$app->redis->executeCommand('GET', [$key]);
  193. if (empty($val)) {
  194. util::success(['price' => -1]);
  195. }
  196. util::success(['price' => $val]);
  197. }
  198. // message_server 远程调用专用方法,可用于:1.@商家--客户发了商品消息 2.有未读消息的通知(可以额外添加触发条件)3.哦哦
  199. public function actionMessageServerCall()
  200. {
  201. Yii::info('message_server 远程调用');
  202. $callbackData = Yii::$app->request->post();
  203. if (empty($callbackData)) {
  204. $postStr = file_get_contents('php://input');
  205. // 处理转义字符,将JSON字符串正确解析为数组
  206. //$postStr = stripslashes($postStr);
  207. $callbackData = json_decode($postStr, true);
  208. if (empty($callbackData)) {
  209. util::fail('回调请求的数据为空');
  210. }
  211. }
  212. Yii::info(json_encode($callbackData));
  213. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']); // 成功状态的返回数据
  214. }
  215. }