uni 微信公众号授权
调转网址 需要appid local 本地地址需要 encodeURIComponent 后面的参数则参照图片 得到code后 跳入else 请求后台完成登录
let appid = ""; //为测试号id
let code = getUrlParam("code"); //是否存在code
let local = window.location.href;
if (code == null || code === "") {
//不存在就打开上面的地址进行授权
window.location.href =`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
}
else {}
截取地址中的 code值
// 判断公众号截取code
const getUrlParam = (name) => {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
let r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}