Python实现小程序获取openid和电话号码
#获取openid 
#前端代码
wx.login({
	success(response) {
	    _this.util.request(_this.api.GetOpenid, {code: response.code,},'GET').then(res => {
		uni.setStorageSync('openid', res.userInfo.openid);
		uni.setStorageSync('sessionkey', res.userInfo.session_key);
	    })
	}
})
#后端代码
def get_openid_by_code(code):
    url = '[https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js\_code={2}&grant\_type=authorization\_code](https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code)'
    get_url = url.format(小程序appid, 小程序secret, code)
    r = requests.get(get_url)
    return r.json()


#获取电话号码
#前端代码
<button class="confirm-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">立即登录</button> 
getPhoneNumber(e){
    //e.mp.detail.iv, e.mp.detail.encryptedData
    if (e.mp.detail.errMsg == 'getPhoneNumber:fail user deny') return;
    let _this = this;
    var post_data = {
        sessionkey: wx.getStorageSync('sessionkey'),
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData
    };
    _this.util.request(_this.api.GetPhone, post_data, 'POST').then(res=>{
          console.log(res)
    })
},
#后端代码
def get_phone(encryptedData, iv, sessionKey):
    # base64 decode
    sessionKey = base64.b64decode(sessionKey)    encryptedData = base64.b64decode(encryptedData)
    iv = base64.b64decode(iv)

    cipher = AES.new(sessionKey, AES.MODE_CBC, iv)

    decrypted = json.loads(_unpad(cipher.decrypt(encryptedData)))

    if decrypted['watermark']['appid'] != wx.appid:
        raise Exception('Invalid Buffer')
    return decrypted

def _unpad(s):
    return s[:-ord(s[len(s) - 1:])]




暂无评论