hws007
hws007
  • 发布:2022-04-21 22:04
  • 更新:2023-07-12 11:34
  • 阅读:1148

分享拦截路由,权限判断

分类:uni-app
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];  
    list.forEach(item => { //用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器  
        uni.addInterceptor(item, {  
            invoke(e) { // 调用前拦截  
                const {  
                    role,  
                    permission  
                } = uniCloud.getCurrentUserInfo()  
                if (!role.includes('admin') && !permission.includes(e.hasPermission)) {  
                    return uni.showToast({  
                        title: '没有权限!请联系管理员!',  
                        icon: 'none',  
                        position:'center'  
                    })  
                }  
                return e  
            },  
            fail(err) { // 失败回调拦截   
                console.log(err);  

            }  
        })  
    })
uni.navigateTo({  
    url: 'job',  
    hasPermission: 'MenDianYuYuanGong_EDIT'  
});
1 关注 分享
zhaoyu2020

要回复文章请先登录注册

hhyang

hhyang

如果你正在使用 vue3 + vite 请参考 [uni-simple-router v3 路由、拦截、最优雅的解决方案重磅来袭](https://ask.dcloud.net.cn/article/40621),或者查看 [官方文档](https://v3.hhyang.cn/)
2023-07-12 11:34
z***@gmail.com

z***@gmail.com

这种写法对微信小程序的原生tabbar和navigator不起作用吧?
2023-03-24 22:50
zhaoyu2020

zhaoyu2020

楼主 你怎么实现首次打开拦截的
2023-02-24 17:26