一、先来看看效果
接口请求返回的数据:
二、官方登录流程图
三、小程序登录流程梳理:
wx.login({
success: resp => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
console.log(resp);
var that = this;
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: userResult => {
var platUserInfoMap = {}
platUserInfoMap["encryptedData"] = userResult.encryptedData;
platUserInfoMap["iv"] = userResult.iv;
wx.request({
url: 'http://127.0.0.1:5000/user/wxlogin',
data: {
platCode: resp.code,
platUserInfoMap: platUserInfoMap,
},
header: {
"Content-Type": "application/json"
},
method: 'POST',
dataType:'json',
success: function (res) {
console.log(res)
wx.setStorageSync("userinfo", res.userinfo) //设置本地缓存
},
fail: function (err) { },//请求失败
complete: function () { }//请求完成后执行的函数
})
}
})
}
}
})
}
})
session_key
和 openid
和 unionid
encryptedData
解密{
"openId": "OPENID",
"nickName": "NICKNAME",
"gender": GENDER,
"city": "CITY",
"province": "PROVINCE",
"country": "COUNTRY",
"avatarUrl": "AVATARURL",
"unionId": "UNIONID",
"watermark":
{
"appid":"APPID",
"timestamp":TIMESTAMP
}
}
from Crypto.Cipher import AES
这边一般会遇到ModuleNotFoundError:No module named "Crypto"
错误pip3 install pycryptodome
Lib—-site-package
中查看是否有Crypto
文件夹,这时你应该看到有crypto
文件夹,将其重命名为Crypto
即可import base64
import json
from Crypto.Cipher import AES
class WXBizDataCrypt:
def __init__(self, appId, sessionKey):
self.appId = appId
self.sessionKey = sessionKey
def decrypt(self, encryptedData, iv):
# base64 decode
sessionKey = base64.b64decode(self.sessionKey)
encryptedData = base64.b64decode(encryptedData)
iv = base64.b64decode(iv)
cipher = AES.new(sessionKey, AES.MODE_CBC, iv)
decrypted = json.loads(self._unpad(cipher.decrypt(encryptedData)))
if decrypted['watermark']['appid'] != self.appId:
raise Exception('Invalid Buffer')
return decrypted
def _unpad(self, s):
return s[:-ord(s[len(s)-1:])]
/user/wxlogin
api代码:import json,requests
from WXBizDataCrypt import WXBizDataCrypt
from flask import Flask
@app.route('/user/wxlogin', methods=['GET','POST'])
def user_wxlogin():
data = json.loads(request.get_data().decode('utf-8')) # 将前端Json数据转为字典
appID = 'appID' # 开发者关于微信小程序的appID
appSecret = 'appSecret' # 开发者关于微信小程序的appSecret
code = data['platCode'] # 前端POST过来的微信临时登录凭证code
encryptedData = data['platUserInfoMap']['encryptedData']
iv = data['platUserInfoMap']['iv']
req_params = {
'appid': appID,
'secret': appSecret,
'js_code': code,
'grant_type': 'authorization_code'
}
wx_login_api = 'https://api.weixin.qq.com/sns/jscode2session'
response_data = requests.get(wx_login_api, params=req_params) # 向API发起GET请求
resData = response_data.json()
openid = resData ['openid'] # 得到用户关于当前小程序的OpenID
session_key = resData ['session_key'] # 得到用户关于当前小程序的会话密钥session_key
pc = WXBizDataCrypt(appID, session_key) #对用户信息进行解密
userinfo = pc.decrypt(encryptedData, iv) #获得用户信息
print(userinfo)
'''
下面部分是通过判断数据库中用户是否存在来确定添加或返回自定义登录态(若用户不存在则添加;若用户存在,返回用户信息)
--------略略略略略略略略略-------------
这部分我就省略啦,数据库中对用户进行操作
'''
return json.dumps({"code": 200, "msg": "登录成功","userinfo":userinfo}, indent=4, sort_keys=True, default=str, ensure_ascii=False)