有个前提是小程序得是企业帐号,个人帐号没有开放这个能力。
用button组件获取手机号,比较简单,记录一下避免遗忘。
1
| <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| getPhoneNumber(e) { util.getOpenId().then(openId => { let params = { encryptedData: e.detail.encryptedData, iv: e.detail.iv, openid: openId, loginSysName: "APPLETS_HAOFANG" }
if (e.detail && e.detail.encryptedData) { wx.$post("/user/api/wxDecryptData", params).then(res => { this.setData({ telPhone: res.phoneNumber }) }) } }) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
function getOpenId() { return new Promise((resolve, reject) => { wx.login({ success: res => { let params = { loginSysName: "APPLETS_HAOFANG", loginType: 7, loginName: res.code, }; wx.$post("/user/api/wxAuth", params).then((res) => { if (!res) { wx.showToast({ icon: 'none', title: '获取不到openid', }) } resolve(res.openid) }).then(res => { reject('') }) }, fail: err => { reject('') } }) })
} module.exports = { getOpenId: getOpenId }
|