核心:
const whiteList=[
'/pages/index/index',
'/pages/login/login'
]
const loginPage="/pages/login/login";
export function initPermission(){
console.log("initPermission:",)
const routeFnList=["navigateTo","redirectTo","reLaunch","switchTab"]
routeFnList.forEach(item=>{
uni.addInterceptor(item,{
invoke(e){
console.log("invoke:")
const token=uni.getStorageInfoSync("token");
const url=e.url.split("?")[0];
console.log("url:",url)
let notNeed=whiteList.includes(url)
if(notNeed){
return e
}else{
console.log("token:",token)
if(token.keys.length==0){
uni.showToast({
title:"请登录",
icon:"none"
})
uni.navigateTo({
url:loginPage
})
return false
}else{
return e
}
}
},
success(res){
console.log("invoke-success",res)
},
fail(err){
console.log("err",err)
}
})
})
}
第二步:在app.vue中的onlaunch调用该函数
import {initPermission} from "./permission.js"
export default {
onLaunch: function() {
console.log('App Launch')
initPermission()
},
}