0


uniapp全局拦截之uni.addInterceptor

这个就很让人无语。。。。试了几次发现首次进入页面不拦截,准备做一个uniapp一进来判断授权的情况,但是这个只有第一次之后才会触发(因为我做的是微信公众号的H5页面的分享出去所以会需要首次进入拦截,如果对于首次登录拦截没有要求的可见最下边代码)。。分了两种情况

  1. 在需要分享出去的页面onload中做了是否有token的判断
onLoad(e) {
            console.log(333)
            console.log(e, 'eeeeeeejinxing')
            console.log(e.myid, 'e.myid')
            console.log(e.activityId, 'e.activityId')
            console.log(e.id)
            if (e.myid || e.activityId) {
                this.shareId = e.myid;
                let token = localStorage.getItem('token');
                console.log("1111");
                console.log(token);
                if (token) {
                    console.log('token')
                    this.user(e.activityId)
                } else {
                    console.log('notoken')
                    this.getWechatCode(e.activityId);
                }
                console.log('1.111')
                // this.being(e.activityId)        
                return
            } else {
                console.log('2222')
                // this.uid = JSON.parse(localStorage.getItem('userInfo')).id
                // console.log(this.uid, 'uididddd')
                this.being(e.id)
                this.activityId = e.id;
            }
            // this.share()
            //uid分享出去的人的id id是活动id 
},
  1. 在app.vue中的onLaunch中以下代码。直接一进页面判断有没有token 没有就强制跳转到首页(我的获取微信授权的在首页获取 没有做登录页)跳转需要区分是否是tabbar页面
let token = uni.getStorageSync('token')
            if (token) {

            } else {
                uni.reLaunch({
                    url: "/pages/tab/index",
                    success: () => {

                    }
                })
}

其实还有好几种 。比如vuex ,或者把登录的代码片段封装起来 在main.js中mixin混入。不过这两种都还没试。其实这两种还是不如路由拦截来的方便。。。但是这个uniapp的拦截器很鸡肋,而且用不了vue的router.beforeEach......

===================================

其实如果对首次登录拦截没有要求的话。可在App.vue的onLaunch中写,

(也是用的别人代码 侵权联系我 删)

let needLogin = [
                "/pages/tab/index",
]
            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 = localStorage.getItem('token')
                        //获取当前页面路径(即url去掉"?"和"?"后的参数)
                        console.log(token,'router index token')
                        const url = e.url.split('?')[0]
                        console.log(url,'router index url')

                        console.log(needLogin.includes(url))
                        //判断要打开的页面是否需要验证登录
                        if (needLogin.includes(url) && token == '') {
                            uni.showToast({
                                title: '该页面需要登录才能访问,请先登录',
                                icon: 'none'
                            })
                            uni.navigateTo({
                                url: "/pages/login/login"
                            })
                            return false
                        }

                        return true
                    },
                    fail(err) { // 失败回调拦截 
                        console.log(err);
                    },
                })
})

也可以新建个router/index.js文件写。在main.js中引入

标签: 前端 vue.js

本文转载自: https://blog.csdn.net/keaicll/article/details/129670953
版权归原作者 keaicll 所有, 如有侵权,请联系我们删除。

“uniapp全局拦截之uni.addInterceptor”的评论:

还没有评论