fix: 修复 mp-weixin 模板箭头函数和 TextDecoder 不兼容
- 微信小程序 wxml 模板不支持内联箭头函数 @submit 改为方法引用,submitClarification 自己找未提交卡片 - TextDecoder 在 mp-weixin 不可用 decodeChunk 改用纯 JS UTF-8 解码实现
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user