微信 获取token

This commit is contained in:
2026-01-27 17:03:08 +08:00
parent 52e8a209fb
commit ae67fb6f8e
5 changed files with 69 additions and 102 deletions

View File

@@ -7,12 +7,9 @@ import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.czg.exception.CzgException;
import com.czg.service.RedisService;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@@ -41,9 +38,6 @@ public class WxAccountUtil {
@Value("${wx.ysk.warnMsgTmpId}")
private String warnMsgTmpId;
@Resource
@Lazy
private RedisService redisService;
static LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
@@ -97,16 +91,20 @@ public class WxAccountUtil {
}
public String getAccessToken() {
String resp = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", appId, secrete));
JSONObject respInfo = JSONObject.parseObject(resp);
if (!respInfo.containsKey("access_token")) {
log.warn("公众号获取token失败, 响应内容: {}", resp);
throw new RuntimeException(resp);
String resp = HttpUtil.get(StrUtil.format("https://access-token.sxczgkj.com/accessToken?appId={}&appSecret={}", appId, secrete));
// 响应 {"accessToken":"100_6C_jltHANT1y2Fot5PXKFDzPXTyWumCsao0oMoNRvJUTuxS0IOVO4nBmjdmx5dZfYItShFVSAKYzNDf7ZGLPlx52ii1Y1qerrbbSmIiLWCrec5qjBY4gV5Tfv8YKKTdABAEEN","appId":"wx212769170d2c6b2a"}
if(StrUtil.isNotBlank(resp)){
JSONObject respInfo = JSONObject.parseObject(resp);
return respInfo.getString("accessToken");
}else {
String resp1 = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", appId, secrete));
JSONObject respInfo = JSONObject.parseObject(resp1);
if (!respInfo.containsKey("access_token")) {
log.warn("银收客 公众号获取token失败, 响应内容: {}", resp1);
throw new CzgException(resp);
}
return respInfo.getString("access_token");
}
String accessToken = respInfo.getString("access_token");
// int expiresIn = respInfo.getInteger("expires_in");
// redisService.set("accessToken", accessToken, expiresIn - 10);
return accessToken;
}
public JSONObject sendTemplateMsg(String detail, String templateId, String toUserOpenId, Map<String, Object> data) {