diff --git a/mini-program/src/pages/main/ScriptView.vue b/mini-program/src/pages/main/ScriptView.vue index cfc9d0e..7db3e0e 100644 --- a/mini-program/src/pages/main/ScriptView.vue +++ b/mini-program/src/pages/main/ScriptView.vue @@ -126,7 +126,7 @@ 已回答:{{ msg.answer }} @@ -1674,11 +1674,13 @@ const handleShortNovelEvent = (event) => { } } -const submitClarification = (msg, answer) => { +const submitClarification = (answer) => { if (answeringClarification.value) return - if (msg) { - msg.submitted = true - msg.answer = answer + // 找最后一张未提交的卡片消息(mp-weixin 模板不支持内联箭头函数传 msg,改为自己查找) + const card = [...resultMessages.value].reverse().find(m => m.kind === 'card' && !m.submitted) + if (card) { + card.submitted = true + card.answer = answer } addResultMessage({ role: 'user', kind: 'text', content: answer }) answeringClarification.value = true diff --git a/mini-program/src/services/shortNovel.js b/mini-program/src/services/shortNovel.js index b15024c..83ca97c 100644 --- a/mini-program/src/services/shortNovel.js +++ b/mini-program/src/services/shortNovel.js @@ -106,11 +106,36 @@ export const followupStream = ({ sessionId, action, payload, onEvent, onError }) /** * 解码 chunk 数据 + * 兼容 mp-weixin(不支持 TextDecoder) */ function decodeChunk(chunk) { if (typeof chunk === 'string') return chunk - const decoder = new TextDecoder('utf-8') - return decoder.decode(new Uint8Array(chunk)) + // 小程序环境 chunk 可能是 ArrayBuffer,转成 UTF-8 字符串 + const bytes = new Uint8Array(chunk) + let result = '' + let i = 0 + while (i < bytes.length) { + const byte1 = bytes[i++] + if (byte1 < 0x80) { + result += String.fromCharCode(byte1) + } else if (byte1 < 0xE0) { + const byte2 = bytes[i++] + result += String.fromCharCode(((byte1 & 0x1F) << 6) | (byte2 & 0x3F)) + } else if (byte1 < 0xF0) { + const byte2 = bytes[i++] + const byte3 = bytes[i++] + result += String.fromCharCode(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F)) + } else { + const byte2 = bytes[i++] + const byte3 = bytes[i++] + const byte4 = bytes[i++] + const codePoint = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F) + // 转成 UTF-16 代理对 + const adjusted = codePoint - 0x10000 + result += String.fromCharCode(0xD800 | (adjusted >> 10)) + String.fromCharCode(0xDC00 | (adjusted & 0x3FF)) + } + } + return result } /**