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

成功!

相关推荐
柏舟飞流10 分钟前
Spring Boot + Spring Security + RBAC:从登录鉴权到权限模型设计
java·spring boot·spring
2601_9619633836 分钟前
Spring Boot集成电子签章的7个典型问题与解决方案:从入门到生产级实践
大数据·人工智能·spring boot·python·区块链·智能合约
星轨zb38 分钟前
[Corner项目实战]Spring Boot + LangChain4j Tool Calling实战:让AI自动选择推荐策略
人工智能·spring boot·后端·langchain4j
玩烂小程序1 小时前
微信小程序手串DIY功能开发实录:飞入动画 + 环形排布 + 拖拽换序 + 旋转查看 + 保存设计
微信小程序
逻极1 小时前
Spring Boot 微服务开发提速:我们如何将接口响应时间降低60%
java·spring boot·微服务·性能优化·自动配置
摇滚侠1 小时前
SSM 框架实战教程 SpringBoot 自动配置 176-179
java·spring boot·后端
坚持是一种态度2 小时前
Spring AI Demo - 多模型智能聊天应用
人工智能·spring boot
斯内普吖2 小时前
(开源)高校素拓分管理系统小程序实战指南 基于 Java + SpringBoot + uni-app + Vue + MySQL
java·spring boot·mysql·小程序·uni-app·开源
何时梦醒2 小时前
HTML5 Canvas 从入门到实战:手把手教你打造一款"打飞机"小游戏
微信小程序