uni App 获取手机号,授权获取经纬度,openid,unionid 选择收贷地址,

// 获取用户手机号
                getPhoneNumber(e){
                    // getPhoneNumber:fail no permission 说明没有开通权限,需要企业账号。
                    console.log(e.detail.errMsg)                                            // 判断用户是否允许获取手机号
                    console.log(e.detail.iv)                                                    // 参数 iv
                    console.log(e.detail.encryptedData)                               // 参数encryptedData
                    if(e.detail.errMsg == "getPhoneNumber:ok"){                // 用户允许或去手机号
                            console.log('来了,老弟')    
                    }
                },
                        

     // 获取 openid unionid
            getuserNew(){
                        uni.login({
                            provider: 'weixin',
                            success:res=>{
                                        // console.log(res.code);  
                                         uni.request({  
                                            url: 'https://api.weixin.qq.com/sns/jscode2session',  
                                                method:'GET',  
                                                data: {  
                                                    appid: 'wxb7f97dd94793cb5e',                         //你的小程序的APPID  
                                                    secret: '31cbb0f3cc0af4ac4aeb515bd3e856f0',                 //你的小程序的secret,  
                                                    js_code:res.code,                                 //wx.login 登录成功后的code  
                                                    grant_type: 'authorization_code',
                                                },  
                                                success: (cts) => {  
                                                    // 换取成功后 暂存这些数据 留作后续操作  
                                                    this.openId=cts.data.openid               //openid 用户唯一标识  
                                                    this.unionid=cts.data.unionid             //unionid 开放平台唯一标识   当公众号和小程序同时登录过才会有
                                                    this.session_key=cts.data.session_key     //session_key  会话密钥  
                                                    console.log(cts)
                                                    console.log(this.openId,this.session_key)
                                                }  
                                            }); 
                                    
                                        }
                        });
             },
             // 弹出授权并获取 经纬度
            async GetAddressCoordinate(e)
            {                
                  let that = this
                        // 获取用户是否开启 授权获取当前的地理位置、速度的权限。
                        uni.getSetting({
                          success (res) {
                            console.log(res)
                  
                            // 如果没有授权
                            if (!res.authSetting['scope.userLocation']) {
                              // 则拉起授权窗口
                              uni.authorize({
                                scope: 'scope.userLocation',
                                success () {
                                                    console.info('成功')
                                  //点击允许后--就一直会进入成功授权的回调 就可以使用获取的方法了
                                  uni.getLocation({
                                    type: 'wgs84',
                                    success: function (res) {
                                      that.x = res.longitude
                                      that.y = res.latitude
                                      console.log(res)
                                      console.log('当前位置的经度:' + res.longitude)
                                      console.log('当前位置的纬度:' + res.latitude)
                                      uni.showToast({
                                        title: '当前位置的经纬度:' + res.longitude + ',' + res.latitude,
                                        icon: 'success',
                                        mask: true
                                      })
                                    }, fail (error) {
                                      console.log('失败', error)
                                    }
                                  })
                                },
                                fail (error) {
                                  //点击了拒绝授权后--就一直会进入失败回调函数--此时就可以在这里重新拉起授权窗口
                                  console.log('拒绝授权', error)
                  
                                  uni.showModal({
                                    title: '提示',
                                    content: '若点击不授权,将无法使用位置功能',
                                    cancelText: '不授权',
                                    cancelColor: '#999',
                                    confirmText: '授权',
                                    confirmColor: '#f94218',
                                    success (res) {
                                      console.log(res)
                                      if (res.confirm) {
                                        // 选择弹框内授权
                                        uni.openSetting({
                                          success (res) {
                                            console.log(res.authSetting)
                                          }
                                        })
                                      } else if (res.cancel) {
                                        // 选择弹框内 不授权
                                        console.log('用户点击不授权')
                                      }
                                    }
                                  })
                                }
                              })
                            } else {
                                            
                              // 有权限则直接获取
                              uni.getLocation({
                                type: 'wgs84',
                                success: function (res) {
                                  that.x = res.longitude
                                  that.y = res.latitude
                                  console.log(res)
                                  console.log('当前位置的经度:' + res.longitude)
                                  console.log('当前位置的纬度:' + res.latitude)
                                  uni.showToast({
                                    title: '当前位置的经纬度:' + res.longitude + ',' + res.latitude,
                                    icon: 'success',
                                    mask: true
                                  })
                                }, fail (error) {
                                  uni.showToast({
                                    title: '请勿频繁调用!',
                                    icon: 'none',
                                  })
                                  console.log('失败', error)
                                }
                              })
                            }
                          }
                        })
            },
            
            //获取 当前位置的经度 经度
             getLocation () {
                      let that = this
                      // 获取用户是否开启 授权获取当前的地理位置、速度的权限。
                      uni.getSetting({
                        success (res) {
                          console.log(res)
                
                          // 如果没有授权
                          if (!res.authSetting['scope.userLocation']) {
                            // 则拉起授权窗口
                            uni.authorize({
                              scope: 'scope.userLocation',
                              success () {
                                //点击允许后--就一直会进入成功授权的回调 就可以使用获取的方法了
                                uni.getLocation({
                                  type: 'wgs84',
                                  success: function (res) {
                                    that.x = res.longitude
                                    that.y = res.latitude
                                    console.log(res)
                                    console.log('当前位置的经度:' + res.longitude)
                                    console.log('当前位置的纬度:' + res.latitude)
                                    uni.showToast({
                                      title: '当前位置的经纬度:' + res.longitude + ',' + res.latitude,
                                      icon: 'success',
                                      mask: true
                                    })
                                  }, fail (error) {
                                    console.log('失败', error)
                                  }
                                })
                              },
                              fail (error) {
                                //点击了拒绝授权后--就一直会进入失败回调函数--此时就可以在这里重新拉起授权窗口
                                console.log('拒绝授权', error)
                
                                uni.showModal({
                                  title: '提示',
                                  content: '若点击不授权,将无法使用位置功能',
                                  cancelText: '不授权',
                                  cancelColor: '#999',
                                  confirmText: '授权',
                                  confirmColor: '#f94218',
                                  success (res) {
                                    console.log(res)
                                    if (res.confirm) {
                                      // 选择弹框内授权
                                      uni.openSetting({
                                        success (res) {
                                          console.log(res.authSetting)
                                        }
                                      })
                                    } else if (res.cancel) {
                                      // 选择弹框内 不授权
                                      console.log('用户点击不授权')
                                    }
                                  }
                                })
                              }
                            })
                          } else {
                            // 有权限则直接获取
                            uni.getLocation({
                              type: 'wgs84',
                              success: function (res) {
                                that.x = res.longitude
                                that.y = res.latitude
                                console.log(res)
                                console.log('当前位置的经度:' + res.longitude)
                                console.log('当前位置的纬度:' + res.latitude)
                                uni.showToast({
                                  title: '当前位置的经纬度:' + res.longitude + ',' + res.latitude,
                                  icon: 'success',
                                  mask: true
                                })
                              }, fail (error) {
                                uni.showToast({
                                  title: '请勿频繁调用!',
                                  icon: 'none',
                                })
                                console.log('失败', error)
                              }
                            })
                          }
                        }
                      })
                 // const univerifyManager = uni.getLocation()
                 // console.info(univerifyManager)
            }
uni app 获取手机号,经纬度,openid等信息

uniapp 收贷地址

    <button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress" >请选择收货地址+</button>

 async    chooseAddress(){    
                const  res = await    uni.chooseAddress()
                
                 // 2. 用户成功的选择了收货地址
                if(res.errMsg === 'chooseAddress:ok')
                {
                    // console.log(res)
                     // 3.2 把下面这行代码注释掉,使用 3.3 中的代码替代之
                    // this.address=res
                     // 3.3 调用 Store 中提供的 updateAddress 方法,将 address 保存到 Store 里面
                    this.updateAddress(res)
                }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章