methods: {
getCodeApi(state){//获取code
let urlNow=encodeURIComponent(window.location.href);
let scope='snsapi_userinfo'; //snsapi_userinfo //静默授权 用户无感知
let appid='wxf5a9a32471220846';
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);
},
getUrlKey(name){//获取url 参数
return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(location.href)||[,""])[1].replace(/\+/g,'%20'))||null;
}
},
created(){//返回值
let code=this.getUrlKey("code");
if(code){
this.$axios.get("/Wxopenid/getUserInfo?code="+code)
.then((res)=>{
console.log(res);
})
}else{
this.getCodeApi("123");
}
}
vue 授权获取微信openId
最新推荐文章于 2024-06-23 23:51:42 发布