转诊方功能优化

This commit is contained in:
李琦
2026-02-07 11:12:18 +08:00
parent d233a42754
commit 184ff88bd2
8 changed files with 874 additions and 14 deletions

View File

@@ -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);
// 如果已连接,直接bindGo后端会自动清理旧连接
// 注意不在这里调用unbind因为unbind会关闭当前连接
return this.bindUser(userId, true);
} else {
// 如果未连接,先连接再绑定
this.connect();
// 连接成功后会在 onOpen 中自动绑定
// 连接成功后会在 onOpen 中自动绑定(会自动清理旧连接)
return true;
}
}
@@ -86,7 +88,8 @@ export class WebSocketManager {
// 绑定当前用户使用正确的keyuser_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;