news 2026/6/3 4:03:24

微信小程序 springboot获取手机号

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
微信小程序 springboot获取手机号

小程序增加一个button,在js中增加一个方法

<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">{{phone}}</button> getPhoneNumber (e) { console.log(e.detail.code) // 动态令牌 var that = this; wx.request({ url: 'http://localhost:8080/wx/login', //仅为示例,并非真实的接口地址 data: { code: e.detail.code }, header: { 'content-type': 'application/json' // 默认值 }, success (res) { console.log(res.data); that.setData({ 'motto':res.data }) } }) }

在Springboot写一个工具类

package com.example.demo.utils; import cn.hutool.core.map.MapUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Map; @Component public class WxUtils { public static String APPID; public static String APPSECRET; @Value("${wx.appid}") public void initAppid(String s){ APPID = s; } @Value("${wx.appsecret}") public void initAppsecret(String s){ APPSECRET = s; } public static String getAccessToken(){ String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; url = url.replace("APPID", APPID).replace("APPSECRET", APPSECRET); JSONObject jsonObject = JSONUtil.parseObj(HttpUtil.get(url)); return jsonObject.getStr("access_token"); } public static String getOpenId(String code){ String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; url = url.replace("APPID", APPID); url = url.replace("SECRET", APPSECRET); url = url.replace("CODE", code); JSONObject jsonObject = JSONUtil.parseObj(HttpUtil.get(url)); return jsonObject.getStr("openid"); } public static String getPhone(String code) { String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN"; url = url.replace("ACCESS_TOKEN", getAccessToken()); Map<String, Object> param = MapUtil.newHashMap(); param.put("code", code); JSONObject jsonObject = JSONUtil.parseObj(HttpUtil.post(url, JSONUtil.toJsonStr(param))); return jsonObject.getJSONObject("phone_info").getStr("phoneNumber"); } }

写一个测试类

package com.example.demo.controller; import com.example.demo.utils.WxUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/wx") public class WxController { public String index(){ return "index"; } @GetMapping("/login") public String login(String code) { return WxUtils.getPhone(code); } }

点击login,弹出确认对话框

允许后向后台发送请求,返回电话号码

2025-12-18T22:09:33.360+08:00 INFO 18656 --- [demo1] [nio-8080-exec-2] c.e.demo.intercepter.GlobalInterceptor : 请求地址:http://localhost:8080/wx/login

成功!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/2 5:37:12

大数据架构自动化运维:从部署到扩缩容

大数据架构自动化运维&#xff1a;从部署到扩缩容关键词&#xff1a;大数据运维、自动化部署、弹性扩缩容、监控告警、AIOps摘要&#xff1a;本文从“开一家永远不打烊的智能餐厅”的生活场景切入&#xff0c;用通俗易懂的语言讲解大数据架构自动化运维的核心逻辑。我们将一步一…

作者头像 李华
网站建设 2026/5/28 11:39:37

位运算求解八皇后问题:极致优雅的性能优化之道

八皇后问题是计算机科学中的经典回溯算法案例&#xff0c;但在大规模棋盘时性能瓶颈明显。今天我们来介绍一种高效优雅的位运算解法&#xff0c;它不仅能大幅提升性能&#xff0c;还能让代码更加简洁清晰。一、位运算基础&#xff1a;八皇后必备的位操作技巧在深入八皇后问题之…

作者头像 李华
网站建设 2026/6/2 11:12:43

34、Shell编程中的流程控制与位置参数使用

Shell编程中的流程控制与位置参数使用 1. 流程控制之case语句 在编程里,流程控制是非常重要的一部分。之前在处理用户选择时,我们会用一系列 if 命令来判断用户选了哪个选项。不过,这种结构在程序里经常出现,所以很多编程语言(像shell)都提供了用于多选择决策的流程控…

作者头像 李华
网站建设 2026/5/29 21:52:36

揭秘边缘端Agent数据持久化难题:4步实现低功耗高可靠存储

第一章&#xff1a;边缘端Agent数据持久化的挑战与意义在物联网和边缘计算快速发展的背景下&#xff0c;边缘端Agent作为连接终端设备与云端服务的核心组件&#xff0c;承担着数据采集、本地处理与状态同步等关键任务。由于边缘设备常面临网络不稳定、资源受限和突发断电等问题…

作者头像 李华
网站建设 2026/6/2 13:27:14

从采集到洞察:工业互联网Agent数据分析的7个必知步骤

第一章&#xff1a;工业互联网Agent数据分析的核心价值在工业互联网体系中&#xff0c;Agent作为部署于边缘设备或关键节点的智能代理程序&#xff0c;承担着数据采集、实时处理与本地决策的重要职责。其产生的数据不仅涵盖设备运行状态、环境参数和操作日志&#xff0c;还包含…

作者头像 李华