AI模块联调
This commit is contained in:
@@ -51,12 +51,101 @@ function StreamRequestMiniProgram(url, data = {}, onDataReceived, onError, onCom
|
||||
}
|
||||
});
|
||||
|
||||
// 监听分块数据
|
||||
requestTask.onChunkReceived((res) => {
|
||||
try {
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
const chunk = decoder.decode(new Uint8Array(res.data));
|
||||
console.log('📦 收到分块数据:', chunk);
|
||||
// UTF-8解码函数,用于微信小程序真机环境
|
||||
function utf8Decode(uint8Array) {
|
||||
let result = '';
|
||||
let i = 0;
|
||||
const len = uint8Array.length;
|
||||
|
||||
while (i < len) {
|
||||
const byte1 = uint8Array[i];
|
||||
|
||||
// 1字节字符 (0xxxxxxx)
|
||||
if (byte1 < 0x80) {
|
||||
result += String.fromCharCode(byte1);
|
||||
i++;
|
||||
}
|
||||
// 2字节字符 (110xxxxx 10xxxxxx)
|
||||
else if (byte1 >= 0xC0 && byte1 < 0xE0) {
|
||||
if (i + 1 < len) {
|
||||
const byte2 = uint8Array[i + 1];
|
||||
if (byte2 >= 0x80 && byte2 < 0xC0) {
|
||||
const codePoint = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
|
||||
result += String.fromCharCode(codePoint);
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// 无效的UTF-8序列,跳过
|
||||
result += '<27>';
|
||||
i++;
|
||||
}
|
||||
// 3字节字符 (1110xxxx 10xxxxxx 10xxxxxx)
|
||||
else if (byte1 >= 0xE0 && byte1 < 0xF0) {
|
||||
if (i + 2 < len) {
|
||||
const byte2 = uint8Array[i + 1];
|
||||
const byte3 = uint8Array[i + 2];
|
||||
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0)) {
|
||||
const codePoint = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
|
||||
result += String.fromCharCode(codePoint);
|
||||
i += 3;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// 无效的UTF-8序列,跳过
|
||||
result += '<27>';
|
||||
i++;
|
||||
}
|
||||
// 4字节字符 (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)
|
||||
else if (byte1 >= 0xF0 && byte1 < 0xF8) {
|
||||
if (i + 3 < len) {
|
||||
const byte2 = uint8Array[i + 1];
|
||||
const byte3 = uint8Array[i + 2];
|
||||
const byte4 = uint8Array[i + 3];
|
||||
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0) && (byte4 >= 0x80 && byte4 < 0xC0)) {
|
||||
let codePoint = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
|
||||
// 处理UTF-16代理对
|
||||
if (codePoint >= 0x10000) {
|
||||
codePoint -= 0x10000;
|
||||
const highSurrogate = (codePoint >> 10) + 0xD800;
|
||||
const lowSurrogate = (codePoint & 0x3FF) + 0xDC00;
|
||||
result += String.fromCharCode(highSurrogate, lowSurrogate);
|
||||
} else {
|
||||
result += String.fromCharCode(codePoint);
|
||||
}
|
||||
i += 4;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// 无效的UTF-8序列,跳过
|
||||
result += '<27>';
|
||||
i++;
|
||||
}
|
||||
// 无效的UTF-8序列,跳过
|
||||
else {
|
||||
result += '<27>';
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 监听分块数据
|
||||
requestTask.onChunkReceived((res) => {
|
||||
try {
|
||||
// 微信小程序兼容处理:微信小程序不支持TextDecoder,使用自定义UTF-8解码
|
||||
let chunk = '';
|
||||
if (typeof TextDecoder !== 'undefined') {
|
||||
// 支持TextDecoder的环境(如开发者工具)
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
chunk = decoder.decode(new Uint8Array(res.data));
|
||||
} else {
|
||||
// 微信小程序真机环境,使用自定义UTF-8解码函数
|
||||
const uint8Array = new Uint8Array(res.data);
|
||||
chunk = utf8Decode(uint8Array);
|
||||
}
|
||||
console.log('📦 收到分块数据:', chunk);
|
||||
buffer += chunk;
|
||||
|
||||
let lines = buffer.split("\n");
|
||||
|
||||
Reference in New Issue
Block a user