微信公众号加载页面,获取code ,页面会闪三次才跳转页面
const {code = ''} = qs.parse(window.location.search); // 获取当前页面地址中的code参数的值
const local = (window.location.href); // 对当前地址用encodeURIComponent进行编码
if (!code) {
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8bb74a6d328b1f15&redirect_uri=' + encodeURIComponent(local, "UTF-8") + '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'
} else {
_this.weixinCode = code; // 能拿到code,说明用户已同意授权,拿到code
if (code != '' && code != null && code != undefined ) {
_this.$api.code(code).then((res) => {if (res.code == 0) { // 没有注册,跳转到注册页面
if (res.type == 1) {
_this.$router.push({
name: 'regist',
query: {
openid: res.openid,
unionid: res.unionid
}
})
} else if (res.type == 2) { // 已经注册过,跳转到个人中心
window.sessionStorage.setItem('token', res.token)
_this.$router.push({
name: 'member'
})
}
} else {
}
}).catch((res) => {
})
}
}
第一次,进你的页面,发现你没有code,然后要跳转授权的长链接,长链接访问完之后,最终又跳转你的页面,你数数,1,2,3