Java微信小程序获取openId和用户手机号示例

先看官方文档 小程序登录

首先是获取小程序的openID,主要是下面这三个参数,appid 和 secret 申请完小程序就会有,js_code 这个需要前端通过 SDK 调用wx.login() 方法获取再送到后端
在这里插入图片描述

然后再看下获取用户手机号 获取手机号

access_token 是通过小程序 app 和 secret 获取的,code 一样还是前端调 SDK 送到后端

在这里插入图片描述

直接上代码

package com.sakura.user.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sakura.common.exception.BusinessException;
import com.sakura.common.redis.RedisUtil;
import com.sakura.common.tool.HttpsRequestUtil;
import com.sakura.common.tool.StringUtil;
import com.sakura.user.vo.wx.WxAccessTokenVo;
import com.sakura.user.vo.wx.WxAppLoginVo;
import com.sakura.user.vo.wx.WxPhoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author Sakura
 * @date 2024/8/22 11:04
 * 微信小程序工具类
 */
@Component
@Slf4j
public class WxAppUtil {

    @Value("${wx.app.appid}")
    private String appid;

    @Value("${wx.app.secret}")
    private String secret;

    // 获取授权token
    private static final String GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";

    // 获取授权用户信息URL
    private static final String JS_CODE_2_SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session";

    private static final String WX_APP_ACCESS_TOKEN = "wx_app_access_token";

    // 获取授权手机号URL
    private static final String GET_USER_PHONE_NUMBER_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";

    @Autowired
    private RedisUtil redisUtil;

    // 获取授权凭证
    public String getAccessToken() {
        // 优先从Redis获取,没有再调接口
        // 如果Redis里面有则直接取Redis里面的
        if (redisUtil.hasKey(WX_APP_ACCESS_TOKEN)) {
            return redisUtil.get(WX_APP_ACCESS_TOKEN).toString();
        }
        // 拼接请求参数
        StringBuilder strUrl = new StringBuilder();
        strUrl.append(GET_TOKEN)
                .append("?grant_type=client_credential")
                .append("&appid=").append(appid)
                .append("&secret=").append(secret);
        try {
            String resultStr = HttpsRequestUtil.sendGetRequest(strUrl.toString());
            log.info("【微信小程序获取授权凭证】:返回结果:" + resultStr);
            WxAccessTokenVo wxAccessTokenVo = JSON.parseObject(resultStr, WxAccessTokenVo.class);
            if (StringUtil.isBlank(wxAccessTokenVo.getAccess_token())) {
                throw new BusinessException("微信小程序获取授权凭证异常");
            }

            //官方 access_token expires_in 为 2个小时, 这里设置 100分钟
            redisUtil.set(WX_APP_ACCESS_TOKEN, wxAccessTokenVo.getAccess_token(), 100 * 60);

            return wxAccessTokenVo.getAccess_token();
        } catch (Exception e) {
            log.info("【微信小程序获取授权凭证】:获取异常:" + e.getMessage());
            e.printStackTrace();
            throw new BusinessException("微信小程序获取授权凭证异常");
        }
    }

    // 小程序授权登录
    public WxAppLoginVo login(String code) {
        // 拼接请求参数
        StringBuilder strUrl = new StringBuilder();
        strUrl.append(JS_CODE_2_SESSION_URL)
                .append("?appid=").append(appid)
                .append("&secret=").append(secret)
                .append("&js_code=").append(code)
                .append("&grant_type=authorization_code");
        try {
            String resultStr = HttpsRequestUtil.sendGetRequest(strUrl.toString());
            log.info("【微信小程序登录】:登录返回结果:" + resultStr);
            WxAppLoginVo wxAppLoginVo = JSON.parseObject(resultStr, WxAppLoginVo.class);
            // 这里要注意,如果请求成功是不会返回errcode的
            if (wxAppLoginVo.getErrcode() != null && wxAppLoginVo.getErrcode() != 0) {
                log.info("【微信小程序登录】:登录失败,错误码:" + wxAppLoginVo.getErrcode() + "  " + wxAppLoginVo.getErrmsg());
                throw new BusinessException(wxAppLoginVo.getErrmsg());
            }
            return wxAppLoginVo;
        } catch (Exception e) {
            log.info("【微信小程序登录】:登录异常:" + e.getMessage());
            e.printStackTrace();
            throw new BusinessException("微信小程序登录异常");
        }
    }

