H5获取微信网页授权

//判断是否微信浏览器
isWeiXin: function(){
    let ua = window.navigator.userAgent.toLowerCase();
  return ua.match(/MicroMessenger/i) == "micromessenger";
},
 
//微信授权
weixin(){
if(Util.isWeiXin()){
//在微信中打开
  let banklink = encodeURIComponent(window.location.href);//提交时,网页中

  let link = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx70568d6c7bdeaa58&redirect_uri=' + banklink + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';//测试
 
  window.location.href = link;
}
},
 
//生命周期里的获取code方法
let that=this;
that.code_weixin= Util.UrlSearch('code');
if(that.code_weixin){                  //(如果有这个code则执行下面的)
  localStorage.setItem('code_weixin',that.code_weixin);       //(储存code)
  that.registerForWechatUserByH5({appType:'H5',code:that.code_weixin});      //方法调接口1
    axios.post('/接口地址/',{appType:'H5',code:codeWeixin传参        //直接调用接口2
}).then(()=>{});
}else{
  that.weixin();              //(如果没有就执行上面的微信授权方法)
}
posted @ 2018-07-29 21:27  大姐姐54  阅读(4431)  评论(0编辑  收藏  举报