一、先来看看效果
接口请求返回的数据:
二、官方登录流程图
三、小程序登录流程梳理:
1、小程序端调用wx.login
2、判断用户是否授权
3、小程序端访问 wx.getuserinfo
4、小程序端js代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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 () { } //请求完成后执行的函数
})
}
})
}
}
})
}
})
|
5、后端服务器访问code2session,通过code2session这个api接口来获取真正需要的微信用户的登录态session_key
和 openid
和 unionid
6、后端服务器校验用户信息,对encrypteddata
解密
微信小程序登录后获得session_key后,返回了encrypteddata,iv的数据,其中encrypteddata解密后包含了用户的信息,解密后的json格式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
{
"openid" : "openid" ,
"nickname" : "nickname" ,
"gender" : gender,
"city" : "city" ,
"province" : "province" ,
"country" : "country" ,
"avatarurl" : "avatarurl" ,
"unionid" : "unionid" ,
"watermark" :
{
"appid" : "appid" ,
"timestamp" :timestamp
}
}
|
7、新建解密文件——wxbizdatacrypt.py
from crypto.cipher import aes
这边一般会遇到modulenotfounderror:no module named "crypto"
错误
(1)执行pip3 install pycryptodome
(2)如果还是提示没有该模块,那就虚拟环境目录lib—-site-package
中查看是否有crypto
文件夹,这时你应该看到有crypto
文件夹,将其重命名为crypto
即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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 :])]
|
8、flask的/user/wxlogin
api代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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)
|
总结
到此这篇关于python flask微信小程序登录详解及登录api实现的文章就介绍到这了,更多相关python flask微信小程序登录详解及登录api实现内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_33966519/article/details/106545820