Commit 3b4ddfda authored by Facius's avatar Facius

Merge branch 'dev-v1.0.0' of https://git.banshouhui.com/wuzhenzhe/study into dev-v1.0.0

parents aec1f34b 39a0da0c
const globalUrl = process.uniEnv || {} const globalUrl = process.uniEnv || {};
// 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token变量 // 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token变量
const install = (Vue, vm) => { const install = (Vue, vm) => {
const systemApiUrl = globalUrl.apiUrl const systemApiUrl = globalUrl.apiUrl;
// 此为自定义配置参数,具体参数见上方说明 // 此为自定义配置参数,具体参数见上方说明
Vue.prototype.$u.http.setConfig({ Vue.prototype.$u.http.setConfig({
baseUrl: systemApiUrl, baseUrl: systemApiUrl,
...@@ -11,7 +11,7 @@ const install = (Vue, vm) => { ...@@ -11,7 +11,7 @@ const install = (Vue, vm) => {
}, },
loadingTime: 0, loadingTime: 0,
originalData: true, originalData: true,
}) });
// 请求拦截,配置Token等参数 // 请求拦截,配置Token等参数
Vue.prototype.$u.http.interceptor.request = (config) => { Vue.prototype.$u.http.interceptor.request = (config) => {
...@@ -28,40 +28,40 @@ const install = (Vue, vm) => { ...@@ -28,40 +28,40 @@ const install = (Vue, vm) => {
// } // }
// 最后需要将config进行return // 最后需要将config进行return
return config return config;
// 如果return一个false值,则会取消本次请求 // 如果return一个false值,则会取消本次请求
// if(config.url == '/user/rest') return false; // 取消某次请求 // if(config.url == '/user/rest') return false; // 取消某次请求
} };
// 响应拦截,判断状态码是否通过 // 响应拦截,判断状态码是否通过
Vue.prototype.$u.http.interceptor.response = (res) => { Vue.prototype.$u.http.interceptor.response = (res) => {
const data = res.data const data = res.data;
if (res.statusCode === 200) { if (res.statusCode === 200) {
if (data.code === 0) { if (data.code === 0) {
return data.data return data.data;
} else if (data.code === 401) { } else if (data.code === 401) {
vm.$u.toast(data.msg) vm.$u.toast(data.msg);
let pages = getCurrentPages() let pages = getCurrentPages();
let route = pages[pages.length - 1].route let route = pages[pages.length - 1].route;
// 如果不是在登录页,则跳转到登录页 // 如果不是在登录页,则跳转到登录页
if (route !== 'pages/login/index') { if (route !== 'pages/login/index') {
setTimeout(() => { setTimeout(() => {
vm.$u.route('/pages/login/index') vm.$u.route('/pages/login/index');
}, 1500) }, 1500);
} }
return false return false;
} else { } else {
// 如果返回false,则会调用Promise的reject回调, // 如果返回false,则会调用Promise的reject回调,
vm.$u.toast(data.msg) vm.$u.toast(data.msg);
return false return false;
} }
} else { } else {
// HTTP状态非200 // HTTP状态非200
vm.$u.toast(data.msg || '服务器错误') vm.$u.toast(data.msg || '服务器错误');
} }
} };
} };
export default { export default {
install, install,
} };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment