微信登录与小程序登录获取openId

微信登录获取openId文档
小程序登录获取openId文档

首先引入 hutool 工具,创建WXUtil.java 工具类,内容如下:

package com.demo2.wx;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Objects;

/** 微信与小程序获取openId
 * author xiaochi
 * date 2024/6/1 14:44
 */
@Slf4j
public class WXUtil {

    private static final int miniType = 2;

    // 开放平台
    private static Open open = new Open();
    // 小程序
    private static Mini mini = new Mini();

    /**
     * 获取微信access_token
     * @param code 临时凭证
     * @param source 类型(1微信2小程序)
     * @return
     */
    public static String getAccessToken(String code, Integer source){
        return getRequestBody(code, source).getAccessToken();
    }

    /**
     * 获取微信openid
     * @param code 临时凭证
     * @param source 类型(1微信2小程序)
     * @return
     */
    public static String getOpenId(String code,Integer source){
        return getRequestBody(code, source).getOpenid();
    }

    /**
     * 获取报文
     * @param code 临时凭证
     * @param source 类型(1微信2小程序)
     * @return
     */
    public static WXResponseBody getRequestBody(String code, Integer source){
        // app登录获取openid
        String uri = String.format(open.getOPEN_ACCESS_TOKEN_URL(), open.getOpenAppid(), open.getOpenAppsecret(),code);
        if (Objects.equals(source,miniType)){//小程序登录获取openid
            uri = String.format(mini.getMINI_OPEN_ID_URL(), mini.getMiniappid(), mini.getMiniappsecret(),code);
        }
        return getRequest(uri,source);
    }

    /**
     * 发起请求微信
     * @param uri
     * @return
     */
    public static WXResponseBody getRequest(String uri,Integer source){
        HttpResponse response;
        if (Objects.equals(source,miniType)) {//小程序登录获取openid
            response = HttpRequest.get(uri).execute();
        }else {
            response = HttpRequest.post(uri).execute();
        }
        log.info("access_token响应报文:{}",response.body());
        if (StrUtil.isBlank(response.body())){
            throw new BusinessException(response.getStatus(),"获取access_token异常");
        }
        WXResponseBody body = JSONUtil.toBean(response.body(), WXResponseBody.class);
        if (!ObjectUtils.isEmpty(body.getErrcode())){
            throw new BusinessException(body.getErrcode(),body.getErrmsg());
        }
        return body;
    }

    /**
     * 响应报文
     */
    @Data
    public static class WXResponseBody{
        // 错误返回
        private Integer errcode;
        private String errmsg;
        // 正确返回
        private String accessToken;
        private String refreshToken;
        private String openid;
        private String scope;
        private String unionid;
    }

    /**
     * 开放平台
     */
    @Data
    public static class Open{
        /**
         * 开放平台获取access_token地址
         */
        private String OPEN_ACCESS_TOKEN_URL="https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";

        /**
         * 开放平台appid
         */
        private String openAppid = "xxxxxxxxxxxxxxxx";

        /**
         * 开放平台appsecret
         */
        private String openAppsecret = "xxxxxxxxxxxxxxxx";
    }

    /**
     * 小程序
     */
    @Data
    public static class Mini{
        /**
         * 小程序登录获取opendID地址
         */
        private String MINI_OPEN_ID_URL="https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";

        /**
         * 小程序appid
         */
        private String miniappid;

        /**
         * 小程序appsecret
         */
        private String miniappsecret;
    }

    /** 业务异常
     * @author  xiaochi
     * @date    2021/11/21 13:38
     * @desc    业务异常
     */
    public static class BusinessException extends RuntimeException {

        private static final long serialVersionUID = -6429106670540534165L;
        private int code;

        private String message;

        public BusinessException() {
        }

        public BusinessException(int code, String message) {
            super(message);
            this.code = code;
            this.message = message;
        }

        public BusinessException(int code, String message,Throwable cause) {
            super(message,cause);
            this.code = code;
            this.message = message;
        }

        public static void display(int code, String message) {
            throw new BusinessException(code, message);
        }

        public static void display(int code, String message,Throwable cause) {
            throw new BusinessException(code, message,cause);
        }

        public int getCode() {
            return code;
        }
    }
}

调用的时候直接传入临时凭证code与类型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值