涉及大量代碼修改,請備份相關(guān)被修改文件
邏輯如下
本身客服系統(tǒng)自帶游客功能,對代碼進行了適當?shù)男薷?,讓游客信息能順利入?/p>
不可忽視的問題
找大佬繼續(xù)優(yōu)化
- 游客uid用的時間戳,如果你的已注冊會員數(shù)量達到10位數(shù),會與已注冊會員沖突,而且當并發(fā)比較高的時候,比如恰好1秒內(nèi)有2個游客同時咨詢,也會沖突
- 為了讓游客獲取到聊天記錄,取消了聊天記錄的驗證(需要大佬解決),會泄露
- 沒有對游客聊天記錄定時清理
php代碼修改
app/controller/api/v2/user/StoreService.php
修改從第46行開始,將
??function record
? 后面的{}
? 里面的內(nèi)容修改為
[$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
? ,將整行代碼移動到下方授權(quán)不通過,不會拋出異常繼續(xù)執(zhí)行
? 的{}
? 里面
前端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代碼段,請將
? 的內(nèi)容修改成以下代碼