You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
|
let needLogin = ['/pages/index/index','/pages/handle/handle','/pages/login/login','/pages/sweep/sweep','/pages/networkDetail/networkDetail']
|
|
|
|
|
|
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
|
|
|
|
|
|
|
|
|
|
|
|
list.forEach(item => { //用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
|
|
|
|
|
|
console.log(item,'router list item')
|
|
|
|
|
|
uni.addInterceptor(item, {
|
|
|
|
|
|
invoke(e) { // 调用前拦截
|
|
|
|
|
|
//获取用户的token
|
|
|
|
|
|
// console.log(e,'routerjs invoke')
|
|
|
|
|
|
// const token = uni.getStorageSync('token')
|
|
|
|
|
|
// 暂时将token验证替换手机号
|
|
|
|
|
|
const token = uni.getStorageSync('phoneNumber')
|
|
|
|
|
|
//获取当前页面路径(即url去掉"?"和"?"后的参数)
|
|
|
|
|
|
// console.log(token,'router index token')
|
|
|
|
|
|
const url = e.url.split('?')[0]
|
|
|
|
|
|
// console.log(url,'router index url')
|
|
|
|
|
|
|
|
|
|
|
|
//判断要打开的页面是否需要验证登录
|
|
|
|
|
|
if ( !needLogin.includes(url) && !token) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// uni.showToast({
|
|
|
|
|
|
// title: '该页面需要登录才能访问,请先登录',
|
|
|
|
|
|
// icon: 'none',
|
|
|
|
|
|
// duration:1000
|
|
|
|
|
|
// })
|
|
|
|
|
|
// setTimeout(()=>{
|
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
|
url: "/pages/login/login",
|
|
|
|
|
|
success: (res) => {
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: (res) => {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
// },1500)
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
},
|
|
|
|
|
|
fail(err) { // 失败回调拦截
|
|
|
|
|
|
console.log(err);
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|