1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| const res = wx.request({ url: `${baseURL}/wechat/stream`, method: "POST", data: param, enableChunked: true, responseType: "arraybuffer", timeout: 1800000, success: (res) => { console.log("请求成功"); const lastIndex = updatedMessages.length - 1; if (lastIndex >= 0) { const updatedMessagesCopy = [...updatedMessages]; updatedMessagesCopy[lastIndex].isLoading = false; this.setData({ messages: updatedMessagesCopy, isLoading: false, }); } this.scrollToBottom(); }, fail: (error) => { console.error("请求失败", error); const lastIndex = updatedMessages.length - 1; if (lastIndex >= 0) { const updatedMessagesCopy = [...updatedMessages]; updatedMessagesCopy[lastIndex].isLoading = false; this.setData({ messages: updatedMessagesCopy, isLoading: false, }); } wx.showToast({ title: "发送失败,请重试", icon: "none", }); }, });
res.onChunkReceived((chunk) => { console.log(chunk, "chunk"); let text = "";
try { if (wx.arrayBufferToString) { text = wx.arrayBufferToString(chunk.data); } else { const uint8Array = new Uint8Array(chunk.data); let i = 0; while (i < uint8Array.length) { let byte = uint8Array[i++]; if (byte < 0x80) { text += String.fromCharCode(byte); } else if (byte < 0xe0) { if (i < uint8Array.length) { const byte2 = uint8Array[i++]; text += String.fromCharCode(((byte & 0x1f) << 6) | (byte2 & 0x3f)); } } else if (byte < 0xf0) { if (i + 1 < uint8Array.length) { const byte2 = uint8Array[i++]; const byte3 = uint8Array[i++]; text += String.fromCharCode( ((byte & 0x0f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f) ); } } } }
const lines = text.split("\n"); let isStop = false; for (let line of lines) { if (!line || line.startsWith("data:")) { line = line.replace("data:", ""); } if (!line) { continue; }
try { const pattern = /"response":"(.*?)"/; const result = line.match(pattern); if (result) { const content = result[1]; const lastIndex = updatedMessages.length - 1;
if (lastIndex >= 0) { const updatedMessagesCopy = [...updatedMessages]; updatedMessagesCopy[lastIndex].isLoading = false; updatedMessagesCopy[lastIndex].content += content;
const thinkContent = this.getThinkContent( updatedMessagesCopy[lastIndex].content ); const replyContent = this.getReplyContent( updatedMessagesCopy[lastIndex].content );
updatedMessagesCopy[lastIndex].thinkContent = thinkContent;
updatedMessagesCopy[lastIndex].replyContent = replyContent;
this.setData( { messages: updatedMessagesCopy, isLoading: false, }, () => { this.scrollToBottom(); } );
const updatedChatList = this.data.chatList.map((chat) => { if (chat.id === this.data.currentChatId) { return { ...chat, messages: updatedMessagesCopy }; } return chat; });
this.setData({ chatList: updatedChatList }); } } } catch (error) { console.error("JSON parse error:", error, line); continue; } } } catch (error) { console.error("解码数据时出错:", error); const lastIndex = updatedMessages.length - 1; if (lastIndex >= 0) { const updatedMessagesCopy = [...updatedMessages]; updatedMessagesCopy[lastIndex].isLoading = false; this.setData({ messages: updatedMessagesCopy, isLoading: false, }); } } });
this.scrollToBottom();
|