Showing
1 changed file
with
19 additions
and
0 deletions
| ... | @@ -2,6 +2,7 @@ package com.infoloop.tianting.service.client; | ... | @@ -2,6 +2,7 @@ package com.infoloop.tianting.service.client; |
| 2 | 2 | ||
| 3 | import com.infoloop.tianting.model.dto.WxUserDto.SendSubscriptionMessageResponse; | 3 | import com.infoloop.tianting.model.dto.WxUserDto.SendSubscriptionMessageResponse; |
| 4 | import com.infoloop.tianting.model.dto.WxUserDto.WxUserTokenDto; | 4 | import com.infoloop.tianting.model.dto.WxUserDto.WxUserTokenDto; |
| 5 | +import org.springframework.lang.Nullable; | ||
| 5 | import com.infoloop.tianting.utils.JsonUtil; | 6 | import com.infoloop.tianting.utils.JsonUtil; |
| 6 | import lombok.RequiredArgsConstructor; | 7 | import lombok.RequiredArgsConstructor; |
| 7 | import lombok.extern.slf4j.Slf4j; | 8 | import lombok.extern.slf4j.Slf4j; |
| ... | @@ -51,10 +52,21 @@ public class WxOfficialAccountHttpClient { | ... | @@ -51,10 +52,21 @@ public class WxOfficialAccountHttpClient { |
| 51 | 52 | ||
| 52 | private final Object lock = new Object(); | 53 | private final Object lock = new Object(); |
| 53 | 54 | ||
| 55 | + private boolean isOfficialAccountConfigured() { | ||
| 56 | + return StringUtils.isNotBlank(appId) && StringUtils.isNotBlank(appSecret); | ||
| 57 | + } | ||
| 58 | + | ||
| 54 | /** | 59 | /** |
| 55 | * 获取公众号 access_token(带缓存) | 60 | * 获取公众号 access_token(带缓存) |
| 61 | + * | ||
| 62 | + * @return token;未配置 appId/appSecret 时返回 null(不请求微信接口) | ||
| 56 | */ | 63 | */ |
| 64 | + @Nullable | ||
| 57 | public String fetchStableAccessToken() { | 65 | public String fetchStableAccessToken() { |
| 66 | + if (!isOfficialAccountConfigured()) { | ||
| 67 | + log.warn("未配置 officialAccount.appId / officialAccount.appSecret,跳过获取公众号 access_token"); | ||
| 68 | + return null; | ||
| 69 | + } | ||
| 58 | log.info("Fetching Official Account access token..."); | 70 | log.info("Fetching Official Account access token..."); |
| 59 | String cachedToken = stringRedisTemplate.opsForValue().get(ACCESS_TOKEN_CACHE_KEY); | 71 | String cachedToken = stringRedisTemplate.opsForValue().get(ACCESS_TOKEN_CACHE_KEY); |
| 60 | if (StringUtils.isNotEmpty(cachedToken)) { | 72 | if (StringUtils.isNotEmpty(cachedToken)) { |
| ... | @@ -117,8 +129,15 @@ public class WxOfficialAccountHttpClient { | ... | @@ -117,8 +129,15 @@ public class WxOfficialAccountHttpClient { |
| 117 | Map<String, Object> data, | 129 | Map<String, Object> data, |
| 118 | String url, | 130 | String url, |
| 119 | Map<String, String> miniprogram) { | 131 | Map<String, String> miniprogram) { |
| 132 | + if (!isOfficialAccountConfigured()) { | ||
| 133 | + log.warn("未配置公众号,跳过发送订阅消息 openId={}", openId); | ||
| 134 | + return new SendSubscriptionMessageResponse(-1, "微信公众号未配置"); | ||
| 135 | + } | ||
| 120 | try { | 136 | try { |
| 121 | String accessToken = fetchStableAccessToken(); | 137 | String accessToken = fetchStableAccessToken(); |
| 138 | + if (accessToken == null) { | ||
| 139 | + return new SendSubscriptionMessageResponse(-1, "未获取到公众号 access_token"); | ||
| 140 | + } | ||
| 122 | HttpHeaders headers = new HttpHeaders(); | 141 | HttpHeaders headers = new HttpHeaders(); |
| 123 | headers.setContentType(MediaType.APPLICATION_JSON); | 142 | headers.setContentType(MediaType.APPLICATION_JSON); |
| 124 | 143 | ... | ... |
-
Please register or login to post a comment