Files
RentWeAppFront/common/http.interceptor.js

95 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-11-14 11:39:33 +08:00
// 这里的vm就是我们在vue文件里面的this所以我们能在这里获取vuex的变量比如存放在里面的token
// 同时我们也可以在此使用getApp().globalData如果你把token放在getApp().globalData的话也是可以使用的
import config from "./config.js" // 全局配置文件
const install = (Vue, vm) => {
Vue.prototype.$u.http.setConfig({
// baseUrl打包app时放开h5模式下会和vue.config.js代理冲突导致失效
baseUrl: config.baseUrl,
2026-05-14 14:42:51 +08:00
originalData: true,
2026-01-15 17:18:24 +08:00
timeout: 10000,
2025-11-14 11:39:33 +08:00
});
// 请求拦截配置Token等参数
Vue.prototype.$u.http.interceptor.request = (config) => {
2026-01-15 17:18:24 +08:00
// 1⃣ 获取本地 userInfo
const userInfo = uni.getStorageSync('userInfo') || {};
// 2⃣ 提取 userType
2026-05-14 14:42:51 +08:00
const userType = userInfo.userType;
config.header = {
...config.header, // ⭐ 保留外部传入的 header
'content-type': 'application/json',
...(userType ? { 'USERTYPE': userType } : {}) // 有 userType 才加
}
return config;
2025-11-14 11:39:33 +08:00
};
// 响应拦截,判断状态码是否通过
2026-06-03 16:02:11 +08:00
// 防止多次跳转登录页
let isRedirectingToLogin = false;
2025-11-14 11:39:33 +08:00
Vue.prototype.$u.http.interceptor.response = (res) => {
// 如果把originalData设置为了true这里得到将会是服务器返回的所有的原始数据
// 判断可能变成了res.statueCode或者res.data.code之类的请打印查看结果
2026-01-15 17:18:24 +08:00
if(res.statusCode == 200) {
2026-06-03 16:02:11 +08:00
return res.data;
2026-01-15 17:18:24 +08:00
} else if(res.statusCode == 301) {
2025-11-14 11:39:33 +08:00
vm.$u.toast('警告:' + res.msg);
return false;
2026-05-14 14:42:51 +08:00
} else if(res.statusCode == 401 || res.statusCode == 403) {
2026-06-03 16:02:11 +08:00
// 防止多次跳转
if (isRedirectingToLogin) {
return false;
}
2026-05-14 14:42:51 +08:00
// 判断当前页面是否为登录页
2026-06-03 16:02:11 +08:00
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const currentPath = currentPage ? currentPage.route : '';
if (currentPath !== 'pages-biz/login/login') {
isRedirectingToLogin = true;
// 清除 token 和用户信息
vm.$u.vuex('vuex_token', '');
vm.$u.vuex('vuex_user', {});
uni.removeStorageSync('userInfo');
2026-05-14 14:42:51 +08:00
vm.$u.toast('认证失败,请重新登录');
2026-06-03 16:02:11 +08:00
// 使用 reLaunch 清空页面栈,避免返回时出现问题
uni.reLaunch({
url: '/pages-biz/login/login',
complete: () => {
// 跳转完成后重置标记
setTimeout(() => {
isRedirectingToLogin = false;
}, 1000)
}
});
2026-05-14 14:42:51 +08:00
}
2025-11-14 11:39:33 +08:00
return false;
2026-01-15 17:18:24 +08:00
} else if(res.statusCode == 500) {
vm.$u.toast('请求错误:' + res.msg);
2025-11-14 11:39:33 +08:00
return false;
} else {
// 其他情况都认为是不合法的
vm.$u.toast('警告:' + res.msg);
return false;
}
};
2026-01-15 17:18:24 +08:00
2026-05-14 14:42:51 +08:00
// 请求错误拦截(超时/网络异常)
Vue.prototype.$u.http.interceptor.responseError = (err) => {
// err 是 uni.request 的 fail 对象
// 超时一般是 err.errMsg 包含 timeout
if (err.errMsg && err.errMsg.includes('timeout')) {
vm.$u.toast('请求超时,请稍后重试');
} else {
vm.$u.toast('网络异常,请检查网络');
}
return Promise.reject(err);
};
2025-11-14 11:39:33 +08:00
}
export default {
install
2026-05-14 14:42:51 +08:00
}