uniapp微信公众号h5页面需要获取用户的code,将获取方法记录一下。
请求地址获取code
getWechatCode() {
//跳转至微信官方进行获取code,地址中需要填写appid
window.location.href =
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + configs.appid + '&redirect_uri=' +
encodeURIComponent(configs.uri) + '&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
}
获取回调code
console.log(this.getUrlParam('code'))
getUrlParam(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
let url = window.location.href.split('#')[0];
let search = url.split('?')[1];
console.log(search);
if (search) {
var r = search.substr(0).match(reg);
if (r !== null) {
return unescape(r[2]);
}
return null;
} else return null;
},