Files
YGChatCS/src/request/base/request.js
duanshuwen 14defcc5b7 feat(recharge): add full virtual recharge flow for aigc credits
- Replace legacy recharge API endpoints with new dedicated VirtualRechargeApi with sensitive request logging
- Implement pending order tracking, automatic payment reconciliation, and recovery for interrupted payments
- Overhaul recharge component logic to support both pre-defined package and custom amount recharge
- Fix currency price formatting to consistently display 2 decimal places for all monetary values
- Remove unused xiaoqi-avatar asset and related component imports
- Simplify Vite build asset naming configuration
- Switch application to test development mode
2026-07-22 16:17:16 +08:00

107 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { getCurrentConfig } from "@/constant/base";
import { useAppStore, useLocationStore } from "@/store";
import { NOTICE_EVENT_LOGOUT } from "@/constant/constant";
import { getAccessToken, removeAccessToken } from "@/constant/token";
import { goLogin } from "../../hooks/useGoLogin";
const clientId = getCurrentConfig().clientId;
const defaultConfig = {
header: {
"Content-Type": "application/json",
Authorization: "", // 可在此动态设置 token
clientId: clientId,
},
};
function request(url, args = {}, method = "POST", customConfig = {}) {
const { sensitive = false, ...requestCustomConfig } = customConfig;
customConfig = requestCustomConfig;
const appStore = useAppStore();
// 判断 url 是否以 http 开头
if (!/^http/.test(url)) {
url = appStore.serverConfig?.baseUrl + url;
}
// 获取位置信息并添加到请求头
const locationStore = useLocationStore();
if (locationStore.latitude && locationStore.longitude) {
customConfig.header = {
...customConfig.header,
"X-Latitude": locationStore.latitude,
"X-Longitude": locationStore.longitude
};
}
// 动态获取 token
const token = getAccessToken();
let header = {
...defaultConfig.header,
...customConfig.header,
};
// 判断是否需要 token
if (customConfig.noToken) {
delete header.Authorization;
} else {
if (!header.Authorization && (token || customConfig.token)) {
header.Authorization = `Bearer ${token || customConfig.token}`;
}
}
const config = {
...defaultConfig,
...customConfig,
header,
};
if (sensitive) {
console.log(`\n\n请求接口: ${url}, \n敏感请求参数和请求头已隐藏\n\n`);
} else {
console.log(`\n\n请求接口: ${url}, \n请求参数: ${JSON.stringify(args)}, \n请求头: ${JSON.stringify(config)}\n\n`);
}
return new Promise((resolve, reject) => {
uni.request({
url,
data: args,
method,
...config,
success: (res) => {
if (sensitive) {
console.log(`\n\n请求接口: ${url}, \n响应状态: ${res.statusCode || "unknown"}\n\n`);
} else {
console.log(`\n\n请求接口: ${url}, \n请求响应: ${JSON.stringify(res)}, \n\n`);
}
resolve(res.data);
if (res.statusCode && res.statusCode === 424) {
console.log("424错误重新登录");
removeAccessToken();
uni.$emit(NOTICE_EVENT_LOGOUT);
setTimeout(() => {
/// 去登录页面
goLogin();
}, 500);
}
},
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 = {}, config = {}) {
return request(url, args, "GET", config);
};
export default request;