50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
import {
|
|
getToken
|
|
} from "./auth"
|
|
export default function initApp() {
|
|
/**
|
|
* 页面跳转拦截器
|
|
*/
|
|
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
|
|
list.forEach(item => { //用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
|
|
uni.addInterceptor(item, {
|
|
invoke(e) { // 调用前拦截
|
|
//获取用户的token
|
|
const token = getToken();
|
|
|
|
const url = e.url.split('?')[0];
|
|
let notNeed = ["/pages/login/login"].includes(url)
|
|
// 如果在whiteList里面就不需要登录
|
|
if (notNeed) {
|
|
return e
|
|
} else {
|
|
//需要登录
|
|
if (token == undefined || token == '') {
|
|
uni.showToast({
|
|
title: '请先登录',
|
|
icon: 'none'
|
|
})
|
|
uni.reLaunch({
|
|
url: "/pages/login/login"
|
|
})
|
|
return false
|
|
} else {
|
|
return e
|
|
}
|
|
}
|
|
|
|
|
|
},
|
|
fail(err) { // 失败回调拦截
|
|
console.log(err);
|
|
// uni.showModal({
|
|
// content: JSON.stringify(err),
|
|
// showCancel: false
|
|
// });
|
|
|
|
}
|
|
})
|
|
})
|
|
|
|
|
|
} |