CodeBtn(){
let _this=this;
let appid = "xxxxxxxxx"; //公众平台appid
const code = _this.getUrlParam('code'); //是否存在code
let local = encodeURIComponent(window.location.href);//获取当前url
if (code == null || code === "") {
//不存在就打开上面的地址进行授权
window.location.href =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${local}&response_type=code&scope=snsapi_userinfo&state=wecedirect`;
}else{
_this.code = code;
_this.getOpenId(code);
}
},
},
// 获取url地址栏参数
getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var rrr = decodeURIComponent(window.location.search);
var r = rrr.substr(1).match(reg);
if(r != null) return unescape(r[2]);
return null;
},
getOpenId(){
// 修改url参数以及设置参数
window.history.replaceState({}, '','http://www.wkjiankang.com');
}
网友评论