微信小程式登入流程
阿新 • • 發佈:2021-01-19
一、微信介面
GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
文件: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
二、獲取openId
1. 前端通過wx.login介面獲得臨時登入憑證 code
2. 服務端呼叫微信code2Session介面,使用前端傳入的引數jscode,後臺使用配置好的appid和secret請求微信介面
3. 此時一定會拿到openId,但不一定能拿到unionId和session_key
unionId機制說明文件:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html
三、獲取unionId和頭像暱稱
1. 使用者授權後,再次呼叫該微信介面,可以獲取unionId和session_key
2. 前端傳入encryData加密資料和iv,服務端使用session_key和iv,解密前端傳過來的加密資料,得到頭像、暱稱
文件:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html