request->get(); $userId = isset($get['userId']) ? $get['userId'] : 0; $user = UserService::getById($userId); UserService::valid($user, $this->sjId); return $this->renderPartial('index', ['userId' => $userId]); } //聊天双方的用户信息 ssh 2019.12.30 public function actionBegin() { $id = Yii::$app->request->get('id', 0); $user = UserService::getUserInfo($id); UserService::valid($user, $this->sjId); //新增最近联系人 ChatService::addRecentlyUser($this->sjId, $id); //获取最近联系人 $recentlyUser = ChatService::getRecentlyUserList($this->sjId); util::success(['merchant' => $this->sj->attributes, 'user' => $user, 'recentlyUser' => $recentlyUser]); } //绑定 ssh 2019.12.28 public function actionBindMerchant() { $post = Yii::$app->request->post(); $clientId = isset($post['clientId']) ? $post['clientId'] : ''; $id = 'merchantChat_' . $this->sjId; Gateway::bindUid($clientId, $id); //连接就将新消息置为已读 $consoleId = 'merchantConsole_' . $this->sjId; $consoleOnline = Gateway::getClientIdByUid($consoleId); if ($consoleOnline) { $data = ['type' => 'newMessage', 'status' => 0]; Gateway::sendToUid($consoleId, json_encode($data)); } } //关闭当前链接 public function actionClose() { $post = Yii::$app->request->post(); $clientId = isset($post['clientId']) ? $post['clientId'] : ''; Gateway::closeClient($clientId); } public function actionBindConsole() { $post = Yii::$app->request->post(); $clientId = isset($post['clientId']) ? $post['clientId'] : ''; $id = 'merchantConsole_' . $this->sjId; Gateway::bindUid($clientId, $id); } //发消息给客户 ssh 2019.12.28 public function actionSend() { $post = Yii::$app->request->post(); $type = isset($post['type']) ? $post['type'] : 'text'; $userId = isset($post['userId']) ? $post['userId'] : 0; $user = UserService::getById($userId); UserService::valid($user, $this->sjId); $arr = []; $arr['type'] = $type; $arr['source'] = 'merchant'; $arr['time'] = '12:51'; switch ($type) { case 'text': //文字 $content = $post['content']; $arr['content'] = $content; break; case 'img': //图片 $shortUrl = $post['shortUrl']; $return = business::formatUploadImg($shortUrl); $arr['url'] = $return['url']; $arr['smallUrl'] = $return['smallUrl']; break; case 'link': //常用链接 $id = $post['linkId']; $respond = MerchantService::getCommonUrl($id); $arr['name'] = $respond['name']; $arr['img'] = $respond['img']; $arr['url'] = $respond['url']; break; case 'goods': //商品 $id = $post['goodsId']; $info = GoodsClass::getGoodsInfo($id); GoodsService::valid($info, $this->mainId); $arr['name'] = $info['name']; $arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : ''; $arr['price'] = isset($info['price']) ? $info['price'] : 300; break; } $data[] = $arr; //放入缓存 ChatService::saveMessage($data, $userId); $online = Gateway::getClientIdByUid($userId); if ($online) { //直接发送 Gateway::sendToUid($userId, json_encode($data)); util::success($data); } util::success($data); } //最近联系人 ssh 2019.12.29 public function actionRecentlyUser() { $respond = ChatService::getRecentlyUserList($this->mainId); util::success($respond); } //最近聊天的内容 ssh 2019.12.29 public function actionRecentlyMessage() { //商品 图片 快捷链接 文字 $id = Yii::$app->request->get('id'); $info = UserService::getById($id); UserService::valid($info, $this->sjId); $respond = ChatService::getMessageList($id); util::success($respond); } // ---------------------------- 新聊天功能的接口 ---------------------------------- //最近聊天人 public function actionLatestUsers() { $respond = ChatService::getLatestUsers($this->shopId); util::success($respond); } //聊天的内容 public function actionChatHistory() { $roomName = Yii::$app->request->post('roomName'); // 校验 $roomeName 的值,防止越权访问他人数据 $roomName = 'room_messages:' . $roomName; $respond = \bizHd\message\services\ChatService::getChatHistory($roomName); util::success($respond); } //门店的未读消息总数 public function actionUnReadMsgCount() { $c = ChatService::getUnReadMsgCount($this->shopId, 'message'); util::success(['msg_count' => $c]); } // 给暂无价格商品报价 public function actionCreateQuotedPrice() { $post = Yii::$app->request->post(); $key = $post['key']; $price = round(floatval($post['price']), 2); $keyArr = explode('-', $key); if (count($keyArr) != 3) { util::error(-1, '校验失败,key格式错误'); } $shopId = intval($keyArr[0]); if ($this->shopId != $shopId) { util::error(-1, '校验失败,不是你的客户'); } $goodsId = intval($keyArr[2]); $goods = GoodsClass::getById($goodsId, false, 'id, mainId, priceType'); // if ($goods['priceType'] != 0) { // util::error(-1, '此商品无需走报价'); // } if ($goods['mainId'] != $this->mainId) { util::error(-1, '校验失败,不是你的商品'); } $userId = intval($keyArr[1]); //$currentKey = 'report_price:' . $shopId . '_' . $userId . '_' . $goodsId; // 无价格报价,多处相关,请搜索关键词 shopId_userId_goodsId $currentKey = ChatClass::getQuotedPriceKey($shopId, $userId, $goodsId); $val = Yii::$app->redis->executeCommand('GET', [$currentKey]); if (!empty($val)) { // 修改报价 Yii::info('修改报价:' . $currentKey . ' -- 旧价:' . $val . ', 新价:' . $price); } Yii::$app->redis->executeCommand('SETEX', [$currentKey, 86400, $price]); // 报价保留一天 util::success(['oldPice' => $val, 'newPrice' => $price]); } //用 redis key 获取商品报价 public function actionGetQuotedPrice() { $key = Yii::$app->request->post('key'); $keyArr = explode('-', $key); if (count($keyArr) != 3) { util::error(-1, '校验失败,key格式错误'); } $shopId = intval($keyArr[0]); if ($shopId != $this->shopId) { util::error(-1, '校验失败'); } $key = ChatClass::getQuotedPriceKey($keyArr[0], $keyArr[1], $keyArr[2]); $val = Yii::$app->redis->executeCommand('GET', [$key]); if (empty($val)) { util::success(['price' => -1]); } util::success(['price' => $val]); } // message_server 远程调用专用方法,可用于:1.@商家--客户发了商品消息 2.有未读消息的通知(可以额外添加触发条件)3.哦哦 public function actionMessageServerCall() { Yii::info('message_server 远程调用'); $callbackData = Yii::$app->request->post(); if (empty($callbackData)) { $postStr = file_get_contents('php://input'); // 处理转义字符,将JSON字符串正确解析为数组 //$postStr = stripslashes($postStr); $callbackData = json_decode($postStr, true); if (empty($callbackData)) { util::fail('回调请求的数据为空'); } } Yii::info(json_encode($callbackData)); return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']); // 成功状态的返回数据 } }