api:onChunkReceived 基础库 3.811 微信版本8.0.61
如果基础调试库降低到3.6.6以及以下就能流式返回了。那我担心发生产后该问题仍然会存在
import { showToast } from '@/utils/index';
import store from '@/store';
const REQUEST_HOST = 'https://dashscope.aliyuncs.com';
function chatStream(content, { onMessage, onFinish, onError }) {
const url = '/compatible-mode/v1/chat/completions';
const data = {
"model": "qwen-plus",
"stream": true,
"messages": [
{
"role": "system",
"content": `请优化以下用户提供的工作经历描述。要求:
1. **突出成果与贡献:** 使用强动词开头,量化成果(金额、百分比、数量、时间等),强调对团队/公司的具体影响。
2. **增强专业性:** 使用行业相关术语,体现具体职责和所用技能/技术。
3. **清晰简洁:** 语言精炼,逻辑清晰,避免冗余信息。使用项目符号或分号分隔关键点。
4. **符合事实:** 优化基于提供的信息,不虚构未提及的内容。
5. **目标导向:** 优化后的描述应能体现用户在该职位上的核心价值和能力。
6. **包含技能关键词:** 自然地融入与职位相关的核心技能关键词。**请直接提供优化后的工作经历描述,不要包含解释或额外说明。**`},
{
"role": "user",
"content": content ,
}
]
};
let buffer = '';
let isFinished = false;
const requestTask = wx.request({
url: `${process.env.SiteUrl}chat/ai/${url}`,
data,
method: 'POST', // 请求方式
// method: 'GET', // 请求方式
enableChunked: true,
responseType: 'arraybuffer', // 设置响应类型为 arraybuffer
timeout: 100000,
header: {
'Authorization': 'Bearer xxx',
'X-Requested-With': 'XMLHttpRequest',
"Content-Type": 'application/json'
},
success: (res) => {
showChunkMsg(buffer, res, onMessage, onFinish);
console.log('结束2')
if (!isFinished) onFinish && onFinish();
},
fail: (err) => {
console.log('错误', err);
onError && onError(err);
}
});
requestTask.onChunkReceived(res => {
console.log('接收', res);
showChunkMsg(buffer, res, onMessage, onFinish);
})
return requestTask; // 可用于外部 abort
}
const showChunkMsg = (buffer, res, onMessage, onFinish) => {
let chunk;
if (typeof TextDecoder !== 'undefined') {
chunk = new TextDecoder('utf-8').decode(new Uint8Array(res.data)); // 本地开发者工具使用
} else if (res.data instanceof ArrayBuffer) {
// 兜底:自己实现的 arrayBufferToString
chunk = arrayBufferToString(res.data);
} else if (typeof res.data === 'string') {
chunk = res.data;
} else {
chunk = '';
}
buffer += chunk;
// 你需要按行分割,逐条处理
const lines = buffer.split('\n');
buffer = lines.pop(); // 最后一行可能是不完整的,留到下次
for (const line of lines) {
if (!line.trim()) continue;
if (line.startsWith('data: ')) {
const jsonStr = line.replace(/^data: /, '');
try {
const json = JSON.parse(jsonStr);
const content = json.choices?.[0]?.delta?.content || '';
if (content) onMessage(content);
if (json.choices?.[0]?.finish_reason) {
isFinished = true;
onFinish && onFinish();
}
} catch (e) {
// 解析失败,可能是 [DONE] 或其他非json内容,忽略
}
}
}
}
// 把 微信返回的arrayBuffer格式,转成字符串
const arrayBufferToString = (arr) => {
if (typeof arr === "string") {
return arr;
}
var dataview = new DataView(arr);
var ints = new Uint8Array(arr.byteLength);
for (var i = 0; i < ints.length; i++) {
ints[i] = dataview.getUint8(i);
}
var str = "",
_arr = ints;
for (var i = 0; i < _arr.length; i++) {
if (_arr[i]) {
var one = _arr[i].toString(2),
v = one.match(/^1+?(?=0)/);
if (v && one.length == 8) {
var bytesLength = v[0].length;
var store = _arr[i].toString(2).slice(7 - bytesLength);
for (var st = 1; st < bytesLength; st++) {
if (_arr[st + i]) {
store += _arr[st + i].toString(2).slice(2);
}
}
str += String.fromCharCode(parseInt(store, 2));
i += bytesLength - 1;
} else {
str += String.fromCharCode(_arr[i]);
}
}
}
return str;
};
export default {
chatStream
}

请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
社区真垃圾