uni-app进行微信开发,获取微信授权方式

uni-app进行微信开发,获取微信授权方式

created() {
    this.getOpenId();
},

methods: {
    getOpenId() {
        //先判断有没有授权(判断地址栏code,有就-用户同意了授权,没有-没授权或者拒绝授权)
        let code = this.$route.query.code; //此处使用的是history路由模式,hash这么拿不到。
        if (code) {
            // 走后端接口(把code传给后台,让后台根据code获取openID与用户信息)
            uni.request({
                url: "v1/weichat/getInfo",
                data:{
                    "code":code
                },
                method: 'POST',
                success: (res) => {
                    
                }
            })
            
        } else {
            this.getCode();
        }
    },
    getCode() {
        const appid = "wx5*********963";
        const wwwUrl = urlencode("http://xxxx.com/pages/index/login");
        //没授权先授权,授权后会有一个code在地址栏上
        window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + wwwUrl + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    },
},

 

posted @ 2023-05-06 14:24  坏小子1993  阅读(221)  评论(0编辑  收藏  举报