官方文档
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
小程序登录
小程序可以通过微信官方提供的登录能力方便地获取微信提供的用户身份标识,快速建立小程序内的用户体系。
登录流程时序
说明:
- 调用wx.login()获取临时登录凭证code,并回传到开发者服务器。
- 调用auth.code2Session接口,换取用户唯一标识 OpenID和会话密钥 session_key。
之后开发者服务器可以根据用户标识来生成自定义登录态,用于后续业务逻辑中前后端交互时识别用户身份。
注意:
-
会话密钥
session_key
是对用户数据进行加密签名的密钥。为了应用自身的数据安全,开发者服务器不应该把会话密钥下发到小程序,也不应该对外提供这个密钥。 - 临时登录凭证 code 只能使用一次
小程序的登录总结
1 小程序端执行wx.login()获取code
2 将1中的code发送到后端,后端调用auth.code2Session这个接口,得到openid和session_key
3 自定义登入状态,我们生成一个key与openid和session_key相绑定。把key返回到小程序中
4 小程序端保存,然后下次请求需要登入的接口的时候,把key带上。
案例
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# app.js的onLaunch中 因为登录是用户感觉不到的 App({ onLaunch: function () { let that = this / / 登录 wx.login({ success: res = > { / / 发送 res.code 到后台换取 openId, sessionKey, unionId console.log(res.code) / / 拿到code wx.request({ url:that.globalData.baseurl + "/login/" , data:{ 'code' :res.code}, method: 'POST' , success(e){ console.log(e.data) / / wx.setStorageSync( "token" , e.data.data.token) } }) } }), globalData: { userInfo: null, baseurl: "http://127.0.0.1:8000" } # dango后台中 # urls.py 路由中 path( 'login/' , user.Login.as_view()), # user.py中 from rest_framework.views import APIView from rest_framework.response import Response from api.wx import wx_login import hashlib, time from django.core.cache import cache from api.models import Wxuser class Login(APIView): def post( self , request): param = request.data print (request.data) if not param.get( 'code' ): return Response({ "status" : 1 , "msg" : '缺少参数' }) else : code = param.get( "code" ) user_data = wx_login.get_login_info(code) if user_data: val = user_data[ 'session_key' ] + "&" + user_data[ 'openid' ] md5 = hashlib.md5() md5.update( str (time.clock()).encode( "utf-8" )) key = md5.hexdigest() cache. set (key, val) # 放进内存redis库中,把key传给前端当token.下次前台带着key就能拿到val has_user = Wxuser.objects. filter (openid = user_data[ 'openid' ]).first() # 判断openid是否存在 if not has_user: Wxuser.objects.create(openid = user_data[ 'openid' ]) return Response({ "status" : 0 , "msg" : "ok" , "data" : { "token" : key} }) else : return Response({ "status" : 2 , "data" : "无效的code" }) # models.py中: from django.db import models class Wxuser(models.Model): id = models.AutoField(primary_key = True ) openid = models.CharField(max_length = 255 ) name = models.CharField(max_length = 50 ) avatar = models.CharField(max_length = 200 ) language = models.CharField(max_length = 50 ) province = models.CharField(max_length = 50 ) city = models.CharField(max_length = 50 ) country = models.CharField(max_length = 50 ) #gender = models.CharField(max_length=50) creat_time = models.DateTimeField(auto_now_add = True ) update_time = models.DateTimeField(auto_now = True ) def __str__( self ): return self .openid # settings.py中:自己注册的微信小程序信息 AppId = "" AppSecret = "" code2Session = "https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code" # wx_login.py中: from api.wx import settings import requests def get_login_info(code): # 拼接路径 code_url = settings.code2Session. format (settings.AppId,settings.AppSecret,code) response = requests.get(code_url) # 返回的是json数据 json_response = response.json() # 把json数据转换为字典 if json_response.get( 'session_key' ): return json_response else : return False # cache缓存配置redis数据库 |
以上就是python代码实现小程序登录流程时序总结的详细内容,更多关于python小程序登录时序总结的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/guyouyin123/p/12482362.html