Login applet

Login applet

Official Documents

Description:

  1. Call wx.login () to obtain temporary registration certificate code , and the developers back to the server.
  2. The transmission to the rear end of the 1 code, call auth.code2Session interface to exchange user OpenID unique identification and session key session_key of .
  3. Custom login state, generating a key character string and openid session_key phase bound key returns the applet to
  4. Save small program end, then the next request login interface when necessary, to bring the key

note:

  1. The session key session_keyis user data encrypted signature key. In order to apply their own data security, server developers should not be a session key issued to a small program, it should not be provided outside the key .
  2. Temporary registration certificate code can only be used once


Custom login status

//app.js
App({
  onLaunch: function () {
    let that = this
    // 登录
    // wx.login() 获取 code
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        // console.log(res)
        wx.request({
          url: that.globalData.baseurl+"login/",
          data:{"code":res.code},
          method:"POST",
          success(e){
            // console.log(e)
            wx.setStorageSync('token', e.data.data.token)
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    baseurl: "http://127.0.0.1:8000/"
  }
})

The back-end, the new folder wx, create settings.py file and folder wx_loging.py in wx

settings.py

AppId = 'wxb662c1fdacbcb9426'
AppSecret = '98e19d90eb1169a50e714122edd09b316'

code2Session = 'https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code'

wx_loging.py

from app01.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_response = response.json()
    if json_response.get('session_key'):
        return json_response
    else:
        return False

views.py

class Login(APIView):
    def post(self, request):
        param = 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)
            # {'session_key': '0ghT3CyfdxXrT4FF6vaowQ==', 'openid': 'o_36qT4bcvwdtarkY4pzl0PfE35E'}
            if user_data:
                val = user_data['session_key'] + '&' + user_data['openid']
                md5 = hashlib.md5()
                md5.update(str(time.clock()).encode('utf-8'))
                md5.update(user_data['session_key'].encode('utf-8'))
                key = md5.hexdigest()
                cache.set(key, val)
                has_user = Wxuser.objects.filter(openid=user_data['openid']).first()
                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, "msg": '无效的 code'})

Custom login state, generating a key character string and openid session_key phase bound key returns the applet to

Save small program end, then the next request login interface when necessary, to bring the key

Guess you like

Origin www.cnblogs.com/kai-/p/12470741.html