Vue公眾號配置微信授權(路由采用history模式)


created() {
let vm = this;
/**如果掃碼進入,后端會在地址傳openid;
* */
if (this.$route.query.openid !== "" || Cookies.get('SESSION')) {
return
}
let code=this.getUrlKey("code");
if (!code) {
this.getCodeApi("123");
} else {
  /*調取接口查看是否登錄*/
userLogin({code:code}).then((res) => {
window.localStorage.setItem("loginState", '1');
}).catch((err)=> {
/*說明沒登錄*/
window.localStorage.setItem("loginState", '0');
vm.$_toast(err.message)
})
}

}
getUrlKey(name){// 獲取url 參數
return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(location.href)||[,""])[1].replace(/\+/g,'%20'))||null;
}
getCodeApi(state){// 獲取code
let urlNow=window.location.href;
let scope='snsapi_base'; // snsapi_userinfo //靜默授權 用戶無感知
let appid=appId;
let url=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
window.location.replace(url);
}


免責聲明!

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



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