本文主要是介绍微信小程序 手机号授权登录 偶尔后端解密失败,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
微信小程序wx.login获取code要在手机号授权前触发
<button:id="code":open-type="hasGetPrivacySetting ? 'getPhoneNumber|agreePrivacyAuthorization' : 'getPhoneNumber'"@getphonenumber="onGetPhoneNumber"class="btn"@click="clickPhoneNumber"></button>
点击按钮是先触发 clickGetCode
async clickGetCode() {this.wxCode = await this.getCode()console.log('this.wxCode', this.wxCode)},getCode() {return new Promise((resolve, reject) => {uni.login({provider: 'weixin',success: res => {resolve(res.code)},fail: reject})})},
手机号授权后再将code和手机号加密串一起给后端
async onGetPhoneNumber(e) {// const code = await this.getCode()console.log('onGetPhoneNumber', e)let that = thisif (e.detail.errMsg == "getPhoneNumber:ok") {loginMpWxMobile({form: {code: that.wxCode,encryptedData: e.detail.encryptedData,isParty: true,iv: e.detail.iv,refereeId: 0}}).then(res => {})} else {console.log('error', e)wx.showToast({title: "授权手机号失败",icon: "none",});}},
这篇关于微信小程序 手机号授权登录 偶尔后端解密失败的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!