news 2026/8/26 21:51:41

微信小程序 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/8/26 14:52:28

Kotaemon缓存策略优化:减少重复检索提升响应速度

Kotaemon缓存策略优化&#xff1a;减少重复检索提升响应速度 在企业级智能对话系统日益普及的今天&#xff0c;一个看似简单的问题——“如何重置我的密码&#xff1f;”——可能在一天内被成百上千名用户反复提出。如果每次请求都触发完整的知识检索、上下文组装和大模型生成流…

作者头像 李华
网站建设 2026/8/26 0:50:51

霍格沃兹魔法之墙:当主题展厅打破次元壁,开启沉浸式魔法奇旅

推开展厅的青铜大门&#xff0c;熟悉的《海德薇变奏曲》在耳畔响起&#xff0c;眼前却并非电影中的霍格沃兹大厅——而是一面“魔法之墙”。它没有砖石堆砌的厚重感&#xff0c;却以流动的光影、灵敏的感应与隐藏的“魔法机关”&#xff0c;将观众瞬间拽入J.K.罗琳笔下的奇幻宇…

作者头像 李华
网站建设 2026/8/26 20:40:19

零基础学写大麦抢票脚本:Python自动化入门教程

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个适合新手的简易大麦抢票脚本教程代码。使用Python基础语法&#xff0c;通过Requests库实现GET/POST请求&#xff0c;讲解如何分析大麦网接口&#xff0c;处理登录Cookie&am…

作者头像 李华
网站建设 2026/8/26 16:30:09

Scikit-Learn 1.8引入 Array API,支持 PyTorch 与 CuPy 张量的原生 GPU 加速

Scikit-Learn 1.8.0 更新引入了实验性的 Array API 支持。这意味着 CuPy 数组或 PyTorch 张量现在可以直接在 Scikit-Learn 的部分组件中直接使用了&#xff0c;且计算过程能保留在 GPU 上。1.8.0 到底更新了什么&#xff1f; Scikit-Learn 开始正式支持Python Array API 标准。…

作者头像 李华
网站建设 2026/8/25 8:39:16

对比实测:传统部署 vs AI生成Docker方案效率提升300%

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 请生成一份详细的Dify项目Docker部署效率对比报告模板&#xff0c;要求&#xff1a;1. 设计对照实验方案 2. 包含时间消耗的量化指标 3. 分析常见错误的解决耗时 4. 提供性能测试对…

作者头像 李华
网站建设 2026/8/26 21:38:37

Wav2Lip实战:打造个性化虚拟主播

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个虚拟主播应用&#xff0c;结合Wav2Lip技术和虚拟形象。功能要求&#xff1a;1. 实时音频输入处理&#xff1b;2. 虚拟形象唇形同步&#xff1b;3. 支持多种表情和动作控制&…

作者头像 李华