使用场景:用户在App内分享网页至微信,用户使用微信浏览器打开,唤起微信公众号授权获取code
登录接口没使用微信的,使用的是我们自己的登录接口,而code是必须参数。
参考文档:微信开放文档-网页授权
步骤1: 获取code
模版在开发文档中,只需要将appid、回调地址换成自己的。scope按需要来确定。
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxcdbe7983be8fb746&redirect_uri=' + encodeURIComponent(window.location.href.split('/#')[0]) + '&response_type=code&scope=snsapi_userinfo#wechat_redirect'
注意点:redirect_uri需要使用 urlEncode 对链接进行处理。
步骤2: 获取token
axios.post(
{ code: window.location.href.split('code=')[1].split('&state=')[0], loginType: 2 },
{
headers: {
'content-type': 'application/json'
}
}
).then(res => {
})
//res.data中拿到token
再使用token在自己的接口中,作为权限判别。