143 lines
3.9 KiB
JavaScript
143 lines
3.9 KiB
JavaScript
import { BASE_URL } from "../../constant/base";
|
||
|
||
const defaultConfig = {
|
||
header: {
|
||
Authorization: 'Basic Y3VzdG9tOmN1c3RvbQ==', // 可在此动态设置 token
|
||
'Content-Type': 'application/x-www-form-urlencoded'
|
||
},
|
||
};
|
||
|
||
function request(url, args = {}, method = 'POST', customConfig = {}) {
|
||
// 判断 url 是否以 http 开头
|
||
if (!/^http/.test(url)) {
|
||
url = BASE_URL + url;
|
||
}
|
||
// 动态获取 token
|
||
const token = uni.getStorageSync('token');
|
||
|
||
let header = {
|
||
...defaultConfig.header,
|
||
...customConfig.header
|
||
};
|
||
// 判断是否需要 token
|
||
if (customConfig.noToken) {
|
||
delete header.Authorization;
|
||
} else {
|
||
if (token) {
|
||
header.Authorization = `Bearer ${token}`;
|
||
}
|
||
}
|
||
|
||
console.log("请求头customConfig:" + JSON.stringify(customConfig))
|
||
|
||
const config = {
|
||
...defaultConfig,
|
||
...customConfig,
|
||
header
|
||
};
|
||
console.log("请求接口:" + url)
|
||
console.log("请求头:" + JSON.stringify(config))
|
||
console.log("请求参数:" + JSON.stringify(args))
|
||
|
||
return new Promise((resolve, reject) => {
|
||
uni.request({
|
||
url,
|
||
data: args,
|
||
method,
|
||
...config,
|
||
success: (res) => {
|
||
console.log("请求响应:" + JSON.stringify(res))
|
||
resolve(res.data)
|
||
},
|
||
fail: (err) => {
|
||
console.error("请求失败:", err);
|
||
reject(err)
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
// 默认 POST
|
||
request.post = function(url, args = {}, config = {}) {
|
||
return request(url, args, 'POST', config);
|
||
};
|
||
|
||
// 支持 GET
|
||
request.get = function(url, args = {}) {
|
||
return request(url, args, 'GET');
|
||
};
|
||
|
||
/**
|
||
* 获取AI聊天流式信息(仅微信小程序支持)
|
||
* @param {Object} params 请求参数
|
||
* @param {Function} onChunk 回调,每收到一段数据触发
|
||
* @returns {Promise}
|
||
*/
|
||
request.getAIChatStream = function(params, onChunk) {
|
||
return new Promise((resolve, reject) => {
|
||
const token = uni.getStorageSync('token');
|
||
|
||
console.log("发送请求内容: ", params)
|
||
// #ifdef MP-WEIXIN
|
||
const requestTask = uni.request({
|
||
url: BASE_URL + '/agent/assistant/chat', // 替换为你的接口地址
|
||
method: 'POST',
|
||
data: params,
|
||
enableChunked: true,
|
||
header: {
|
||
Accept: 'text/event-stream',
|
||
'Content-Type': 'application/json',
|
||
Authorization: `Bearer ${token}`, // 如需token可加
|
||
},
|
||
responseType: 'arraybuffer',
|
||
success(res) {
|
||
resolve(res.data);
|
||
},
|
||
fail(err) {
|
||
reject(err);
|
||
}
|
||
});
|
||
|
||
requestTask.onHeadersReceived(res => {
|
||
console.log('onHeadersReceived', res);
|
||
});
|
||
|
||
requestTask.onChunkReceived(res => {
|
||
const base64 = uni.arrayBufferToBase64(res.data);
|
||
let data = '';
|
||
try {
|
||
data = decodeURIComponent(escape(atob(base64)));
|
||
} catch (e) {
|
||
// 某些平台可能不支持 atob,可以直接用 base64
|
||
data = base64;
|
||
}
|
||
const messages = parseSSEChunk(data);
|
||
messages.forEach(msg => {
|
||
if (onChunk) onChunk(msg);
|
||
});
|
||
});
|
||
// #endif
|
||
});
|
||
}
|
||
|
||
// 解析SSE分段数据
|
||
function parseSSEChunk(raw) {
|
||
// 拆分为多段
|
||
const lines = raw.split('\n\n');
|
||
const results = [];
|
||
lines.forEach(line => {
|
||
// 只处理包含 data: 的行
|
||
const dataMatch = line.match(/data:(\{.*\})/);
|
||
if (dataMatch && dataMatch[1]) {
|
||
try {
|
||
const obj = JSON.parse(dataMatch[1]);
|
||
results.push(obj);
|
||
} catch (e) {
|
||
// 解析失败忽略
|
||
}
|
||
}
|
||
});
|
||
return results;
|
||
}
|
||
|
||
export default request; |