转诊方功能优化
This commit is contained in:
@@ -38,6 +38,7 @@ export class WebSocketManager {
|
||||
|
||||
/**
|
||||
* 重新绑定用户(登录后调用)
|
||||
* 会先清理旧连接,再绑定新连接
|
||||
*/
|
||||
rebindUser() {
|
||||
const userId = uni.getStorageSync('user_id');
|
||||
@@ -50,12 +51,13 @@ export class WebSocketManager {
|
||||
this.clearCachedToken();
|
||||
|
||||
if (this.isConnected) {
|
||||
// 如果已连接,直接绑定
|
||||
return this.bindUser(userId);
|
||||
// 如果已连接,直接bind(Go后端会自动清理旧连接)
|
||||
// 注意:不在这里调用unbind,因为unbind会关闭当前连接
|
||||
return this.bindUser(userId, true);
|
||||
} else {
|
||||
// 如果未连接,先连接再绑定
|
||||
this.connect();
|
||||
// 连接成功后会在 onOpen 中自动绑定
|
||||
// 连接成功后会在 onOpen 中自动绑定(会自动清理旧连接)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -86,7 +88,8 @@ export class WebSocketManager {
|
||||
// 绑定当前用户(使用正确的key:user_id)
|
||||
const userId = uni.getStorageSync('user_id');
|
||||
if (userId) {
|
||||
this.bindUser(userId);
|
||||
// 连接打开后立即绑定,Go后端会自动清理旧连接
|
||||
this.bindUser(userId, true);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -113,20 +116,56 @@ export class WebSocketManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定用户(携带token进行认证)
|
||||
* @param {string} userId 用户ID
|
||||
* 解绑用户(清理旧连接)
|
||||
* 注意:此方法会关闭当前连接,只应在需要完全断开时使用
|
||||
* @param {string} userId 用户ID(格式:user-xxx 或 数字)
|
||||
* @returns {boolean} 是否发送成功
|
||||
*/
|
||||
bindUser(userId) {
|
||||
unbindUser(userId) {
|
||||
const token = this.getToken();
|
||||
if (!token) {
|
||||
console.warn('解绑用户失败:token不存在,跳过解绑');
|
||||
return false;
|
||||
}
|
||||
// 确保userId格式正确(如果是数字,添加user-前缀)
|
||||
const normalizedUserId = userId.toString().startsWith('user-') ? userId : `user-${userId}`;
|
||||
console.log('发送unbind请求,清理旧连接:', normalizedUserId);
|
||||
return this.send({
|
||||
request_type: 'unbind',
|
||||
user_type: 'user',
|
||||
sender_user_id: normalizedUserId,
|
||||
token: token
|
||||
}, false); // unbind不需要再次添加token
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定用户(携带token进行认证)
|
||||
* 在绑定前会自动清理旧连接(通过Go后端的clean_old_connections功能)
|
||||
* @param {string} userId 用户ID(可以是数字或user-xxx格式)
|
||||
* @param {boolean} cleanOldConnections 是否清理旧连接(默认true)
|
||||
*/
|
||||
bindUser(userId, cleanOldConnections = true) {
|
||||
const token = this.getToken();
|
||||
if (!token) {
|
||||
console.error('绑定用户失败:token不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 确保userId格式正确(统一为user-xxx格式)
|
||||
// 如果userId已经是user-xxx格式,保持不变;如果是数字,添加user-前缀
|
||||
const normalizedUserId = userId.toString().startsWith('user-') ? userId : `user-${userId}`;
|
||||
|
||||
// 注意:不在bind前发送unbind,因为unbind会关闭当前连接
|
||||
// 清理旧连接的工作交给Go后端的clean_old_connections功能处理
|
||||
|
||||
// 发送bind请求,添加clean_old_connections标志告诉Go后端清理旧连接
|
||||
console.log('发送bind请求,绑定新连接:', normalizedUserId, 'cleanOldConnections:', cleanOldConnections);
|
||||
return this.send({
|
||||
request_type: 'bind',
|
||||
user_type: 'user',
|
||||
sender_user_id: userId,
|
||||
token: token
|
||||
sender_user_id: normalizedUserId,
|
||||
token: token,
|
||||
clean_old_connections: cleanOldConnections // 告诉Go后端清理旧连接
|
||||
});
|
||||
}
|
||||
|
||||
@@ -199,11 +238,14 @@ export class WebSocketManager {
|
||||
this.getUserIdAndBindClientId();
|
||||
}, 5000)
|
||||
} else {
|
||||
// 直接发送bind请求,Go后端会自动清理旧连接
|
||||
// 注意:不在这里调用unbind,因为unbind会关闭当前连接
|
||||
const bindMessage = {
|
||||
request_type: 'bind',
|
||||
user_type: 'user',
|
||||
sender_user_id: `user-${userId}`,
|
||||
token: token
|
||||
token: token,
|
||||
clean_old_connections: true // 告诉Go后端清理旧连接
|
||||
};
|
||||
this.send(bindMessage);
|
||||
}
|
||||
@@ -227,7 +269,7 @@ export class WebSocketManager {
|
||||
this.cachedToken = null;
|
||||
// 通知应用层token过期
|
||||
uni.$emit('ws-token-expired');
|
||||
// 尝试重新绑定
|
||||
// 尝试重新绑定(会自动清理旧连接)
|
||||
this.getUserIdAndBindClientId();
|
||||
}
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user