- 通过按钮触发获取手机号码事件
<u-button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</u-button>
- 触发事件getPhoneNumber
async getPhoneNumber(e) {
const httpRes = await this.$u.api.getPhone({
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
sessionKey: this.session_key,
})
this.$u.toast(httpRes.phoneNumber);
},
- 进行解密(解密之前要在登录时获取到encryptedData、iv、sessionKey等参数)使用WXBizDataCrypt.js来进行解密。
- 去官网下载WXBizDataCrypt.js包,放到自己的目录下。
- 使用mockm写的后端接口,将解密后的电话号码传给前端即可。
async 'post /getPhone'(req, res) {
const WXBizDataCrypt = require(`./httpData/WXBizDataCrypt.js`)
const appId = minApp.appid
const {
sessionKey,
encryptedData,
iv,
} = req.body
const pc = new WXBizDataCrypt(appId, sessionKey)
const data = pc.decryptData(encryptedData , iv)
res.json(data)
},