流程圖:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Development_Guide.html
1.點擊登陸按鈕
let link = location.href;
let uri = encodeURIComponent(link);
window.location.href =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
2.重定向后的頁面
http://sw.juzhonglw.cn/testwxh5/?code=001X7G1w36oG3Y2i111w3wYlPe0X7G1T&state=STATE#/packageBuild/common/projectList
截取出code的值
3.拿取code參數
let href = location.href.split('code=')[1];
if (href) {
let code = href.split('&')[0];
if (code) {
// 通過code 拿取openid、unionid、access_token 等信息
this.aotuLogin(code)
}
}
4.通過剛剛拿到的code,調用接口獲取 openid、unionid、access_token 等信息(后端完成,然后提供接口給前端調用)
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
5.調用微信登錄接口,提供 openid、unionid 給后端
request('/user/login',{
openid,unionid
}).then(d=>{
...
})