deming/common/http.interceptor.js
2020-08-07 19:56:47 +08:00

67 lines
2.3 KiB
JavaScript
Raw 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.

const install = (Vue, vm) => {
// 此为自定义配置参数,具体参数见上方说明
Vue.prototype.$u.http.setConfig({
baseUrl: 'https://dmmall.sdbairui.com/api',
loadingText: '努力加载中~',
loadingTime: 800,
// originalData: true
});
// 请求拦截配置Token等参数
Vue.prototype.$u.http.interceptor.request = (config) => {
const token = uni.getStorageSync('token');
config.header.Authorization = 'Bearer' + " " + token;
return config;
}
// 响应拦截,如配置,每次请求结束都会执行本方法
Vue.prototype.$u.http.interceptor.response = (res) => {
if(parseInt(res.errCode) == 0) {
// res为服务端返回值可能有errCoderesult等字段
// 这里对res.result进行返回将会在this.$u.post(url).then(res => {})的then回调中的res的到
// 如果配置了originalData为true请留意这里的返回值
return res;
} else if(res.errCode == 401) {
// 假设201为token失效这里跳转登录
// vm.$u.toast('您还没有登录哦,请先去登录!');
if (res.data.action != "memberinfo") {
uni.showModal({
title: "温馨提示",
content: "您还未登录,请立即登录",
cancelText: "以后再说",
confirmText: "立即登录",
confirmColor: "#FF780F",
success(res) {
// console.log(res);
if (res.confirm) {
setTimeout(() => {
// 此为uView的方法详见路由相关文档
vm.$u.route('/pageA/login/login')
}, 500)
}
if (res.cancel) {
}
}
})
}
return false;
} else if (res.errCode == 1) {
console.log(res.message);
return res;
} else {
// 如果返回false则会调用Promise的reject回调
// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中res为服务端的返回值
return res;
}
}
}
export default {
install
}