涉及大量代码修改,请备份相关被修改文件
逻辑如下
本身客服系统自带游客功能,对代码进行了适当的修改,让游客信息能顺利入库
不可忽视的问题
找大佬继续优化
- 游客uid用的时间戳,如果你的已注册会员数量达到10位数,会与已注册会员冲突,而且当并发比较高的时候,比如恰好1秒内有2个游客同时咨询,也会冲突
- 为了让游客获取到聊天记录,取消了聊天记录的验证(需要大佬解决),会泄露
- 没有对游客聊天记录定时清理
php代码修改
app/controller/api/v2/user/StoreService.php
修改从第46行开始,将
function record后面的{}里面的内容修改为
        [$uidTo, $limit, $toUid, $touristUid] = $request->getMore([
            [['uidTo', 'd'], 0],
            [['limit', 'd'], 10],
            [['toUid', 'd'], 0],
            [['touristUid', 'd'], 0],
        ], true);
        $uid = $request->uid();
        if (!$uid) {
            $uid = $touristUid;
        }
        return app('json')->successful($services->getRecord($uid, $uidTo, $limit, $toUid));
app/websocket/Manager.php
跳到第85行,将
function onOpen里面的代码修改为
        $fd = $this->websocket->getSender();
        $type = $request->get('type');
        $token = $request->get('token', '');
        $touristUid = $request->get('tourist_uid', '');
        $tourist = !!$touristUid;
        /*if (!$token || !in_array($type, self::USER_TYPE)) {
            return $this->websocket->close();
        }*/
        if ($token === 'undefined' || $token === 'false') {
            $token = '';
        }
        if ($token === '') {
            $tourist = true;
            if (empty($touristUid)) {
                $touristUid = time();
            }
        }
        // 只有用户模式下才能使用游客模式
        if ($type !== self::USER_TYPE[1] && $tourist) {
            return $this->websocket->close();
        }
        $types = self::USER_TYPE;
        $this->nowRoom->type(array_flip($types)[$type]);
        try {
            $data = $this->exec($type, 'login', [$fd, $request->get('form_type', null), ['token' => $token, 'tourist' => $tourist], $this->response])->getData();
        } catch (\Throwable $e) {
            return $this->websocket->close();
        }
        if ($tourist) {
            $data['status'] = 200;
            $data['data']['uid'] = $touristUid;
        }
        if ($data['status'] != 200 || !($data['data']['uid'] ?? null)) {
            return $this->websocket->close();
        }
        $this->resetPingTimeout($this->pingInterval + $this->pingTimeout);
        $uid = $data['data']['uid'];
        $type = array_search($type, self::USER_TYPE);
        $this->login($type, $uid, $fd);
        $this->nowRoom->add((string)$fd, $uid, 0, $tourist ? 1 : 0);
        $this->send($fd, $this->response->message('ping', ['now' => time()]));
        return $this->send($fd, $this->response->success($data['data']));
app/websocket/BaseHandler.php
找到第61行,将
abstract public function login(array $data = [], Response $response);修改为
abstract public function login($data, Response $response);
找到第126行,将
$data['is_tourist'] = $data['is_tourist'] ?? 0;修改为
$data['is_tourist'] = $isTourist ? 1 : 0;
app/websocket/handler/目录下的四个文件,依次修改
找到
public function login(array $data = [], Response $response)修改为
public function login($data, Response $response)
并且在
{后面添加以下代码
        if (!is_array($data)) {
            $data = [];
        }
route/api.php
搜索
v2.user.StoreService/record,将整行代码移动到下方授权不通过,不会抛出异常继续执行的{}里面
前端Vue代码修改
所有代码目录以
view/uniapp为根目录
api/user.js
找到
function getChatRecord,将{}的代码修改为
	return request.get("v2/user/service/record", data, {
		noAuth: true
	});
libs/new_chat.js
找到第82行到101行,将代码修改为
	onStart: function(token, form_type, tourist_uid) {
		let wssUrl = `${VUE_APP_WS_URL}`
		this.ws = uni.connectSocket({
			// #ifdef H5
			url: wss(wssUrl + '?type=user&token=' + token + '&form_type=' + form_type + '&tourist_uid=' + tourist_uid),
			// #endif
			// #ifdef MP || APP-PLUS
			url: wssUrl + '?type=user&token=' + token + '&form_type=' + form_type + '&tourist_uid=' + tourist_uid,
			// #endif
			header: {
				'content-type': 'application/json'
			},
			method: 'GET',
			success: (res) => {}
		});
		this.ws.onOpen(this.onSocketOpen.bind(this))
		this.ws.onError(this.onError.bind(this));
		this.ws.onMessage(this.onMessage.bind(this))
		this.ws.onClose(this.onClose.bind(this));
	}
pages/customer_list/chat.vue
代码修改较多,直接贴整个script代码段,请将
到的内容修改成以下代码

 
                         
                         
                     
                         
                     
                     
                     
                     
                     
                             
                                    
 
                                         
                                     
                 
                         
                     
                 
         
         
             
         
         
         
		