微信小程序获取手机号
微信小程序中,前端不可以直接获取用户手机号明文,需要后端配合密码解析。
解析手机号需要
解析手机号流程:
class WXBizDataCrypt:
def __init__(self, appId, sessionKey):
self.appId = appId
self.sessionKey = sessionKey
def decrypt(self, encryptedData, iv):
# base64 decode
sessionKey = base64.b64decode(self.sessionKey)
encryptedData = base64.b64decode(encryptedData)
iv = base64.b64decode(iv)
cipher = AES.new(sessionKey, AES.MODE_CBC, iv)
decrypted = json.loads(self._unpad(cipher.decrypt(encryptedData)).decode())
if decrypted['watermark']['appid'] != self.appId:
raise Exception('Invalid Buffer')
# return phonenumber
return decrypted['phoneNumber']
def _unpad(self, s):
return s[:-ord(s[len(s) - 1:])]
安装导入AES算法模块:
sudo pip install pycryptodome
from Crypto.Cipher import AES