除了bug就是bug,这东西真的是方便开发的吗...
requestTask.abort() 无法停止接口请求this.requestTask = wx.request({ url: this.API_URL2, // 后端流式接口地址 method: 'POST', header: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.CHAT_API_KEY}` }, responseType: 'stream', timeout: 600000, data: requestData, // signal: signal, // 使用 signal 参数 enableChunked: true, // 开启流式传输 success: (res) => { console.log('流式请求成功', res); this.isStop = true this.isMessageRendering = false; }, fail: (err) => { console.error('流式请求失败', err); // 请求失败,设置消息渲染结束 this.isMessageRendering = false; } }); // 定义一个监听器函数 const onChunkReceivedCallback = (res) => { // 检查是否停止回复 if (this.isStop) { console.log('停止回复,不再处理后续数据'); // 停止回复,设置消息渲染结束 this.isMessageRendering = false; // 在某个时刻,需要移除监听器 this.requestTask.offChunkReceived(onChunkReceivedCallback) this.requestTask.abort(); console.log( this.requestTask, ' this.requestTask') // 手动触发成功逻辑 return; } else{ const uint8Array = new Uint8Array(res.data); let text = String.fromCharCode.apply(null, uint8Array); // 将 ArrayBuffer 转换为字符串 // 去掉前缀 "data: " if (text.startsWith('data: ')) { text = text.substring(6); } // 尝试解析为 JSON 对象 try { const jsonObject = JSON.parse(text); console.log(jsonObject.answer, '.answer') if (jsonObject.event === 'message' && jsonObject.answer) { // 查找 loading 提示消息的索引 if (loadingIndex === -1) { loadingIndex = this.messages.findIndex(msg => msg.htmlContent === 'AI 正在思考...'); if (loadingIndex !== -1) { this.messages.splice(loadingIndex, 1); // 从 messages 数组中删除加载提示消息 } // 初始化 AI 回复消息 this.messages.push({ content: '', // 原始 Markdown 内容 htmlContent: '', // 初始化为空 isUser: false }); } fullAnswer += jsonObject.answer; console.log('JSONfullAnswer:', fullAnswer); // 更新最后一条消息的内容 const lastMessageIndex = this.messages.length - 1; this.messages[lastMessageIndex].htmlContent = marked(fullAnswer); this.scrollToBottom(); } else if (jsonObject.event === 'end') { // 流式响应结束,设置消息渲染结束 this.isMessageRendering = false; } } catch (e) { console.error('JSON 解析失败:', e); } } }; // 添加监听器 this.requestTask.onChunkReceived(onChunkReceivedCallback); if (this.isStop) { console.log('停止回复,不再处理后续数据'); // 停止回复,设置消息渲染结束 this.isMessageRendering = false; // 在某个时刻,需要移除监听器 this.requestTask.offChunkReceived(onChunkReceivedCallback) this.requestTask.abort(); console.log( this.requestTask, ' this.requestTask') // 手动触发成功逻辑 return; } 我想在这个里面停止这个接口的调用,用了 this.requestTask.offChunkReceived(onChunkReceivedCallback) this.requestTask.abort();这两个方式,第一个off是关闭监听了,但是接口还在继续调流式数据,后面那个直接不生效,要怎么解决
09-11