首页 > 编程语言 >uniapp微信小程序获取手机号 位置信息

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

时间:2024-06-03 18:11:47浏览次数:18  
标签:uniapp console 手机号 success res let 微信 uni log

一.获取手机号

获取手机号

<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)
    }
})

标签:uniapp,console,手机号,success,res,let,微信,uni,log
From: https://www.cnblogs.com/reround/p/18229391

相关文章

  • 【微信小程序】bug解决:van-tab+ucharts多图显示问题
    这是一条价值30r的博客......
  • python 提取手机号
    importre#导入正则表达式处理模块defget_phone(text):'''使用正则表达式提取文本中的手机号:paramtext:原始文本:return:手机号数组'''phones=re.findall(r'(13\d{9}|14[5|7]\d{8}|15\d{9}|166{\d{8}|17[3|6|7]{\d{8}|18\d{9}......
  • 微信小程序-声明和绑定事件
    一.概念小程序页面使用的数据在Page()方法里使用data对象进行声明定义定义好之后,使用{{}}进行绑定声明Page({data:{school:'未发之中',obj:{name:'dadada'},id:1,useChk:true}})绑定<!--使用双括号展示数据--><view>{{scho......
  • 微信小程序开发中的表单验证与数据提交
    表单验证和数据提交在微信小程序开发中是非常常见的功能。表单验证用于确保用户输入的数据符合指定的格式要求,数据提交用于将用户输入的数据发送到服务器端进行处理。下面是一个示例,演示了如何在微信小程序中进行表单验证和数据提交。首先,让我们创建一个简单的表单,包含用户名......
  • 苹果手机怎么恢复微信聊天记录?2024不要错过的4个方法!
    作为一款广受欢迎聊天APP,微信会保存我们的聊天记录,包括图片、视频和信息。然而,很多人都想知道,如果我们意外丢失或删除了苹果手机微信上的数据,是否还能恢复它们?答案是100%肯定的。接下来,本文将为你介绍苹果手机怎么恢复微信聊天记录,其中包括在有备份或无备份的情况下找回删除的......
  • 【计算机毕业设计】331基于微信小程序的家庭财务管理系统
    ......
  • 【计算机毕业设计】342大学生校园兼职微信小程序
    ......
  • 免费分享一套微信小程序图书借阅(图书管理)系统(SpringBoot后端)
    大家好,我是java1234_小锋老师,看到一个不错的微信小程序图书借阅(图书管理)系统(,分享下哈。项目介绍该项目是一套图书馆信息管理系统,包括用户小程序以及后台管理系统,基于SpringBoot+MyBatis实现。前台商城系统包含用户注册登录、首页门户、图书查询、在线借阅、个人中心、我......
  • uniapp开发APP遇到的问题
    图标变成了undefined原因:设置prefix时,在非nvue环境下,需u-input才有效。//旧版<u--inputplaceholder="请输入用户名"type="text"> <templateslot="prefix"> <viewclass="solts"> <viewstyle="padding-top:6upx&qu......
  • 微信小程序------音乐播放------背景音乐播放
    使用wx.getBackgroundAudioManager()方法播放背景音乐即便突出也会显示音乐播放1、成品展示单击右上角按钮播放音乐 2、样式设置2.1html样式<viewclass="playerplayer-{{isPlayingMusic?'play':'pause'}}"bindtap="play"><imagesrc="/pages/ima......