孟苏慧

fix

......@@ -83,9 +83,7 @@ public interface ConfigConstants {
String MINI_PROGRAM_APP_SECRET = "${miniprogram.appSecret}";
String MINI_PROGRAM_ORDER_REMINDER_TEMPLATE_ID = "${miniprogram.orderReminderTemplateId}";
// 公众号配置(用于订阅消息推送)
String OFFICIAL_ACCOUNT_APP_ID = "${officialAccount.appId}";
String OFFICIAL_ACCOUNT_APP_SECRET = "${officialAccount.appSecret}";
String OFFICIAL_ACCOUNT_ORDER_REMINDER_TEMPLATE_ID = "${officialAccount.orderReminderTemplateId}";
// 公众号模板 ID(可选);appId/appSecret 见 WxOfficialAccountHttpClient 字段上的 @Value 默认值
String OFFICIAL_ACCOUNT_ORDER_REMINDER_TEMPLATE_ID = "${officialAccount.orderReminderTemplateId:}";
}
......
......@@ -8,7 +8,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
......@@ -17,13 +17,12 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.infoloop.tianting.constant.ConfigConstants.OFFICIAL_ACCOUNT_APP_ID;
import static com.infoloop.tianting.constant.ConfigConstants.OFFICIAL_ACCOUNT_APP_SECRET;
/**
* 微信公众号 HTTP 客户端
* 用于发送公众号一次性订阅消息
......@@ -43,15 +42,21 @@ public class WxOfficialAccountHttpClient {
private final RestTemplate restTemplate;
private final StringRedisTemplate stringRedisTemplate;
private final Environment environment;
@Value(OFFICIAL_ACCOUNT_APP_ID)
private String appId;
/** 由 {@link #bindOfficialAccountConfig()} 从 Environment 读取;缺失配置项时为空白,不触发占位符解析 */
private String appId = "";
@Value(OFFICIAL_ACCOUNT_APP_SECRET)
private String appSecret;
private String appSecret = "";
private final Object lock = new Object();
@PostConstruct
void bindOfficialAccountConfig() {
this.appId = StringUtils.trimToEmpty(environment.getProperty("officialAccount.appId"));
this.appSecret = StringUtils.trimToEmpty(environment.getProperty("officialAccount.appSecret"));
}
private boolean isOfficialAccountConfigured() {
return StringUtils.isNotBlank(appId) && StringUtils.isNotBlank(appSecret);
}
......