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

成功!

相关推荐
初次攀爬者3 小时前
Kafka 基础介绍
spring boot·kafka·消息队列
用户8307196840823 小时前
spring ai alibaba + nacos +mcp 实现mcp服务负载均衡调用实战
spring boot·spring·mcp
WangHappy3 小时前
不写 Canvas 也能搞定!小程序图片导出的 WebView 通信方案
前端·微信小程序
Java水解4 小时前
SpringBoot3全栈开发实战:从入门到精通的完整指南
spring boot·后端
小时前端8 小时前
微信小程序选不了本地文件?用 web-view + H5 一招搞定
前端·微信小程序·uni-app
初次攀爬者1 天前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺1 天前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart1 天前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
icebreaker1 天前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker1 天前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序