12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import store from '@/store/index.js';
- import {Router} from '@/libs/router';
- // 页面白名单
- const whiteList = [
- '/pages/login/index',
- '/pages/index/index'
- ];
- /*设置白名单后台可以返回权限,然后再匹配路由地址*/
- let permission=['icon'];//权限数组示例---可以通过store登录的时候缓存
- for(let i=0;i<permission.length;i++){
- let permitName=permission[i];
- let path=Router[permitName].path;
- if(path){
- if(!whiteList.includes(path)){
- whiteList.push(path)
- }
- }
- }
- function hasPermission (url) {
- // 在白名单中或有token,直接跳转
- //whiteList.indexOf(url) !== -1 //使用白名单校验页面的时候使用
- if(store.state.access_token) {
- return true
- }
- return false
- }
-
- uni.addInterceptor('navigateTo', {
- // 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
- invoke (e) {
- if(!hasPermission(e.url)){
- uni.reLaunch({
- url: '/pages/login/index'
- })
- return false
- }
- return true
- },
- success (e) {
- console.log(e)
- },
- fail(e) {
- console.log(e)
- }
- })
- uni.addInterceptor('switchTab', {
- // tabbar页面跳转前进行拦截
- invoke (e) {
- if(!hasPermission(e.url)){
- uni.reLaunch({
- url: '/pages/login/index'
- })
- return false
- }
- return true
- },
- success (e) {
- console.log(e)
- },
- fail(e) {
- console.log(e)
- }
- })
|