51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

uniapp微信小程序获取用户手机号

价值产生信心,信心产生热忱,而热忱则征服世界。------华特·H·柯亭姆

我们在uniapp开发中有时会需要获取用户的手机号

可以在官方文档找到对应的方法

这里注意一点,只能通过button进行点击获取

image-20210606234600326

image-20210606232622281

但我们这样获取到后还需要解密

比如我这里获取到的

|------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | hljs vue <template> <view><button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">点我获取手机号</button></view> </template> <script> export default { data() { return {}; }, methods: { getPhoneNumber(e) { console.log(e.detail.errMsg); console.log(e.detail.iv); console.log(e.detail.encryptedData); } } }; </script> |

image-20210606232933403

你会发现获取到的是一串密文

image-20210606232955137

这个我们需要用session_key

需要通过调用wx.login

|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 | hljs javascript wx.login({ success (res) { if (res.code) { console.log('获取code成功,code为:' + res.code) } else { console.log('登录失败!' + res.errMsg) } } }) |

获取一个code作为参数

然后再加上appidappsecrect调用微信提供的api

image-20210606233703061

去换取openidsession_key

然后再用微信官方提供的一个js去解密

下载地址

我们这里是node环境

所以引入node能用的这个js即可

image-20210606234020683

使用方式在上面的那个demo.js中有

打开就是这样的

|---------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 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 36 37 38 39 40 41 42 43 44 45 | hljs javascript var WXBizDataCrypt = require('./WXBizDataCrypt') var appId = 'wx4f4bc4dec97d474b' var sessionKey = 'tiihtNczf5v6AKRyjwEUhQ==' var encryptedData = 'CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM'+ 'QmRzooG2xrDcvSnxIMXFufNstNGTyaGS'+ '9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+'+ '3hVbJSRgv+4lGOETKUQz6OYStslQ142d'+ 'NCuabNPGBzlooOmB231qMM85d2/fV6Ch'+ 'evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6'+ '/1Xx1COxFvrc2d7UL/lmHInNlxuacJXw'+ 'u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn'+ '/Hz7saL8xz+W//FRAUid1OksQaQx4CMs'+ '8LOddcQhULW4ucetDf96JcR3g0gfRK4P'+ 'C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB'+ '6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns'+ '/8wR2SiRS7MNACwTyrGvt9ts8p12PKFd'+ 'lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV'+ 'oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG'+ '20f0a04COwfneQAGGwd5oa+T8yO5hzuy'+ 'Db/XcxxmK01EpqOyuxINew==' var iv = 'r7BXXKkLb8qrSNn05n0qiA==' var pc = new WXBizDataCrypt(appId, sessionKey) var data = pc.decryptData(encryptedData , iv) console.log('解密后 data: ', data) // 解密后的数据为 // // data = { // "nickName": "Band", // "gender": 1, // "language": "zh_CN", // "city": "Guangzhou", // "province": "Guangdong", // "country": "CN", // "avatarUrl": "http://wx.qlogo.cn/mmopen/vi_32/aSKcBBPpibyKNicHNTMM0qJVh8Kjgiak2AHWr8MHM4WgMEm7GFhsf8OYrySdbvAMvTsw3mo8ibKicsnfN5pRjl1p8HQ/0", // "unionId": "ocMvos6NjeKLIBqg5Mr9QjxrP1FA", // "watermark": { // "timestamp": 1477314187, // "appid": "wx4f4bc4dec97d474b" // } // } |

解密完成后即可获取手机号~

赞(1)
未经允许不得转载:工具盒子 » uniapp微信小程序获取用户手机号