ChatController.php 8.7 KB

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