From 6e658c9967f02f26c91a260af8dbd9edeea4d224 Mon Sep 17 00:00:00 2001 From: zoujing Date: Tue, 5 Aug 2025 22:46:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=B9=E8=AF=9D=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request/api/AgentChatStream.js | 55 +++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/request/api/AgentChatStream.js b/request/api/AgentChatStream.js index 7f56b6a..f01cd78 100644 --- a/request/api/AgentChatStream.js +++ b/request/api/AgentChatStream.js @@ -63,7 +63,7 @@ function agentChatStream(params, onChunk) { const base64 = uni.arrayBufferToBase64(res.data); let data = ''; try { - data = decodeURIComponent(escape(atob(base64))); + data = decodeURIComponent(escape(weAtob(base64))); } catch (e) { // 某些平台可能不支持 atob,可以直接用 base64 data = base64; @@ -77,6 +77,59 @@ function agentChatStream(params, onChunk) { }); } +// window.atob兼容性处理 +const weAtob = (string) => { + const b64re = + /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/; + const b64 = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; + + // 去除空白字符 + string = String(string).replace(/[\t\n\f\r ]+/g, ''); + + // 验证 Base64 编码 + if (!b64re.test(string)) { + throw new TypeError( + // eslint-disable-next-line quotes + "Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded." + ); + } + + // 填充字符 + string += '=='.slice(2 - (string.length & 3)); + + let bitmap, + result = '', + r1, + r2, + i = 0; + + for (; i < string.length;) { + bitmap = + (b64.indexOf(string.charAt(i++)) << 18) | + (b64.indexOf(string.charAt(i++)) << 12) | + ((r1 = b64.indexOf(string.charAt(i++))) << 6) | + (r2 = b64.indexOf(string.charAt(i++))); + + if (r1 === 64) { + result += String.fromCharCode((bitmap >> 16) & 255); + } else if (r2 === 64) { + result += String.fromCharCode( + (bitmap >> 16) & 255, + (bitmap >> 8) & 255 + ); + } else { + result += String.fromCharCode( + (bitmap >> 16) & 255, + (bitmap >> 8) & 255, + bitmap & 255 + ); + } + } + return result; +}; + + // 解析SSE分段数据 function parseSSEChunk(raw) { // 拆分为多段