uniapp微信小程序获取手机号 位置信息

一.获取手机号

获取手机号

<template>
   <u-button open-type="getPhoneNumber" @getphonenumber="getNumber">一键登录</u-button>
</template>

<script>
  export default {
  
    methods: {
        getNumber(e) {
            let appid = '小程序ID'
            let secret = '小程序秘钥'
            uni.request({
                url: 'https://api.weixin.qq.com/cgi-bin/token',
                method: 'GET',
                data: {
                    appid,
                    secret,
                    grant_type: 'client_credential'
                },
                success: (res) => {
                    let accessToken = res.data.access_token
                    uni.request({
                        url: `https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${accessToken}`,
                        method: 'POST',
                        data: {
                            code: e.detail.code
                        },
                        success: (result) => {
                            console.log(result)
                            if(result.data.errcode === 0) {
                                let phone = result.data.phone_info.phoneNumber
                            }
                        },
                        fail() {
                            uni.showToast({
                                title: '获取失败,请重试',
                                icon: 'error'
                            })
                        }
                    })
                },
                fail() {
                    uni.showToast({
                        title: '获取失败,请重试',
                        icon: 'error'
                    })
                }
            })
        }
    }
  }
</script> 

二.获取位置和选择位置

在manifest.json里添加
 {
  "name": "xxx",
  "appid": "xxx",
  "description": "xxx",
  ...
  "mp-weixin": {
    "permission" : {
        "scope.userLocation" : {
            "desc" : "您的位置信息将用于展示您与商家的距离,为您提供更好的服务。"
        }
    },
    "requiredPrivateInfos" : [ "getLocation", "chooseLocation" ]
  }
}

 

获取位置信息
 uni.getSetting({
    success: res => {
        let s = res.authSetting
        if(s['scope.userLocation']) {
            //说明已经授权了
            uni.getLocation({
                type: 'wgs84',
                success: (res) => {
                    console.log(res)
                },
                fail: () => {
                    console.log('拒绝了')
                }
            })
        }else {
            //没有授权就引导用户开启
            uni.openSetting({
                success: (res) => {
                    let r = res.authSetting
                    if(r['scope.userLocation']) {
                        console.log('授权位置成功')
                         uni.getLocation({
                            type: 'wgs84',
                            success: (res) => {
                                console.log(res)
                            },
                            fail: () => {
                                console.log('拒绝了')
                            }
                        })
                    }else {
                        console.log('授权位置失败')
                    }
                }
            })
        }
    }
})

 

选择位置
 uni.chooseLocation({
    success: res => {
        console.log(res)
    },
    fail: (err) => {
        console.log(err)
    }
})
posted @ 2024-06-03 17:59  环岛公路  阅读(439)  评论(0编辑  收藏  举报