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);
}

转载于:https://www.cnblogs.com/wangxinyubokeyuan/p/10511991.html

你可能感兴趣的:(Vue公众号配置微信授权(路由采用history模式))