栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合小程序登录获取手机号

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合小程序登录获取手机号

文章目录
  • 一、需要用到的依赖包
  • 二、小程序登录获取手机号的流程
  • 三、参数接收DTO
  • 四、后端发请求的util工具方法
  • 五、获取手机号的接口
  • 六、请求接口获取的phone_info信息


一、需要用到的依赖包
        
            org.projectlombok
            lombok
        
        
        
            cn.hutool
            hutool-all
            5.4.0
        
二、小程序登录获取手机号的流程

1.前端请求getPhoneNumber方法获取code传给后端接口;
2.后端接口通过配置的appid、secretKey请求接口https://api.weixin.qq.com/cgi-bin/token获取access_token参数;
3.后端通过参数code和参数access_token,去请求接口https://api.weixin.qq.com/wxa/business/getuserphonenumber来获取手机号。

三、参数接收DTO
@Data
public class WeChatPhoneDTO {
    // getPhoneNumber接口返回的code
    private String code;
    // 小程序的appid(一般是在程序中配置,不需要前端传参)
    private String appid;
    // 小程序的secretKey(一般是在程序中配置,不需要前端传参)
    private String secretKey;
}
四、后端发请求的util工具方法
public class WeChatUtil {
    
    public static JSONObject getAccessToken(String appid, String secretKey) {
        String result = null;
        try {
            String baseUrl = "https://api.weixin.qq.com/cgi-bin/token";
            HashMap requestParam = new HashMap<>();
            // 小程序 appId
            requestParam.put("grant_type", "client_credential");
            // 小程序唯一凭证id
            requestParam.put("appid", appid);
            // 小程序 appSecret(小程序的唯一凭证密钥)
            requestParam.put("secret", secretKey);
            // 发送GET请求读取调用微信接口获取openid用户唯一标识
            result = HttpUtil.get(baseUrl, requestParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONUtil.parseObj(result);
    }

    
    public static JSONObject getPhoneNumber(String code, String accessToken) {
        String result = null;
        try {
            // 接口调用凭证:accessToken
            String baseUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
            HashMap requestParam = new HashMap<>();
            // 手机号调用凭证
            requestParam.put("code", code);
            // 发送post请求读取调用微信接口获取openid用户唯一标识
            String jsonStr = JSONUtil.toJsonStr(requestParam);
            HttpResponse response = HttpRequest.post(baseUrl)
                    .header(Header.CONTENT_ENCODING, "UTF-8")
                    // 发送json数据需要设置contentType
                    .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded")
                    .body(jsonStr)
                    .execute();
            if (response.getStatus() == HttpStatus.HTTP_OK) {
                result = response.body();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONUtil.parseObj(result);
    }
}
五、获取手机号的接口
@RestController
@RequestMapping("/login")
public class WeChatUserLoginController {
    
    @PostMapping("/phone")
    public String getPhone(@RequestBody WeChatPhoneDTO weChatPhone){
        // 1.请求微信接口服务,获取accessToken
        JSONObject accessTokenJson = WeChatUtil.getAccessToken(weChatPhone.getAppid(), weChatPhone.getSecretKey());
        String accessToken = accessTokenJson.get("access_token",String.class);
        // 2.请求微信接口服务,获取用户手机号信息
        JSONObject phoneNumberJson = WeChatUtil.getPhoneNumber(weChatPhone.getCode(), accessToken);
        WeChatPhoneInfo phoneInfo = phoneNumberJson.get("phone_info", WeChatPhoneInfo.class);
        return phoneInfo.getPurePhoneNumber();
    }
}
六、请求接口获取的phone_info信息
@Data
public class WeChatPhoneInfo {
    // 用户绑定的手机号(国外手机号会有区号)
    private String phoneNumber;
    // 没有区号的手机号
    private String purePhoneNumber;
    // 区号
    private String countryCode;
    // 数据水印
    private String watermark;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956114.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号