ChatController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 = floatval($post['price']);
  164. $keyArr = explode('-', $key);
  165. $shopId = intval($keyArr[0]);
  166. if ($this->shopId != $shopId) {
  167. util::error(-1, '校验失败,并不是你的客户');
  168. }
  169. $goodsId = $keyArr[2];
  170. $goods = GoodsClass::getById($goodsId, false, 'id, priceType');
  171. if ($goods['priceType'] != 0) {
  172. util::error(-1, '此商品无需走报价');
  173. }
  174. $userId = $keyArr[1] ?? 0;
  175. //$currentKey = 'report_price:' . $shopId . '_' . $userId . '_' . $goodsId; // 无价格报价,多处相关,请搜索关键词 shopId_userId_goodsId
  176. $currentKey = ChatClass::getQuotedPriceKey($shopId, $userId, $goodsId);
  177. $val = Yii::$app->redis->executeCommand('GET', [$currentKey]);
  178. if (!empty($saveAuthCode)) { // 修改报价
  179. Yii::info('修改报价:' . $currentKey . ' -- 旧价:' . $val . ', 新价:' . $price);
  180. }
  181. Yii::$app->redis->executeCommand('SETEX', [$currentKey, 86400, $price]); // 报价保留一天
  182. util::success(['oldPice' => $val, 'newPrice' => $price]);
  183. }
  184. //用 redis key 获取商品报价
  185. public function actionGetQuotedPrice()
  186. {
  187. $key = Yii::$app->request->post('key');
  188. $keyArr = explode('-', $key);
  189. $shopId = intval($keyArr[0]);
  190. if ($shopId != $this->shopId) {
  191. util::error(-1, '校验失败');
  192. }
  193. $val = Yii::$app->redis->executeCommand('GET', [$key]);
  194. if (empty($val)) {
  195. util::success(['price' => -1]);
  196. }
  197. util::success(['price' => $val]);
  198. }
  199. // message_server 远程调用专用方法,可用于:1.@商家--客户发了商品消息 2.有未读消息的通知(可以额外添加触发条件)3.哦哦
  200. public function actionMessageServerCall()
  201. {
  202. Yii::info('message_server 远程调用');
  203. $callbackData = Yii::$app->request->post();
  204. if (empty($callbackData)) {
  205. $postStr = file_get_contents('php://input');
  206. // 处理转义字符,将JSON字符串正确解析为数组
  207. //$postStr = stripslashes($postStr);
  208. $callbackData = json_decode($postStr, true);
  209. if (empty($callbackData)) {
  210. util::fail('回调请求的数据为空');
  211. }
  212. }
  213. Yii::info(json_encode($callbackData));
  214. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']); // 成功状态的返回数据
  215. }
  216. }