    // 获取微信授权手机号
    public WxPhoneVo getWxPhone(String code) {
        // 先要获取access_token
        String accessToken = getAccessToken();
        // 拼接请求参数
        StringBuilder strUrl = new StringBuilder();
        strUrl.append(GET_USER_PHONE_NUMBER_URL)
                .append("?access_token=")
                .append(accessToken);

        // 封装请求参数到body
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);

        try {
            String resultStr = HttpsRequestUtil.sendPostRequest(strUrl.toString(), jsonObject.toJSONString());
            log.info("【微信小程序获取手机号】:获取手机号返回结果:" + resultStr);
            WxPhoneVo wxPhoneVo = JSON.parseObject(resultStr, WxPhoneVo.class);
            if (wxPhoneVo.getErrcode() != null && wxPhoneVo.getErrcode() != 0) {
                log.info("【微信小程序获取手机号】:获取手机号失败,错误码:" + wxPhoneVo.getErrcode() + "  " + wxPhoneVo.getErrmsg());
                throw new BusinessException(wxPhoneVo.getErrmsg());
            }
            return wxPhoneVo;
        } catch (Exception e) {
            log.info("【微信小程序获取手机号】:获取手机号异常:" + e.getMessage());
            e.printStackTrace();
            throw new BusinessException("微信小程序获取手机号异常");
        }
    }
}

http工具类

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author Sakura
 * @date 2024/6/24 17:07
 */
public class HttpsRequestUtil {

    public static String sendPostRequest(String urlString, String jsonInputString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");

        // 设置请求头
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");

        // 启用输出流,用于发送请求数据
        connection.setDoOutput(true);

        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = jsonInputString.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        // 获取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
            StringBuilder response = new StringBuilder();
            String responseLine;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
            return response.toString();
        } finally {
            // 关闭连接
            connection.disconnect();
        }
    }

    public static String sendGetRequest(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为GET
        connection.setRequestMethod("GET");

        // 设置请求头
        connection.setRequestProperty("Accept", "application/json");

        // 获取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
            StringBuilder response = new StringBuilder();
            String responseLine;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
            return response.toString();
        } finally {
            // 关闭连接
            connection.disconnect();
        }
    }

    public static byte[] sendPostReturnByte(String urlString, String jsonInputString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");

        // 设置请求头
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");

        // 启用输出流,用于发送请求数据
        connection.setDoOutput(true);

        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = jsonInputString.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        // 获取响应
        try (InputStream is = connection.getInputStream()) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int bytesRead;
            byte[] data = new byte[1024];
            while ((bytesRead = is.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytesRead);
            }
            return buffer.toByteArray();
        } finally {
            // 关闭连接
            connection.disconnect();
        }
    }

}

然后几个封装结果的对象

package com.sakura.user.vo.wx;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
@ApiModel(value = "微信小程序授权登录信息")
public class WxAppLoginVo implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("会话密钥")
    private String session_key;

    @ApiModelProperty("用户在开放平台的唯一标识符,当前小程序已绑定到微信开放平台账号下会返回")
    private String unionid;

    @ApiModelProperty("错误信息")
    private String errmsg;

    @ApiModelProperty("用户唯一标识")
    private String openid;

    @ApiModelProperty("错误码")
    private Integer errcode;

}
package com.sakura.user.vo.wx;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
@ApiModel(value = "微信手机号信息")
public class WxPhoneVo implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("错误信息")
    private String errmsg;

    @ApiModelProperty("错误码")
    private Integer errcode;

    @ApiModelProperty("微信手机号详细信息")
    private WxPhoneInfoVo phone_info;

}
package com.sakura.user.vo.wx;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
@ApiModel(value = "微信手机号详细信息")
public class WxPhoneInfoVo implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("用户绑定的手机号(国外手机号会有区号)")
    private String phoneNumber;

    @ApiModelProperty("没有区号的手机号")
    private String purePhoneNumber;

    @ApiModelProperty("区号")
    private String countryCode;

}

接口都很简单,这里我再补充一下业务逻辑,就是用户扫码进入小程序时,前端以静默的方式调用一次 wx.login() 方法拿到 code 再调后端接口去获取用户 openId 和 unionId(绑定开放平台后会返回),后台拿到 openId 后就可以给用户返回登录成功信息,信息里面可以有一个手机号标识,然后前端再根据这个标识判断,如果没有手机号,前端再调小程序微信授权手机号的按钮,这个必须用户手动确定,前端拿到手机号授权 code 再送给后端获取手机号并更新到用户信息,完成绑定。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子非衣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值