| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- namespace mall\controllers;
- use biz\wx\classes\WxMessageClass;
- use bizMall\custom\classes\CustomClass;
- use bizMall\goods\services\GoodsService;
- use bizMall\merchant\classes\ShopClass;
- use bizMall\merchant\services\MerchantService;
- use bizMall\message\services\ChatService;
- use common\components\business;
- use common\components\noticeUtil;
- use common\components\util;
- use Yii;
- use GatewayClient\Gateway;
- class ChatController extends BaseController
- {
- public $guestAccess = ['message-server-call'];
- //聊天首页,前端使用
- public function actionIndex()
- {
- return $this->renderPartial('index');
- }
- //绑定 ssh 2019.12.28
- public function actionBindUser()
- {
- $post = Yii::$app->request->post();
- $clientId = isset($post['clientId']) ? $post['clientId'] : '';
- $userId = $this->userId;
- Gateway::bindUid($clientId, $userId);
- }
- //聊天双方的用户信息 ssh 2019.12.30
- public function actionBegin()
- {
- //新增最近联系人
- ChatService::addRecentlyUser($this->sjId, $this->userId);
- util::success(['merchant' => $this->sj->attributes, 'user' => $this->user]);
- }
- //发消息给老板 ssh 2019.12.28
- public function actionSend()
- {
- $post = Yii::$app->request->post();
- $type = isset($post['type']) ? $post['type'] : 'text';
- $arr = [];
- $arr['type'] = $type;
- $arr['source'] = 'user';
- $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 = GoodsService::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;
- $id = 'merchantChat_' . $this->sjId;
- $chatOnline = Gateway::getClientIdByUid($id);
- if ($chatOnline) {
- //直接发送
- Gateway::sendToUid($id, json_encode($data));
- util::success($data);
- }
- //有打开后台但没打开聊天框,通知有新消息
- $consoleId = 'merchantConsole_' . $this->sjId;
- $consoleOnline = Gateway::getClientIdByUid($consoleId);
- if ($consoleOnline) {
- $data = ['type' => 'newMessage', 'status' => 1];
- Gateway::sendToUid($consoleId, json_encode($data));
- }
- util::success($data);
- }
- //关闭当前链接
- public function actionClose()
- {
- $post = Yii::$app->request->post();
- $clientId = isset($post['clientId']) ? $post['clientId'] : '';
- Gateway::closeClient($clientId);
- }
- //最近聊天的内容 ssh 2019.12.29
- public function actionRecentlyMessage()
- {
- $respond = ChatService::getMessageList($this->userId);
- util::success($respond);
- }
- // ---------------------------- 新聊天功能的接口 ----------------------------------
- //最近聊天人
- public function actionLatestUsers()
- {
- $respond = ChatService::getLatestUsers(intval($this->userId));
- util::success($respond);
- }
- //聊天的内容
- public function actionChatHistory()
- {
- $roomName = Yii::$app->request->post('roomName');
- // 校验 $roomeName 的值,防止越权访问他人数据
- $roomName = 'room_messages:' . $roomName;
- $respond = ChatService::getChatHistory($roomName);
- util::success($respond);
- }
- //客户的未读消息总数
- public function actionUnReadMsgCount()
- {
- $c = ChatService::getUnReadMsgCount(intval($this->userId), 'message');
- util::success(['msg_count' => $c]);
- }
- //用 redis key 获取商品报价
- public function actionGetQuotedPrice()
- {
- $key = Yii::$app->request->post('key');
- $keyArr = explode('-', $key);
- $userId = intval($keyArr[1]);
- if ($userId != $this->userId) {
- util::error(-1, '校验失败');
- }
- $val = Yii::$app->redis->executeCommand('GET', [$key]);
- if (empty($val)) {
- util::success(['price' => -1]);
- }
- util::success(['price' => $val]);
- }
- // message_server 远程调用专用方法,可用于:1.有未读消息的通知(可以额外添加触发条件)2.
- 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('回调请求的数据为空');
- }
- }
- $shopId = $callbackData['shopId'] ?? 0;
- $userId = $callbackData['userId'] ?? '';
- $key = 'has_remind_' . $shopId . '_' . $userId;
- if (!empty($shopId) && !empty($userId)) {
- //5秒不重复通知
- $hasRemind = Yii::$app->redis->executeCommand('GET', [$key]);
- if (empty($hasRemind)) {
- $shop = ShopClass::getById($shopId, true);
- if (!empty($shop)) {
- WxMessageClass::newMessageInform($shop, "商城上客户有留言");
- Yii::$app->redis->executeCommand('SETEX', [$key, 5, 'has']);
- noticeUtil::push("客户留言已通知花店," . $shopId . ' userId:' . $userId, '15280215347');
- }
- }
- }
- Yii::info(json_encode($callbackData));
- return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']); // 成功状态的返回数据
- }
- }
|