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

成功!

相关推荐
辰海Coding17 小时前
MiniSpring框架学习-完成的 IoC 容器
java·spring boot·学习·架构
Maiko Star1 天前
* SpringBoot整合LangChain4j
java·spring boot·后端·langchain4j
绝知此事1 天前
【产品更名】通义灵码升级为 Qoder CN:AI 编码助手新时代,附大模型收费与 Spring Boot 支持全对比
人工智能·spring boot·后端·idea·ai编程
linmoo19861 天前
Agent应用实践之四 - 基础:AgentScope-SpringBoot集成源码解析
人工智能·spring boot·agent·agentscope·openclaw
海兰1 天前
【第21篇-续】graph-Stream-Node改造为适配openAI模型示例
java·人工智能·spring boot·spring·spring ai
Albert Edison1 天前
基于 SpringBoot + RabbitMQ 完成企业级应用通信
spring boot·rabbitmq·java-rabbitmq
happymaker06261 天前
Spring学习日记——DAY03(yml文件)
java·spring boot·spring
白菜__1 天前
微信小程序网关逆向分析
javascript·微信小程序·小程序·node.js·网络爬虫·微信网关·小程序网关
TANKING-1 天前
微信小程序订阅消息推送系统(一次性/长期订阅消息推送)
微信小程序·小程序
hikktn1 天前
企业级Spring Boot应用管理:从零打造生产级启动脚本
java·spring boot·后端