微信登錄獲取openId文檔
小程序登錄獲取openId文檔
首先引入hutool
工具淳附,創(chuàng)建WXUtil.java
工具類(lèi)馍悟,內(nèi)容如下:
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;
// 開(kāi)放平臺(tái)
private static Open open = new Open();
// 小程序
private static Mini mini = new Mini();
/**
* 獲取微信access_token
* @param code 臨時(shí)憑證
* @param source 類(lèi)型(1微信2小程序)
* @return
*/
public static String getAccessToken(String code, Integer source){
return getRequestBody(code, source).getAccessToken();
}
/**
* 獲取微信openid
* @param code 臨時(shí)憑證
* @param source 類(lèi)型(1微信2小程序)
* @return
*/
public static String getOpenId(String code,Integer source){
return getRequestBody(code, source).getOpenid();
}
/**
* 獲取報(bào)文
* @param code 臨時(shí)憑證
* @param source 類(lèi)型(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);
}
/**
* 發(fā)起請(qǐng)求微信
* @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響應(yīng)報(bào)文:{}",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;
}
/**
* 響應(yīng)報(bào)文
*/
@Data
public static class WXResponseBody{
// 錯(cuò)誤返回
private Integer errcode;
private String errmsg;
// 正確返回
private String accessToken;
private String refreshToken;
private String openid;
private String scope;
private String unionid;
}
/**
* 開(kāi)放平臺(tái)
*/
@Data
public static class Open{
/**
* 開(kāi)放平臺(tái)獲取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";
/**
* 開(kāi)放平臺(tái)appid
*/
private String openAppid = "xxxxxxxxxxxxxxxx";
/**
* 開(kāi)放平臺(tái)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;
}
/** 業(yè)務(wù)異常
* @author xiaochi
* @date 2021/11/21 13:38
* @desc 業(yè)務(wù)異常
*/
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;
}
}
}
調(diào)用的時(shí)候直接傳入臨時(shí)憑證code與類(lèi)型。