In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the "how to use python code to achieve Mini Program login process timing" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "how to use python code to achieve Mini Program login process timing" article can help you solve the problem.
Login on Mini Program
Login process timing
Description:
Call wx.login () to get the temporary login credential code and pass it back to the developer server.
Call the auth.code2Session API in exchange for the user's unique ID OpenID and session key session_key.
After that, the developer server can generate a custom login state according to the user identity, which can be used to identify the user during the front-end interaction in the subsequent business logic.
Note:
The session key session_key is a key that encrypts and signs user data. In order to apply its own data security, the developer server should not send the session key to Mini Program, nor should it provide this key to the public.
Temporary login credentials code can only be used once
Login summary of Mini Program
1. Execute wx.login () on Mini Program to obtain code
2 send the code in 1 to the backend, and the backend calls the interface auth.code2Session to get openid and session_key
3 Custom login status, we generate a key to bind with openid and session_key. Return key to Mini Program
4 save it on Mini Program, and the next time you request an API to log in, bring key with you.
In case # app.js 's onLaunch, because login is App that the user cannot feel ({onLaunch: function () {let that = this / / login wx.login ({success: res = > {/ / send res.code to the background in exchange for openId, sessionKey, unionId console.log (res.code) / / get 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 backend # path in urls.py routing ('login/', user.Login.as_view ()), from rest_framework.views import APIViewfrom rest_framework.response import Responsefrom api.wx import wx_loginimport hashlib in # user.py, timefrom django.core.cache import cachefrom api.models import Wxuserclass Login (APIView): def post (self) "msg": 'missing parameter'}) 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 () Val) # put it into the memory redis library and pass the key to the front end as token. Next time you can get val has_user = Wxuser.objects.filter (openid=user_data ['openid']). First () # with key at the front desk to determine whether if not has_user exists in openid: Wxuser.objects.create (openid=user_data [' openid']) return Response ({"status": 0) "msg": "ok", "data": {"token": key}} else: return Response ({"status": 2 "data": "invalid code"}) # models.py: from django.db import modelsclass 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): in return self.openid# settings.py: self-registered WeChat Mini Programs information AppId= "AppSecret="code2Session=" https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}& Settings.AppSecret,code) response = requests.get (code_url) # returns json data json_response = response.json () # converts json data into a dictionary if json_response.get ('session_key'): return json_response else: return False# cache cache configuration redis database on "how to use python code to implement the timing of the Mini Program login process". That's it. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.