微信小程序获取手机号失败? | 邓士鹏

1.必须先获取code

在onShow中请求uni.Login({})

2.

<button 
class="wrapperFooterRightBox" 
open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">
立即购买
</button>
decryptPhoneNumber(res){
                uni.showLoading({
                    title:'登录中 ...'
                })
                const that = this
                var detail = res.detail
                
                if (detail.errMsg != 'getPhoneNumber:ok') {
                    return
                }
                
                
                
                
                uni.checkSession({
                    success: (res) => {
                        const params = {
                            wechat_code:that.wxcode,
                            enData:detail.encryptedData,
                            vi:detail.iv,
                            is_scan:that.is_scan,
                        }
                        console.log(params)
                        that.$myRequest.globalRequest(that.$myRouteApi._wxLogin, params, 'POST')
                        .then((res)=>{

                                    console.log(res)
                                    const data = res.data
                                    console.log(data)
                                    uni.setStorageSync('access_token', data.access_token);
                                    uni.setStorageSync('refresh_token', data.refresh_token);
                                    that.is_login = 1
                                    uni.hideLoading()
                                    //    从新模拟点击
                                    that.onClickSubmit()
                                    
                                    // that.$myApi.getUserFindApi()
 
                        })
                    },
                    fail: (res) => {
                        uni.login({
                          provider: 'weixin',
                          success: function (loginRes) {
                              const params = {
                                wechat_code:loginRes.code,
                                enData:detail.encryptedData,
                                vi:detail.iv,
                                is_scan:that.is_scan,
                              }
                              console.log(params)
                              that.$myRequest.globalRequest(that.$myRouteApi._wechatLogin, params, 'POST')
                              .then((res)=>{

                                    console.log(res)
                                    const data = res.data
                                    console.log(data)
                                    uni.setStorageSync('access_token', data.access_token);
                                    uni.setStorageSync('refresh_token', data.refresh_token);
                                    that.is_login = 1
                                    uni.hideLoading()
                                    //    从新模拟点击
                                    that.onClickSubmit()
                                    
                                    that.$myApi.getUserFindApi()

                                      
                              })
                              
                              
                          }
                        })
                            
                            
                    }
                })