涉及大量代碼修改,請(qǐng)備份相關(guān)被修改文件
邏輯如下
本身客服系統(tǒng)自帶游客功能,對(duì)代碼進(jìn)行了適當(dāng)?shù)男薷?,讓游客信息能順利入?kù)
不可忽視的問(wèn)題
找大佬繼續(xù)優(yōu)化
- 游客uid用的時(shí)間戳,如果你的已注冊(cè)會(huì)員數(shù)量達(dá)到10位數(shù),會(huì)與已注冊(cè)會(huì)員沖突,而且當(dāng)并發(fā)比較高的時(shí)候,比如恰好1秒內(nèi)有2個(gè)游客同時(shí)咨詢(xún),也會(huì)沖突
- 為了讓游客獲取到聊天記錄,取消了聊天記錄的驗(yàn)證(需要大佬解決),會(huì)泄露
- 沒(méi)有對(duì)游客聊天記錄定時(shí)清理
php代碼修改
app/controller/api/v2/user/StoreService.php
修改從第46行開(kāi)始,將
??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();
}
}
// 只有用戶(hù)模式下才能使用游客模式
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/目錄下的四個(gè)文件,依次修改
找到
???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
? ,將整行代碼移動(dòng)到下方授權(quán)不通過(guò),不會(huì)拋出異常繼續(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
代碼修改較多,直接貼整個(gè)script代碼段,請(qǐng)將
? 的內(nèi)容修改成以下代碼