微信小程序电话号码授权

embedded/2024/10/21 3:44:56/

前端:
文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html

uniapp调用的时候,要将bind用@替换

<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"></button>
function getPhoneNumber(e){console.log(e.detail.code)  // 动态令牌console.log(e.detail.errMsg) // 回调信息(成功失败都会返回)console.log(e.detail.errno)  // 错误码(失败时返回)if (e.detail.code){User.getUserTelNumber(e.detail.code).then(res=>{// 授权成功setTimeout(()=>{uni.navigateBack({delta:1 //返回的页面层级,1为返回上一个页面})},1000)}).catch((err)=>{console.log(err,"errrrrrr")})}else{// 拒绝授权toast("拒绝授权","error")setTimeout(()=>{uni.navigateBack({delta:1 //返回的页面层级,1为返回上一个页面})},1000)}}

java:
文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html
掉这个获取电话号码,需要先获取access_token
https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html
获取token,有效期是两小时,需要缓存,

    @GetMapping("/getTelNumber")@LoginRequired@ApiOperation(value = "getTelNumber",notes = "getTelNumber")public RespBean getTelNumber(String code){userService.getWechatTelNumber(code);return RespBean.ok("授权成功");}
    /*** 获取微信的 accessToken,, 需要缓存起来,,, 两小时过期* @return*/public String getAccessToken(){String tokenUrl = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", wechatPayConfig.getAppId(), wechatPayConfig.getAppSecret());Map map = restTemplate.getForObject(tokenUrl, Map.class);String accessToken = (String) map.get("access_token");System.out.println("accessToken = " + accessToken);return accessToken;}
    public void getWechatTelNumber(String code) {try {String accessToken= globalMap.get("access_token");if (accessToken == null){accessToken = getAccessToken();globalMap.put("access_token",accessToken);}//通过token和code来获取用户手机号String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken + "&code=" + code;Map<String, String> map = new HashMap<>();map.put("code",code);Object obj = restTemplate.postForObject(url, map, Object.class);System.out.println("obj = " + obj);Map<String,Object> resultMap = (Map<String,Object>) obj;Map<String,Object> phoneInfo = (Map<String, Object>) resultMap.get("phone_info");System.out.println("phoneInfo = " + phoneInfo);String phoneNumber = (String) phoneInfo.get("phoneNumber");// 将number更新到user表User user = SecurityContextHolder.getUser();user.setTel(phoneNumber);this.updateById(user);} catch (RestClientException e) {throw new ServiceException(e.getMessage());}}

因为授权手机号,必须要通过点击按钮,,用户授权,不能够直接调api,,必须要按钮,,可以将按钮展示为一个授权页面,,跳过去授权,,成功之后跳回

引用:https://blog.csdn.net/qq_46940224/article/details/136865901

https://blog.csdn.net/weixin_41698051/article/details/97777993


http://www.ppmy.cn/embedded/97467.html

相关文章

Java并发必杀技!线程池让你的程序速度飙升不止一点点!

文章目录 1 线程池的工作机制是什么&#xff1f;2 线程池的任务出现异常该怎么解决&#xff1f;3 线程池的内存泄露该如何解决&#xff1f; 近期迷上了举例子来结合知识点学习&#xff0c;尽量减少枯燥&#xff0c;如有错见谅哈~ 1 线程池的工作机制是什么&#xff1f; 线程池…

搭建内网开发环境(二)|Nexus安装及使用

引言 上一篇教程中按照了 docker 作为容器化工具&#xff0c;在本篇教程中将使用 docker-compose 安装 nexus。 搭建内网开发环境&#xff08;一&#xff09;&#xff5c;基于docker快速部署开发环境 什么是 Nexus Nexus是一个强大的仓库管理器&#xff0c;主要用于搭建和管…

算法力扣刷题记录 八十七【53. 最大子序和】

前言 贪心章节第4篇。动态规划章节第10篇。同一题&#xff0c;两种方法。 记录 八十七【53. 最大子序和】 一、题目阅读 给你一个整数数组 nums &#xff0c;请你找出一个具有最大和的连续子数组&#xff08;子数组最少包含一个元素&#xff09;&#xff0c;返回其最大和。 …

JavaEE 第9节 阻塞队列详解

一、概念 阻塞队列是在普通队列&#xff08;先进先出的数据结构&#xff09;的基础上增加了阻塞属性的特殊队列 1&#xff09;当阻塞队列空的时候&#xff0c;如果继续出队元素会进入阻塞状态&#xff0c;直到其他线程入队元素。 2&#xff09;当阻塞队列满的时候&#xff0c;…

[000-01-030].Zookeeper学习大纲

我的博客大纲 我的后端学习大纲 第一步&#xff1a;Zookeeper是什么 1.第01节&#xff1a;Zookeeper概述 第二步&#xff1a;Zookeeper怎么使用&#xff1a; 2.1.开发环境搭建 2.第02节 &#xff1a;Zookeeper本地安装3.第03节 &#xff1a;Zookeeper集群操作 2.2.具体使用…

企业级批量无人值守安装

企业级批量无人值守安装 一、批量无人值守安装1.简介PXE工作流程 2.核心技术&#xff08;dhcp、httpd、tftp&#xff09;3.实验3.1 准备环境3.2 防护关闭3.3 软件安装3.4 软件配置DHCP服务设置httpd服务配置tftp服务配置 3.5 编写引导安装相关文件&#xff0c;放到指定位置3.5.…

【漫谈C语言和嵌入式013】函数指针与指针函数详解:概念、区别及实例

在C语言中&#xff0c;理解指针的各种用法是非常关键的&#xff0c;特别是当涉及到更复杂的概念如函数指针和指针函数时。这两者听起来非常相似&#xff0c;但实际上是完全不同的概念。在这篇博客中&#xff0c;我们将探讨函数指针和指针函数的定义、区别以及如何在实际中使用它…

共享经济背景下校园、办公闲置物品交易平台-计算机毕设Java|springboot实战项目

&#x1f34a;作者&#xff1a;计算机毕设匠心工作室 &#x1f34a;简介&#xff1a;毕业后就一直专业从事计算机软件程序开发&#xff0c;至今也有8年工作经验。擅长Java、Python、微信小程序、安卓、大数据、PHP、.NET|C#、Golang等。 擅长&#xff1a;按照需求定制化开发项目…