微信与小程序获取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与类型。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容