微信小程序openid获取-django后台

微信端代码:

wx.request({
            //将code作为参数code为wx.login中获得的code
            url: app.data.surl+'/student/getUserInformation',
            data: { 
              'code': code
            },
            method: 'POST',
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              console.log(res)
              //请求成功返回openid
              if (res.statusCode == 200) {
                app.data.user['openid']=res.data['openid']
              } else {
                console.log(res.errMsg)
              }
              wx.request({
                //将openid作为参数访问登录接口
                url: app.data.surl + '/student/login',
                method: 'POST',
                header: {
                  'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: {
                  'openid': app.data.user['openid']
                },
                success: function (res) {
                  console.log(res.data)
                  if(res.data['userid']!='false'){
                    app.data.user['userid']=res.data['userid']
                    wx.showToast({
                      title: '登录成功',
                      icon: 'success',
                      duration: 2000,
                      success: function () {
                        wx.switchTab({
                          url: '../index/index',
                          success: function (res) { },
                          fail: function (res) { },
                          complete: function (res) { },
                        })
                      }
                    })
                  }
                }
              })
            },
          })

django后台端代码:

def userInfo(request):
    code=request.POST.get('code')
    url = "https://api.weixin.qq.com/sns/jscode2session?appid=******&secret=******&js_code=%s&grant_type=authorization_code" %code
    result = requests.get(url).content.decode("utf-8")
    result=loads(result)['openid']
    data={}
    data['openid']=result
    #print(data)
    return ht(dumps(data))

你可能感兴趣的:(微信小程序-Django)