微信小程序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",});}},