后台异常:
Given final block not properly padded. Such issues can arise if a bad
获取手机号需要用button组件,并且open-type="getPhoneNumber",点击后触发getPhoneNumber方法,得到解密需要的相关参数
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
第一次代码如下:
错误代码
如此每次第一次请求都会解密失败,原因是wxLoginCode被刷新了。
官方文档写的很清楚
现在将代码稍作调整,在进页面时提前缓存一个wx.login返回的code
在onLoad中提前拿到一个code,并存在页面数据中
然后再修改getPhoneNumber方法
修改后的方法
可以看出,在这个方法中没有直接调用wx.login,而是根据官方文档说明,先checkSession,如果已经登录,则直接拿页面缓存的wxLoginCode作为解密依据,否则再调用wx.login。