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();              //(如果沒有就執行上面的微信授權方法)
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM