孟苏慧

fix

......@@ -2,6 +2,7 @@ package com.infoloop.tianting.service.client;
import com.infoloop.tianting.model.dto.WxUserDto.SendSubscriptionMessageResponse;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserTokenDto;
import org.springframework.lang.Nullable;
import com.infoloop.tianting.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
......@@ -51,10 +52,21 @@ public class WxOfficialAccountHttpClient {
private final Object lock = new Object();
private boolean isOfficialAccountConfigured() {
return StringUtils.isNotBlank(appId) && StringUtils.isNotBlank(appSecret);
}
/**
* 获取公众号 access_token(带缓存)
*
* @return token;未配置 appId/appSecret 时返回 null(不请求微信接口)
*/
@Nullable
public String fetchStableAccessToken() {
if (!isOfficialAccountConfigured()) {
log.warn("未配置 officialAccount.appId / officialAccount.appSecret,跳过获取公众号 access_token");
return null;
}
log.info("Fetching Official Account access token...");
String cachedToken = stringRedisTemplate.opsForValue().get(ACCESS_TOKEN_CACHE_KEY);
if (StringUtils.isNotEmpty(cachedToken)) {
......@@ -117,8 +129,15 @@ public class WxOfficialAccountHttpClient {
Map<String, Object> data,
String url,
Map<String, String> miniprogram) {
if (!isOfficialAccountConfigured()) {
log.warn("未配置公众号,跳过发送订阅消息 openId={}", openId);
return new SendSubscriptionMessageResponse(-1, "微信公众号未配置");
}
try {
String accessToken = fetchStableAccessToken();
if (accessToken == null) {
return new SendSubscriptionMessageResponse(-1, "未获取到公众号 access_token");
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
......