阅读背景:

项目中遇到的问题总结(第一个项目)

来源:互联网 

1.授权绑定微信公众号(微信网页授权)

mounted:function() {
    var uid = tools.cookie.get('id') //cookie缓存微信id
    if(uid == undefined ){//判断是否登录
        var _this = this;
        var data = tools.getHrefData();
        var code = data.code;
        if(code){
            _this.getWxInfo(code,function(wxInfo){

                    tools.ajax(  //tools  封装的函数
                        server.wx_register, //上传微信号
                        {
                            data:{
                                'wx_code': wxInfo.openid,
                                sname: wxInfo.nickname,
                                sex: wxInfo.sex,
                                headimgurl: wxInfo.headimgurl
                            },
                            success:function(res){
                                if(res && res.retcode == 200){
                                    
                                    tools.cookie.set('data',JSON.stringify(res.data))
                                    var data=JSON.parse(tools.cookie.get('data'))
                                    
                                    var userid= data.id;
                                    tools.cookie.set('id',userid)
                                    var id = tools.cookie.get('id')
                                    


                                }else{
                                    _this.$vux.toast.show({
                                            type: 'text',
                                            text: res.msg
                                    })
                                }
                            }
                        }
                    )

            });
        }
        else {//微信网页授权
             var weixinUrl = '';
              weixinUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa0da85c9b3d8682d&redirect_uri=https://www.yiyisoft.net/ysxdwy/Public/www/index.html&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
              window.location.href = weixinUrl;
        }
        }else{
            return false;
        }           
},
methods: {
    getWxInfo:function(code,fn){
            var _this = this;
            tools.ajax(server.wx_info,{ // server.wx_info  后端接口
                data:{
                    code:code,
                    type: 'weixin',
                    isWeChat: true
                },
                success:function(res){
                    if(res.code == 1){
                        var wxInfo = JSON.parse(res.data);
                        fn&&fn(wxInfo);
                    }else{
                        _this.$vux.toast.show({//_this.$vux.toast.show  vux里的toast
                            type: 'text',
                            text: res.msg
                        })
                    }
                },
                error:function(){
                    _this.$vux.toast.show({
                            type: 'text',
                            text: "微信授权失败"
                    })
                }
            })
    }
}mounted:function() {
    



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: