123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\webscoket\handler;
- use app\services\kefu\LoginServices;
- use app\services\message\service\StoreServiceRecordServices;
- use app\services\message\service\StoreServiceServices;
- use app\services\user\UserServices;
- use app\webscoket\BaseHandler;
- use app\webscoket\Manager;
- use app\webscoket\Response;
- use crmeb\exceptions\AuthException;
- /**
- * Class KefuHandler
- * @package app\webscoket\handler
- */
- class KefuHandler extends BaseHandler
- {
- /**
- * 客服登录
- * @param array $data
- * @param Response $response
- * @return bool|mixed|\think\response\Json|null
- * @throws \Psr\SimpleCache\InvalidArgumentException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function login(array $data, Response $response)
- {
- if (!isset($data['token']) || !$token = $data['token']) {
- return $response->fail('授权失败!');
- }
- try {
- /** @var LoginServices $services */
- $services = app()->make(LoginServices::class);
- $kefuInfo = $services->parseToken($token);
- } catch (AuthException $e) {
- return $response->fail($e->getMessage());
- }
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $user = $userService->get($kefuInfo['uid'], ['uid', 'nickname']);
- if (!isset($user['uid'])) {
- return $response->fail('您登录的客服用户不存在');
- }
- /** @var StoreServiceRecordServices $service */
- $service = app()->make(StoreServiceRecordServices::class);
- $service->updateRecord(['to_uid' => $user['uid']], ['online' => 1]);
- /** @var StoreServiceServices $service */
- $service = app()->make(StoreServiceServices::class);
- $service->update(['uid' => $user['uid']], ['online' => 1]);
- return $response->success($user->toArray());
- }
- /**
- * 兼容前端方法
- * @param array $data
- * @param Response $response
- */
- public function kefu_login(array $data = [], Response $response)
- {
- return $this->login($data, $response);
- }
- /**
- * 上下线
- * @param array $data
- * @param Response $response
- */
- public function online(array $data = [], Response $response)
- {
- $online = $data['online'] ?? 0;
- $user = $this->room->get($this->fd);
- if ($user) {
- /** @var StoreServiceServices $service */
- $service = app()->make(StoreServiceServices::class);
- $service->update(['uid' => $user['uid']], ['online' => $online]);
- if ($user['to_uid']) {
- $fd = $this->manager->userFd('', $user['to_uid']);
- //给当前正在聊天的用户发送上下线消息
- $this->manager->pushing($fd, $response->message('online', [
- 'online' => $online,
- 'uid' => $user['uid']
- ])->getData());
- }
- }
- }
- /**
- * 客服转接
- * @param array $data
- * @param Response $response
- * @return \think\response\Json
- */
- public function transfer(array $data, Response $response)
- {
- $data = $data['data'] ?? [];
- $uid = $data['recored']['uid'] ?? 0;
- if ($uid && $this->manager->userFd('', $uid)) {
- $data['recored']['online'] = 1;
- } else {
- $data['recored']['online'] = 0;
- }
- return $response->message('transfer', $data);
- }
- /**
- * 退出登录
- * @param array $data
- * @param Response $response
- */
- public function logout(array $data = [], Response $response)
- {
- $user = $this->room->get($this->fd);
- $uid = $user['uid'] ?? 0;
- if ($uid) {
- /** @var StoreServiceServices $service */
- $service = app()->make(StoreServiceServices::class);
- $service->update(['uid' => $user['uid']], ['online' => 0]);
- /** @var StoreServiceRecordServices $service */
- $service = app()->make(StoreServiceRecordServices::class);
- $service->updateRecord(['to_uid' => $uid], ['online' => 0]);
- $this->manager->pushing($this->manager->userFd(Manager::KEFU_TYPE_NUM), $response->message('online', [
- 'online' => 0,
- 'uid' => $uid
- ]), $this->fd);
- }
- }
- }
|