企业信息补全页面开发

This commit is contained in:
冯辉
2025-10-21 22:58:47 +08:00
parent 968e6b4091
commit 8bb3c424e2
61 changed files with 2793375 additions and 812 deletions

View File

@@ -7,6 +7,108 @@ import useUserStore from '@/stores/useUserStore';
* @returns promise
**/
export default function StreamRequest(url, data = {}, onDataReceived, onError, onComplete) {
// #ifdef MP-WEIXIN
return StreamRequestMiniProgram(url, data, onDataReceived, onError, onComplete);
// #endif
// #ifdef H5
return StreamRequestH5(url, data, onDataReceived, onError, onComplete);
// #endif
// #ifndef H5 || MP-WEIXIN
return StreamRequestH5(url, data, onDataReceived, onError, onComplete);
// #endif
}
// 微信小程序流式请求实现
function StreamRequestMiniProgram(url, data = {}, onDataReceived, onError, onComplete) {
const userStore = useUserStore();
const Authorization = userStore.token ? encodeURIComponent(userStore.token) : '';
return new Promise((resolve, reject) => {
let buffer = '';
const requestTask = uni.request({
url: config.StreamBaseURl + url,
method: 'POST',
data: data,
header: {
"Authorization": Authorization,
"Accept": "text/event-stream",
"Content-Type": "application/json;charset=UTF-8"
},
enableChunked: true, // 启用分块传输
success: (res) => {
console.log('📡 Stream request completed');
onComplete && onComplete();
resolve();
},
fail: (err) => {
console.error('Stream 请求失败:', err);
onError && onError(err);
reject(err);
}
});
// 监听分块数据
requestTask.onChunkReceived((res) => {
try {
const decoder = new TextDecoder('utf-8');
const chunk = decoder.decode(new Uint8Array(res.data));
buffer += chunk;
let lines = buffer.split("\n");
buffer = lines.pop() || ''; // 保留不完整的行
for (let line of lines) {
if (line.startsWith("data: ")) {
const jsonData = line.slice(6).trim();
if (jsonData === "[DONE]") {
onComplete && onComplete();
resolve();
return;
}
if (jsonData && jsonData.trim()) {
try {
const parsedData = JSON.parse(jsonData);
// 处理标准的choices格式
if (parsedData?.choices?.[0]?.delta?.content) {
const content = parsedData.choices[0].delta.content;
if (content) {
onDataReceived && onDataReceived(content);
}
}
// 处理reasoning_content
else if (parsedData?.choices?.[0]?.delta?.reasoning_content) {
const content = parsedData.choices[0].delta.reasoning_content;
if (content) {
onDataReceived && onDataReceived(content);
}
}
// 处理tool响应
else if (parsedData?.tool?.response) {
const content = parsedData.tool.response;
if (content) {
onDataReceived && onDataReceived(content);
}
}
} catch (e) {
console.error("JSON 解析失败:", e.message);
}
}
}
}
} catch (error) {
console.error('处理分块数据失败:', error);
}
});
});
}
// H5 流式请求实现(原有的 fetch 实现)
function StreamRequestH5(url, data = {}, onDataReceived, onError, onComplete) {
const userStore = useUserStore();
const Authorization = userStore.token ? encodeURIComponent(userStore.token) : '';