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

成功!

相关推荐
用户8307196840822 小时前
放弃Shiro的N个理由: Spring Security 让安全开发 “零门槛”
spring boot
爱吃烤鸡翅的酸菜鱼4 小时前
Spring Boot 注解全栈指南:涵盖 Bean 注册、配置加载、请求映射、事务控制、数据校验等一网打尽
java·开发语言·spring boot·后端·spring
czlczl200209254 小时前
Spring Boot + Redis :如何设计“登出”功能
spring boot·redis·后端
a程序小傲4 小时前
米哈游Java后端面试被问:Spring Boot Starter的制作原理
java·spring boot·后端
我是小邵4 小时前
【网页编写的编辑器对比】HBuilder / VS Code / Notepad++ / WebStorm
编辑器·notepad++·webstorm
后端小张4 小时前
【JAVA 进阶】深入理解Sentinel:分布式系统的流量守卫者
java·开发语言·spring boot·后端·spring·spring cloud·sentinel
鲁Q同志5 小时前
微信小程序调用上一页的方法(主包,分包)
微信小程序·小程序
2024暴富5 小时前
SpringBoot基于Mybatis拦截器实现数据权限(图文)
spring boot·spring cloud·mybatis
风月歌5 小时前
小程序项目之基于微信小程序的高校课堂教学管理系统源代码(源码+文档)
java·微信小程序·小程序·毕业设计·源码