当前位置: 首页 > news >正文

微信小程序 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

成功!

http://www.cnnetsun.cn/news/139363.html

相关文章:

  • 給自學者的覺醒:我後悔太晚擁抱類型註解,它讓我的Side Project完成速度快了3倍
  • 【康复效率提升300%的秘密】:深度解析医疗Agent自主调参机制
  • htop入门指南:5分钟掌握Linux系统监控
  • 【论文精读(六)】PointCNN:点云也能用卷积?揭秘神奇的 X-Transformation (NeurIPS 2018)
  • 传统热部署VS快马AI:效率提升300%的对比实验
  • 用htop源码快速构建自定义监控工具
  • YOLOv11 改进 - C2PSA | C2PSA融合CPIASA跨范式交互与对齐自注意力机制(ACM MM2025): 交互对齐机制破解特征融合难题,提升小目标与遮挡目标判别力
  • MySQL-MVCC协议(转载IT秀才的文章)
  • 用Groovy快速构建REST API原型:1小时搞定
  • 做 PPT 最难的不是内容,而是模板:10 个免费又好用的 PPT 模板网站整理
  • 需求波动剧烈怎么办?:用多Agent协同预测应对不确定性
  • SD模型实战:用快马平台5分钟搭建AI艺术生成器
  • 游戏 AI 训练资源稀缺预警:2024年最值得收藏的5个开源框架推荐
  • 【量子 Agent 算法优化终极指南】:揭秘下一代智能体高效决策核心机制
  • 医疗康复Agent方案调整实战手册(基于10万+病例数据验证)
  • 会话(Session)
  • AI编程助手如何帮你快速掌握Java基础
  • Alertmanager在生产环境中的5个最佳实践案例
  • 零基础玩转SD模型:快马平台AI带你轻松入门
  • 2026上半年 IT 就业市场机遇丛生,你做好入局准备了吗?
  • 燃尽了...
  • Excel如何快速求出排名第一、第二、第N的对应数据?必备高频函数
  • vue和springboot框架开发的群众网上高效办事系统的设计与实现_6e4j9xi1
  • 飞算JavaAI自然语言直出全流程代码,告别无效加班
  • 蓝桥杯JAVA--启蒙之路(三)语句
  • 金融级情绪识别模型训练全攻略(基于千万级对话数据的优化经验)
  • 计算机系统基础 bufbomb 实验三
  • Tomcat内存机制以及按场景调优
  • ConvertX:自托管的在线文件转换器
  • 2025年支持企业实现社会价值与商业价值的战略