Django后台通過微信小程序傳過來的code獲取用戶唯一身份識別openid


一、微信小程序前端
  • app.js
//app.js
App({
  onLaunch: function () {
    this.globalData = {
      userInfo: null,
      serverAddr: 'http://127.0.0.1:8000',
      apiVersion: "/api/v1.0",
      staticDir: "/static",
      authType:4,
      code: ""
    }
    // 登錄
    let that=this;
    wx.login({
      success(res) {
        //js調用登陸命令獲取到code
        if (res.code) {
          that.globalData.code = res.code
          //通過code調用自己服務接口獲取到openid
        } else {
          console.log('登錄失敗!' + res.errMsg)
        }
      }
    })
    //獲取用戶信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              this.globalData.userInfo = res.userInfo
              if (this.userInfoReadyCallback){
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  onShow:function () {
  }
})


// "iconPath": "",
// "selectedIconPath": ""
  • index.js
//index.js
const WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
var gloData = app.globalData

Page({
....
  onLoad: function() {
    // 獲取用戶信息
    var that = this;
    wx.getSetting({
      success: res => {
        //如果經過授權
        if (res.authSetting['scope.userInfo']) {
          this.setData({
            hideBtn: true
          })
          //獲取用戶信息
          wx.getUserInfo({
            success: res => {
              this.data.userInfo = res.userInfo
              this.data.userInfo.code = gloData.code
              wx.request({
                url: gloData.serverAddr + gloData.apiVersion + "/auth/wxLogin/",
                data: that.data.userInfo,
                success: function (res) {
                  if(res.data.userType!=4)
                  {
                    gloData.authType = res.data.userType
                    that.setData({
                      hide: false,
                      authMSG:""
                    })
                  }else{
                    that.setData({
                      authMSG: "您未經杭州研一智控科技公司授權。請聯系研一工作人員給您授權以正常使用本小程序!"
                    })
                  }
                }
              })
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })

        }
      }
    })
    this.getNews("GET",0);
    // let that = this;
    // WxParse.wxParse('content', 'html', that.data.news[0].content, that, 5);
    // console.log(that.data.news[0].content)
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }
  }
)}
  • app.js 中調用wx.login({})獲取獲取登錄code
  • index.js中請求授權,然后獲取用戶信息,並提交后台處理
二、后台獲取openid
  • appid、以及secret通過微信公眾號獲取,然后作者設置在了Django setting中
    APPID = "wx秘密09"
    SECRET = "6c秘密不給看7031d"
import requests

from django.conf import settings


class OpenidUtils(object):

    url = "https://api.weixin.qq.com/sns/jscode2session"
    appid = settings.APPID
    secret = settings.SECRET



    @classmethod
    def get_openid(cls,code):
        url = cls.url + "?appid=" + cls.appid + "&secret=" + cls.secret + "&js_code=" + code + "&grant_type=authorization_code"
        return requests.get(url).json()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM