115 lines
3.7 KiB
JavaScript
115 lines
3.7 KiB
JavaScript
|
|
// 定义一个全局请求配置(可被多个拦截器修改)
|
|||
|
|
import {setCache} from "@/utils/cache";
|
|||
|
|
|
|||
|
|
export let globalConfig = {
|
|||
|
|
baseURL: 'https://wx.api.borman.top/api/',
|
|||
|
|
// baseURL: 'http://brm.wx.api.borman.top/api/',
|
|||
|
|
// baseURL: 'https://wx.boerman.top/api/',
|
|||
|
|
// baseURL: 'http://127.0.0.1:18004/api/', // 本地测试地址
|
|||
|
|
// baseURL: 'http://127.0.0.1:18006/api/', // 本地测试地址
|
|||
|
|
header: {
|
|||
|
|
'Content-Type': 'application/json'
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 请求拦截器队列
|
|||
|
|
const requestInterceptors = [];
|
|||
|
|
|
|||
|
|
// 添加请求拦截器
|
|||
|
|
export function addRequestInterceptor(interceptor) {
|
|||
|
|
requestInterceptors.push(interceptor);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 执行请求拦截器
|
|||
|
|
export async function runRequestInterceptors(config) {
|
|||
|
|
let newConfig = { ...config };
|
|||
|
|
for (const interceptor of requestInterceptors) {
|
|||
|
|
newConfig = await interceptor(newConfig);
|
|||
|
|
}
|
|||
|
|
return newConfig;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 响应拦截器队列
|
|||
|
|
const responseInterceptors = [];
|
|||
|
|
|
|||
|
|
// 添加响应拦截器
|
|||
|
|
export function addResponseInterceptor(interceptor) {
|
|||
|
|
responseInterceptors.push(interceptor);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 执行响应拦截器
|
|||
|
|
export async function runResponseInterceptors(response) {
|
|||
|
|
let newResponse = { ...response };
|
|||
|
|
for (const interceptor of responseInterceptors) {
|
|||
|
|
newResponse = await interceptor(newResponse);
|
|||
|
|
}
|
|||
|
|
return newResponse;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 初始化默认拦截器(可选)
|
|||
|
|
addRequestInterceptor(config => {
|
|||
|
|
// 自动携带token示例
|
|||
|
|
const token = uni.getStorageSync('token');
|
|||
|
|
// 临时token(测试用)
|
|||
|
|
// const token = 'JG7hKhTQoANh/hyL67BzIg==';
|
|||
|
|
if (token) {
|
|||
|
|
config.header.Authorization = `Bearer ${token}`;
|
|||
|
|
}
|
|||
|
|
return config;
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
addResponseInterceptor(response => {
|
|||
|
|
// 统一处理错误状态码
|
|||
|
|
if (response.statusCode !== 200) {
|
|||
|
|
uni.showToast({ title: `请求错误: ${response.data.message}`, icon: 'none' });
|
|||
|
|
return Promise.reject(response.data.message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (response.data.code === 500) {
|
|||
|
|
uni.showToast({ title: response.data.message, icon: 'none' });
|
|||
|
|
return Promise.reject(response.data.message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (response.data.code === 401) {
|
|||
|
|
uni.showToast({ title: '登录已过期,请重新登录', icon: 'none' });
|
|||
|
|
setCache('token', null);
|
|||
|
|
setTimeout(() => {
|
|||
|
|
// 储存当前访问的路由
|
|||
|
|
// 获取当前页面栈
|
|||
|
|
const pages = getCurrentPages();
|
|||
|
|
if (pages.length === 0) {
|
|||
|
|
console.error('未找到当前页面栈');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
// 获取当前页面实例
|
|||
|
|
const currentPage = pages[pages.length - 1];
|
|||
|
|
if (!currentPage) {
|
|||
|
|
console.error('未找到当前页面实例');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
// 获取当前页面的路由地址
|
|||
|
|
const currentRoute = currentPage.route;
|
|||
|
|
if (!currentRoute) {
|
|||
|
|
console.error('未找到当前页面的路由地址');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
// 获取传参
|
|||
|
|
const options = currentPage.options;
|
|||
|
|
// 构建包含参数的查询字符串
|
|||
|
|
let queryString = '';
|
|||
|
|
if (Object.keys(options).length > 0) {
|
|||
|
|
queryString = '?' + Object.entries(options)
|
|||
|
|
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
|
|||
|
|
.join('&');
|
|||
|
|
}
|
|||
|
|
// 拼接完整的路由地址
|
|||
|
|
const fullRoute = `/${currentRoute}${queryString}`;
|
|||
|
|
// 设置缓存
|
|||
|
|
setCache('currentPage', fullRoute);
|
|||
|
|
uni.reLaunch({ url: '/pages/login/index' });
|
|||
|
|
}, 1000)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return response.data.result; // 根据实际接口返回结构调整
|
|||
|
|
});
|