Merge branch 'cc_20251031_bugfix' into 'master'

feat:WechatTemplateService

See merge request hangzhou/java/custom_zxjp!180
This commit is contained in:
苏竹红
2025-10-31 09:27:25 +00:00

View File

@@ -7,12 +7,14 @@ import com.cool.store.dto.wechat.WechatTemplateMessageDTO;
import com.cool.store.dto.wechat.WechatUserInfoDTO;
import com.cool.store.enums.wechat.WechatTemplateEnum;
import com.cool.store.utils.OkHttpUtil;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.utils.poi.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;
@@ -26,6 +28,8 @@ import java.util.Map;
public class WechatTemplateService {
private static final String ACCESS_TOKEN_KEY = "wechat_service_account_access_token";
@Autowired
private WechatMpProperties wechatMpProperties;
@@ -36,9 +40,16 @@ public class WechatTemplateService {
private OkHttpUtil okHttpUtil;
@Autowired
private ObjectMapper objectMapper;
@Resource
private RedisUtilPool redisUtilPool;
public String getAccessToken() {
String cachedToken = redisUtilPool.getString(ACCESS_TOKEN_KEY);
if (StringUtils.isNotEmpty(cachedToken)) {
log.info("从 Redis 获取 access_token: {}", cachedToken);
return cachedToken;
}
String url = String.format("%s?grant_type=client_credential&appid=%s&secret=%s",
wechatMpProperties.getAccessTokenUrl(),
wechatMpProperties.getAppId(),
@@ -49,7 +60,14 @@ public class WechatTemplateService {
if (StringUtils.isNotEmpty( result)){
AccessTokenDTO responseDTO = objectMapper.readValue(result, AccessTokenDTO.class);
return responseDTO.getAccess_token();
String accessToken = responseDTO.getAccess_token();
// 将获取到的 token 存入 Redis设置过期时间微信 token 有效期 7200 秒,这里设置 7000 秒)
if (StringUtils.isNotEmpty(accessToken)) {
redisUtilPool.setString(ACCESS_TOKEN_KEY, accessToken, 7000);
log.info("将 access_token 存入 Redis: {}", accessToken);
}
return accessToken;
}
return null;
} catch (IOException e) {