Files
lgp-wx-new/api/request.js
2026-06-05 14:06:20 +08:00

70 lines
2.0 KiB
JavaScript
Raw Permalink 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 {
runRequestInterceptors,
runResponseInterceptors,
globalConfig
} from './interceptor.js';
// import * as uni from "@/api/request"; // 原错误导入保留供参考
// 基础请求方法
export function request(options) {
return new Promise(async (resolve, reject) => {
try {
// 合并全局配置
const mergedConfig = {
...globalConfig,
...options,
header: { ...globalConfig.header, ...(options.header || {}) }
};
// 执行请求拦截器
let finalConfig;
try {
finalConfig = await runRequestInterceptors(mergedConfig);
} catch (err) {
return reject(err);
}
if (!finalConfig.url) {
return reject(new Error('URL is required'));
}
// 发起请求修复点移除多余的await
uni.request({
...finalConfig,
url: finalConfig.baseURL + finalConfig.url,
success: async (res) => {
try {
const handledRes = await runResponseInterceptors(res);
// console.log(handledRes, 'ssssssssssssssss'); // 保留调试日志
resolve(handledRes);
} catch (err) {
reject(err);
}
},
fail: (err) => reject(err)
});
} catch (error) {
reject(error);
}
});
}
// 封装GET方法保持async/await结构
export async function get(url, params = {}, options = {}) {
return await request({
url,
method: 'GET',
data: params,
...options
});
}
// 封装POST方法保持async/await结构
export async function post(url, data = {}, options = {}) {
return await request({
url,
method: 'POST',
data,
...options
});
}