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

成功!

相关推荐
AI源码5 小时前
乡村振兴三农小程序
小程序·notepad++
IT研究室5 小时前
最新计算机毕业设计选题推荐-基于spring boot的房屋交易系统-网站-文档指导-Java-springboot
java·spring boot·课程设计
RuoyiOffice5 小时前
Java物联网项目拆解:设备接入之后,数据怎样真正用起来
java·vue.js·spring boot·物联网·iot·企业管理·ruoyi office
千寻技术帮6 小时前
基于微信小程序的家政服务系统(源码+脚本+文档)_50039
mysql·微信小程序·小程序·毕业设计·家政服务
淘源码A7 小时前
智能预问诊系统源码,基于SpringBoot、MyBatis-Plus、uni-app等技术栈,开箱即用
java·spring boot·源码·二次开发·电子病历·预问诊·智能预问诊
愚公搬代码7 小时前
【愚公系列】《微信小程序项目实战(AI编程+视频图解)》045-项目实战:使用 AI 编程工具辅助实现我的订单页面
人工智能·微信小程序·ai编程
源码手记9 小时前
基于微信小程序的购物商城系统-毕设源码
微信小程序·小程序·课程设计
钱栈up13 小时前
删 18 个模块全程零报错:烧结系统单体拆微服务的 P0 实操
spring boot·微服务
软件技术NINI1 天前
基于SpringBoot的社区留守儿童帮扶系统
java·spring boot·后端
社会渣渣1 天前
开发者工具正常、真机白屏:一次渲染差异排查
微信小程序