flat: ai对话提交
This commit is contained in:
106
utils/request.js
106
utils/request.js
@@ -1,69 +1,20 @@
|
||||
import config from "@/config.js"
|
||||
import useUserStore from '@/stores/useUserStore';
|
||||
export function request({
|
||||
url,
|
||||
method = 'GET',
|
||||
data = {},
|
||||
load = false,
|
||||
header = {}
|
||||
} = {}) {
|
||||
import {
|
||||
sm4Decrypt,
|
||||
sm4Encrypt
|
||||
} from '../common/globalFunction';
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (load) {
|
||||
uni.showLoading({
|
||||
title: '请稍候',
|
||||
mask: true
|
||||
});
|
||||
}
|
||||
let Authorization = ''
|
||||
if (useUserStore().token) {
|
||||
Authorization = `${useUserStore().userInfo.token}${useUserStore().token}`
|
||||
}
|
||||
uni.request({
|
||||
url: config.baseUrl + url,
|
||||
method,
|
||||
data: data,
|
||||
header: {
|
||||
'Authorization': Authorization || '',
|
||||
},
|
||||
success: resData => {
|
||||
// 响应拦截
|
||||
if (resData.statusCode === 200) {
|
||||
const {
|
||||
code,
|
||||
msg
|
||||
} = resData.data
|
||||
if (code === 200) {
|
||||
resolve(resData.data)
|
||||
return
|
||||
}
|
||||
uni.showToast({
|
||||
title: msg,
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
if (resData.data?.code === 401 || resData.data?.code === 402) {
|
||||
useUserStore().logOut()
|
||||
uni.showToast({
|
||||
title: '登录过期,请重新登录',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
const err = new Error('请求出现异常,请联系工作人员')
|
||||
err.error = resData
|
||||
reject(err)
|
||||
},
|
||||
fail: err => reject(err),
|
||||
complete() {
|
||||
if (load) {
|
||||
uni.hideLoading();
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const needToEncrypt = [
|
||||
["post", "/app/login"],
|
||||
["get", "/app/user/resume"],
|
||||
["post", "/app/user/resume"],
|
||||
["post", "/app/user/experience/edit"],
|
||||
["post", "/app/user/experience/delete"],
|
||||
["get", "/app/user/experience/getSingle/{value}"],
|
||||
["get", "/app/user/experience/list"]
|
||||
]
|
||||
|
||||
/**
|
||||
* @param url String,请求的地址,默认:none
|
||||
@@ -87,15 +38,44 @@ export function createRequest(url, data = {}, method = 'GET', loading = false, h
|
||||
|
||||
const header = headers || {};
|
||||
header["Authorization"] = encodeURIComponent(Authorization);
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 检查当前请求是否需要加密
|
||||
const isEncrypt = needToEncrypt.some(item => {
|
||||
const matchMethod = item[0].toLowerCase() === method.toLowerCase();
|
||||
const matchUrl = item[1].includes('{') ?
|
||||
url.startsWith(item[1].split('/{')[0]) // 检查动态路径的前缀
|
||||
:
|
||||
item[1] === url; // 检查静态路径
|
||||
return matchMethod && matchUrl;
|
||||
});
|
||||
|
||||
let requestData = data;
|
||||
|
||||
if (isEncrypt) {
|
||||
const jsonData = JSON.stringify(data);
|
||||
const encryptedBody = sm4Encrypt(config.sm4Config.key, jsonData);
|
||||
requestData = {
|
||||
encrypted: true,
|
||||
encryptedData: encryptedBody,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: config.baseUrl + url,
|
||||
method: method,
|
||||
data: data,
|
||||
data: requestData,
|
||||
header,
|
||||
success: resData => {
|
||||
// 响应拦截
|
||||
if (resData.statusCode === 200) {
|
||||
if (resData.data.encrypted) {
|
||||
const decryptedData = sm4Decrypt(config
|
||||
.sm4Config.key, resData.data.encryptedData)
|
||||
resData.data = JSON.parse(decryptedData)
|
||||
}
|
||||
const {
|
||||
code,
|
||||
msg
|
||||
|
||||
@@ -17,7 +17,7 @@ export default function StreamRequest(url, data = {}, onDataReceived, onError, o
|
||||
};
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const response = await fetch(config.StreamBaseURl + url, {
|
||||
const response = await fetch(config.baseUrl + url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(data)
|
||||
@@ -46,6 +46,7 @@ export default function StreamRequest(url, data = {}, onDataReceived, onError, o
|
||||
let lines = buffer.split("\n");
|
||||
buffer = lines.pop(); // 可能是不完整的 JSON 片段,留待下次解析
|
||||
for (let line of lines) {
|
||||
line = line.slice(5).trim()
|
||||
if (line.startsWith("data: ")) {
|
||||
const jsonData = line.slice(6).trim();
|
||||
if (jsonData === "[DONE]") {
|
||||
|
||||
Reference in New Issue
Block a user