76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
// 这里的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,
|
||
originalData:true,
|
||
timeout: 10000,
|
||
});
|
||
// 请求拦截,配置Token等参数
|
||
Vue.prototype.$u.http.interceptor.request = (config) => {
|
||
// config.header.Token = '5d33018e653d897fc259b42cf022c1b3';
|
||
// 方式一,存放在vuex的token,假设使用了uView封装的vuex方式,见:https://uviewui.com/components/globalVariable.html
|
||
// 自定义token头
|
||
// 1️⃣ 获取本地 userInfo
|
||
const userInfo = uni.getStorageSync('userInfo') || {};
|
||
|
||
// 2️⃣ 提取 userType
|
||
const userType = userInfo.userType;
|
||
config.header = {
|
||
...config.header, // ⭐ 保留你外部传入的 header
|
||
'content-type': 'application/json',
|
||
...(userType ? { 'USERTYPE': userType } : {}) // 有 userType 才加
|
||
}
|
||
return config;
|
||
};
|
||
// 响应拦截,判断状态码是否通过
|
||
Vue.prototype.$u.http.interceptor.response = (res) => {
|
||
// 如果把originalData设置为了true,这里得到将会是服务器返回的所有的原始数据
|
||
// 判断可能变成了res.statueCode,或者res.data.code之类的,请打印查看结果
|
||
if(res.statusCode == 200) {
|
||
return res.data;
|
||
} else if(res.statusCode == 301) {
|
||
vm.$u.toast('警告:' + res.msg);
|
||
return false;
|
||
} else if(res.statusCode == 401) {
|
||
uni.navigateTo({
|
||
url:'/pages-biz/login/login'
|
||
})
|
||
vm.$u.toast('认证失败,请重新登录')
|
||
return false;
|
||
} else if(res.statusCode == 403) {
|
||
uni.navigateTo({
|
||
url:'/pages-biz/login/login'
|
||
})
|
||
vm.$u.toast('认证失败,请重新登录')
|
||
return false;
|
||
} else if(res.statusCode == 500) {
|
||
vm.$u.toast('请求错误:' + res.msg);
|
||
return false;
|
||
} else {
|
||
// 其他情况都认为是不合法的
|
||
vm.$u.toast('警告:' + res.msg);
|
||
return false;
|
||
}
|
||
};
|
||
|
||
|
||
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);
|
||
};
|
||
}
|
||
|
||
export default {
|
||
install
|
||
} |