getPhoneNumber(e) { console.log(e.detail); const encryptedData = e.detail.encryptedData; const iv = e.detail.iv; this .reLogin(encryptedData,iv); }, reLogin(encryptedData, iv){ const _that = this ; wx.login({ success(res) { if (res.code) { console.log(); //发起网络请求 wx.request({ method: 'POST' , url: app.apiService.loginWxUserUrl, data: { code: res.code, encrypted_data: encryptedData, iv: iv }, success: (res) => { }, fail: (err) => { } }); } } }) } |
请求模式,每次用户授权手机号码,都是另外调用login获取最新的code,然后传给后端解析
在开发环境用自己的账号测试授权经常会出现如下报错
有时候重新调用又可以
操作信息:
Traceback (most recent call last):
File "./weixin/api/views.py", line 622, in post
account = self.get_account(request, login_info, access_token, code)
File "./weixin/api/views.py", line 557, in get_account
crypt_info = self._wx_decrypt(session_key, encrypted_data, iv) # 通过前端加密的数据获取到用户的手机号
File "./weixin/api/views.py", line 539, in _wx_decrypt
return WXBizDataCrypt(appid, session_key).decrypt(encrypted_data, iv)
File "./weixin/WXBizDataCrypt.py", line 19, in decrypt
decrypted = json.loads(self._unpad(cipher.decrypt(encryptedData)).decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa2 in position 2: invalid start byte
网友回复
方式反了哈。
先login拿到code再用iv、encryptedData、code向后端拿 解密手机号