From 136b3111cf56e813fbe0830533fc1f51702f5b0a Mon Sep 17 00:00:00 2001 From: lik Date: Wed, 7 Oct 2026 15:51:42 +0800 Subject: [PATCH] tmp --- pages/ai/aichat.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) 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) {