Applet login flow micro-channel
A. First of all front-end to back-end first pass code
wx.login({
success(res) {
if (res.code) {
//发起网络请求
wx.request({
url: app.globalData.host+'login',
method:"post",
data: {
code: res.code
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
Two rear end received request code
#.......省略一下配置路由啥的
class Login(APIView):
def post(self,request):
code = request.data.get('code')
print(code,type(code))
#061HMtlG0hAQ6d2hOYkG0DlhlG0HMtlh <class 'str'>
III. After the back-end code initiates a request to obtain access to relevant parameters to the official micro letter
Sponsored links
GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
Request parameter
Attributes | Types of | Defaults | Mandatory | Explanation |
---|---|---|---|---|
appid | string | Yes | Applet appId | |
secret | string | Yes | Applet appSecret | |
js_code | string | Yes | Log in getting the code | |
grant_type | string | Yes | Authorization type, where just fill authorization_code |
return value
Object
Packet returned JSON
Attributes | Types of | Explanation |
---|---|---|
openid | string | Uniquely identifies the user |
session_key | string | Session key |
unionid | string | User a unique identifier of an open platform, will return while meeting UnionID issued conditions, see UnionID mechanism explained . |
errcode | number | error code |
errmsg | string | Error Messages |
Errcode of legal values
value | Explanation | Minimum version |
---|---|---|
-1 | The system is busy at this time, please try again later developers | |
0 | Request successful | |
40029 | code invalid | |
45011 | Frequency limit, each user 100 beats per minute |
Following the above content we spliced into the path he needs
import requests
class Login(APIView):
def post(self,request):
code = request.data.get('code')
#https: // api.weixin.qq.com / sns / jscode2session?appid = {} & secret ={} & js_code = {} & grant_type = authorization_code
url= f"https://api.weixin.qq.com/sns/jscode2session?appid=xxxx&secret=xxxx&js_code={code}&grant_type=authorization_code"
#appid与secret不同的开发者不一样
reponse=requests.get(url)
#因为返回值是json的形式
data=reponse.json()
print(data)
appid you see small program which is set in appid
For there is a corresponding secret appid
Where appid
and secret
are you in小程序账号中
IV. After obtaining the encryption process parameters for the background parameters
import hashlib
class Login(APIView):
........
if data.get("openid") and data.get("session_key"):
md5 = hashlib.md5()
md5.update(data['openid'].encode("utf-8"))
md5.update(data['session_key'].encode("utf-8"))
key = md5.hexdigest()
val =data['session_key']+"&"+data['openid']
print(key,val)
V. If the above process is successful, it returns a distal end to facilitate subsequent identification operation
from rest_framework.response import Response
from django.core.cache import cache
class Login(APIView):
........
cache.set(key,val) #存缓存中较少对于数据库的压力
has_user=models.Wxuser.objects.filter(openid=data['openid']).first() #将唯一标识进行存储
if not has_user:
models.Wxuser.objects.create(openid=data['openid'])
return Response({
"code": 200,
"msg": "ok",
"data":{'login_key':key} #发送login_key其目的是为了后续可以直接去缓存取对于信息
})
VI. The front end of the front without any problem for receiving identification information
The purpose of identifying already signed and obtain identification to facilitate subsequent lookups for user information
var that = this
wx.login({
success(res) {
if (res.code) {
//发起网络请求
wx.request({
url: app.globalData.host + 'login',
method: "post",
data: {
code: res.code
},
success(res) {
console.log(res.data.data.login_key)
console.log(that)
that.setData({
login_key: res.data.data.login_key
}
)
},
})
}else {
console.log('登录失败!' + res.errMsg)
}
}
})
其中setData
Method must target the entire page