微信授权登录

let nowUrl='http://xiaofeng.ckugua.com/index.html';
window.location.href='http://xiaofeng.ckugua.com/webchat/authorize?url='+nowUrl

console.log(nowUrl)

调用授权登录接口,将要授权成功的页面地址传过去,用户点击授权就会返回到该页面

 

let urlCode=this.GetQueryString('code')
console.log(urlCode)
this.GetUserInfo(urlCode)

GetQueryString(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
       console.log(r)
       if (r != null) return unescape(r[2]); return null;
},
GetUserInfo(code){
      let _this=this;
       let url='webchat/userInfo?code='+code
       _this.$Ajax.get(url).then((response)=>{
        	console.log(response.data)
            let res=response.data;
             if(res.code==200){
                 var userid=window.localStorage.getItem(this.GLOBAL.CODE);
                 let openidurl='webchat/wxUser?userid='+userid+'&openId='+res.data.openId+'&nickname='+res.data.name+'&headimgurl='+res.data.headImgUrl;
                        this.GLOBAL.OPENID=res.data.openId;
// {userid:userid,openId:res.data.openId,nickname:res.data.name,headimgurl:res.data.headImgUrl}
                        _this.$Ajax.get(openidurl).then((reslt)=>{
                            console.log(reslt)
                        }).catch((err)=>{
                            console.log(err)
                        })
                    }
 }).catch((errs)=>{

                    console.log(errs)

                })

            },

  

在返回到的页面中写两个函数,GetQueryString(name)为截取当前url地址中的code,

GetUserInfo(code)为向后台传入,获取openid等信息

转载于:https://www.cnblogs.com/WangXinPeng/p/10221831.html

你可能感兴趣的:(微信授权登录)