2025-02-18 13:38:21 +08:00
|
|
|
|
import { useMemberStore } from '@/stores';
|
2024-12-13 15:30:11 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加拦截器
|
|
|
|
|
|
* 拦截 request 请求
|
|
|
|
|
|
* baseURL 设置请求ip地址和端口
|
|
|
|
|
|
*/
|
2025-02-18 13:38:21 +08:00
|
|
|
|
const ENV = process.env.NODE_ENV;
|
2025-02-24 18:51:24 +08:00
|
|
|
|
export const baseURL = ENV === 'development' ? '/api' : '/iws/proxyApi'; // 宏源服务
|
|
|
|
|
|
// export const baseURL = ENV === 'development' ? '/api' : '/proxyApi'; // 测试服务
|
2024-12-13 15:30:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* httpInterceptor 分别拦截 request 和 uploadFile 请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-02-25 16:34:52 +08:00
|
|
|
|
const origin = window.location.href;
|
|
|
|
|
|
|
2024-12-13 15:30:11 +08:00
|
|
|
|
// 添加请求拦截
|
|
|
|
|
|
const httpInterceptor = {
|
|
|
|
|
|
invoke(options) {
|
|
|
|
|
|
// 1. 先判断请求 url 是否为完整的 http 请求路径 如果不是则拼接 baseURL
|
|
|
|
|
|
if (!options.url.startsWith('http')) {
|
2025-02-18 13:38:21 +08:00
|
|
|
|
options.url = baseURL + options.url;
|
2024-12-13 15:30:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 设置请求超时时间,默认为60s,设置为 10s
|
2025-02-18 13:38:21 +08:00
|
|
|
|
options.timeout = 10000;
|
2024-12-13 15:30:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 3. 增加小程序端请求头标识
|
|
|
|
|
|
options.header = {
|
|
|
|
|
|
// 'source-client': 'mini',
|
|
|
|
|
|
...options.header,
|
2025-02-18 13:38:21 +08:00
|
|
|
|
};
|
2024-12-13 15:30:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 4. 增加 token 请求头标识
|
2025-02-18 13:38:21 +08:00
|
|
|
|
const memberStore = useMemberStore();
|
|
|
|
|
|
const token = memberStore.token;
|
2024-12-13 15:30:11 +08:00
|
|
|
|
if (token) {
|
2025-02-18 13:38:21 +08:00
|
|
|
|
options.header.Authorization = token;
|
2024-12-13 15:30:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-02-18 13:38:21 +08:00
|
|
|
|
};
|
2024-12-13 15:30:11 +08:00
|
|
|
|
|
2025-02-18 13:38:21 +08:00
|
|
|
|
uni.addInterceptor('request', httpInterceptor);
|
|
|
|
|
|
uni.addInterceptor('uploadFile', httpInterceptor);
|
2024-12-13 15:30:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 设置http请求
|
|
|
|
|
|
export const http = (options) => {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
uni.request({
|
|
|
|
|
|
...options,
|
|
|
|
|
|
success(res) {
|
|
|
|
|
|
// 1. 判断是否请求成功
|
|
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
|
|
if (res.data.code >= 200 && res.data.code < 300) {
|
2025-02-18 13:38:21 +08:00
|
|
|
|
resolve(res.data);
|
|
|
|
|
|
} else if (res.data.code === 401 || res.data.code === 403) {
|
2024-12-13 15:30:11 +08:00
|
|
|
|
// 2. 401 表示token过期 去往登录页重新登录
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
|
|
|
|
|
title: `${res.data.msg}`,
|
2025-02-18 13:38:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
const memberStore = useMemberStore();
|
|
|
|
|
|
memberStore.clearUserInfo();
|
|
|
|
|
|
memberStore.clearToken();
|
2025-02-25 16:34:52 +08:00
|
|
|
|
if (origin.indexOf('ticket') == -1) {
|
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
|
url: '/pages/login/index',
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 宏源
|
|
|
|
|
|
document.addEventListener(
|
|
|
|
|
|
"deviceready",
|
|
|
|
|
|
() => {
|
|
|
|
|
|
navigator.security.closeWindow();
|
|
|
|
|
|
},
|
|
|
|
|
|
false
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-02-18 13:38:21 +08:00
|
|
|
|
reject(res);
|
2024-12-13 15:30:11 +08:00
|
|
|
|
} else if (res.data.code === 500) {
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
|
|
|
|
|
title: `${res.data.msg}`,
|
2025-02-18 13:38:21 +08:00
|
|
|
|
});
|
2024-12-13 15:30:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if (res.statusCode === 401) {
|
|
|
|
|
|
// 2. 401 表示token过期 去往登录页重新登录
|
2025-02-18 13:38:21 +08:00
|
|
|
|
const memberStore = useMemberStore();
|
|
|
|
|
|
memberStore.clearUserInfo();
|
|
|
|
|
|
memberStore.clearToken();
|
2025-02-26 17:18:53 +08:00
|
|
|
|
if (origin.indexOf('ticket') == -1) {
|
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
|
url: '/pages/login/index',
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 宏源
|
|
|
|
|
|
document.addEventListener(
|
|
|
|
|
|
"deviceready",
|
|
|
|
|
|
() => {
|
|
|
|
|
|
navigator.security.closeWindow();
|
|
|
|
|
|
},
|
|
|
|
|
|
false
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-02-18 13:38:21 +08:00
|
|
|
|
reject(res);
|
2024-12-13 15:30:11 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 3. 其他错误
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
|
|
|
|
|
title: '请求错误',
|
2025-02-18 13:38:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
reject(res);
|
2024-12-13 15:30:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
// console.log(res)
|
|
|
|
|
|
},
|
|
|
|
|
|
fail(err) {
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
2025-02-13 16:29:34 +08:00
|
|
|
|
title: '请求失败' + err.errMsg,
|
2025-02-18 13:38:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
console.log(err, '请求失败');
|
|
|
|
|
|
reject(err);
|
2024-12-13 15:30:11 +08:00
|
|
|
|
},
|
2025-02-18 13:38:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|