授權登錄,首先登錄微信公眾號
網頁服務 - 網頁授權獲取用戶基本信息 --修改
微信網頁授權
具體而言,網頁授權流程分為四步:
1、引導用戶進入授權頁面同意授權,獲取code
2、通過code換取網頁授權access_token(與基礎支持中的access_token不同)
3、如果需要,開發者可以刷新網頁授權access_token,避免過期
4、通過網頁授權access_token和openid獲取用戶基本信息(支持UnionID機制)
獲取code, 可以直接通過網頁獲取
判讀當前頁面 URL是否存在code
1、請求URL:
https://www.test.com/home/index.html
2、判讀code是否存在
if(!code){
location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx27ecccc5ec1f7a&redirect_uri=https://www.test.com/home/index.html&response_type=code&scope=snsapi_userinfo#wechat_redirect';
}
3、成功后,回調URL會攜帶code
https://www.test.com/home/index.html?code=xxxxxxxxxxxxxxxxxxx
4、把code傳給服務器,后續操作