| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace hd\controllers;
- use bizHd\goods\services\GoodsService;
- use biz\sj\services\MerchantService;
- use bizHd\message\services\ChatService;
- use bizHd\user\services\UserService;
- use common\components\business;
- use Yii;
- use common\components\util;
- use GatewayClient\Gateway;
- class ChatController extends BaseController
- {
-
- //聊天 shish 2019.12.28
- public function actionIndex()
- {
- $get = Yii::$app->request->get();
- $userId = isset($get['userId']) ? $get['userId'] : 0;
- $user = UserService::getById($userId);
- UserService::valid($user, $this->sjId);
- return $this->renderPartial('index', ['userId' => $userId]);
- }
- //聊天双方的用户信息 shish 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, 'user' => $user, 'recentlyUser' => $recentlyUser]);
- }
-
- //绑定 shish 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);
- }
-
- //发消息给客户 shish 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 = GoodsService::getGoodsInfo($id);
- GoodsService::valid($info, $this->sjId);
- $arr['goodsName'] = $info['goodsName'];
- $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);
- }
-
- //最近联系人 shish 2019.12.29
- public function actionRecentlyUser()
- {
- $respond = ChatService::getRecentlyUserList($this->sjId);
- util::success($respond);
- }
-
- //最近聊天的内容 shish 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);
- }
-
- }
|