Merge branch 'cc_20251031_bugfix' into 'master'
feat:WechatTemplateService See merge request hangzhou/java/custom_zxjp!180
This commit is contained in:
@@ -7,12 +7,14 @@ import com.cool.store.dto.wechat.WechatTemplateMessageDTO;
|
|||||||
import com.cool.store.dto.wechat.WechatUserInfoDTO;
|
import com.cool.store.dto.wechat.WechatUserInfoDTO;
|
||||||
import com.cool.store.enums.wechat.WechatTemplateEnum;
|
import com.cool.store.enums.wechat.WechatTemplateEnum;
|
||||||
import com.cool.store.utils.OkHttpUtil;
|
import com.cool.store.utils.OkHttpUtil;
|
||||||
|
import com.cool.store.utils.RedisUtilPool;
|
||||||
import com.cool.store.utils.poi.StringUtils;
|
import com.cool.store.utils.poi.StringUtils;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -26,6 +28,8 @@ import java.util.Map;
|
|||||||
public class WechatTemplateService {
|
public class WechatTemplateService {
|
||||||
|
|
||||||
|
|
||||||
|
private static final String ACCESS_TOKEN_KEY = "wechat_service_account_access_token";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WechatMpProperties wechatMpProperties;
|
private WechatMpProperties wechatMpProperties;
|
||||||
|
|
||||||
@@ -36,9 +40,16 @@ public class WechatTemplateService {
|
|||||||
private OkHttpUtil okHttpUtil;
|
private OkHttpUtil okHttpUtil;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ObjectMapper objectMapper;
|
private ObjectMapper objectMapper;
|
||||||
|
@Resource
|
||||||
|
private RedisUtilPool redisUtilPool;
|
||||||
|
|
||||||
|
|
||||||
public String getAccessToken() {
|
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",
|
String url = String.format("%s?grant_type=client_credential&appid=%s&secret=%s",
|
||||||
wechatMpProperties.getAccessTokenUrl(),
|
wechatMpProperties.getAccessTokenUrl(),
|
||||||
wechatMpProperties.getAppId(),
|
wechatMpProperties.getAppId(),
|
||||||
@@ -49,7 +60,14 @@ public class WechatTemplateService {
|
|||||||
|
|
||||||
if (StringUtils.isNotEmpty( result)){
|
if (StringUtils.isNotEmpty( result)){
|
||||||
AccessTokenDTO responseDTO = objectMapper.readValue(result, AccessTokenDTO.class);
|
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;
|
return null;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user