diff --git a/pages/ai/aichat.js b/pages/ai/aichat.js index ccc7bd0..07a385b 100644 --- a/pages/ai/aichat.js +++ b/pages/ai/aichat.js @@ -195,21 +195,23 @@ Page({ const user = app.globalData.user const socket = app.globalData.chatSocket + // 先判空再判连接状态,避免 socket 不存在时读取 isConnected 报错 + if (!socket) { + this.handleAIError('网络连接失败,请检查网络设置') + return + } + if (!socket.isConnected) { socket.connect() } - if (socket) { - socket.send({ - type: type, - content: content, - userId: user ? user._id : '', - appId: app.globalData.appId - }) - } else { - // 处理连接失败的情况 - this.handleAIError('网络连接失败,请检查网络设置') - } + socket.send({ + type: type, + content: content, + // 身份以 token 为准,服务端不再接受凭 userId 明文接入 + token: (user && user.security && user.security.token) || '', + appId: app.globalData.appId + }) }, handleAIError(errorMessage) {