fix: 修复 mp-weixin 模板箭头函数和 TextDecoder 不兼容

- 微信小程序 wxml 模板不支持内联箭头函数
  @submit 改为方法引用,submitClarification 自己找未提交卡片
- TextDecoder 在 mp-weixin 不可用
  decodeChunk 改用纯 JS UTF-8 解码实现
This commit is contained in:
2026-07-19 19:02:52 +08:00
parent 7a0e729ab4
commit b194b343ad
2 changed files with 34 additions and 7 deletions
+7 -5
View File
@@ -126,7 +126,7 @@
<ClarificationCard <ClarificationCard
v-if="!msg.submitted && msg.card" v-if="!msg.submitted && msg.card"
:card="msg.card" :card="msg.card"
@submit="(answer) => submitClarification(msg, answer)" @submit="submitClarification"
/> />
<view v-else class="card-answered"> <view v-else class="card-answered">
<text>已回答{{ msg.answer }}</text> <text>已回答{{ msg.answer }}</text>
@@ -1674,11 +1674,13 @@ const handleShortNovelEvent = (event) => {
} }
} }
const submitClarification = (msg, answer) => { const submitClarification = (answer) => {
if (answeringClarification.value) return if (answeringClarification.value) return
if (msg) { // 找最后一张未提交的卡片消息(mp-weixin 模板不支持内联箭头函数传 msg,改为自己查找)
msg.submitted = true const card = [...resultMessages.value].reverse().find(m => m.kind === 'card' && !m.submitted)
msg.answer = answer if (card) {
card.submitted = true
card.answer = answer
} }
addResultMessage({ role: 'user', kind: 'text', content: answer }) addResultMessage({ role: 'user', kind: 'text', content: answer })
answeringClarification.value = true answeringClarification.value = true
+27 -2
View File
@@ -106,11 +106,36 @@ export const followupStream = ({ sessionId, action, payload, onEvent, onError })
/** /**
* 解码 chunk 数据 * 解码 chunk 数据
* 兼容 mp-weixin(不支持 TextDecoder
*/ */
function decodeChunk(chunk) { function decodeChunk(chunk) {
if (typeof chunk === 'string') return chunk if (typeof chunk === 'string') return chunk
const decoder = new TextDecoder('utf-8') // 小程序环境 chunk 可能是 ArrayBuffer,转成 UTF-8 字符串
return decoder.decode(new Uint8Array(chunk)) 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
} }
/** /**