Showing
2 changed files
with
15 additions
and
12 deletions
| ... | @@ -83,9 +83,7 @@ public interface ConfigConstants { | ... | @@ -83,9 +83,7 @@ public interface ConfigConstants { |
| 83 | String MINI_PROGRAM_APP_SECRET = "${miniprogram.appSecret}"; | 83 | String MINI_PROGRAM_APP_SECRET = "${miniprogram.appSecret}"; |
| 84 | String MINI_PROGRAM_ORDER_REMINDER_TEMPLATE_ID = "${miniprogram.orderReminderTemplateId}"; | 84 | String MINI_PROGRAM_ORDER_REMINDER_TEMPLATE_ID = "${miniprogram.orderReminderTemplateId}"; |
| 85 | 85 | ||
| 86 | - // 公众号配置(用于订阅消息推送) | 86 | + // 公众号模板 ID(可选);appId/appSecret 见 WxOfficialAccountHttpClient 字段上的 @Value 默认值 |
| 87 | - String OFFICIAL_ACCOUNT_APP_ID = "${officialAccount.appId}"; | 87 | + String OFFICIAL_ACCOUNT_ORDER_REMINDER_TEMPLATE_ID = "${officialAccount.orderReminderTemplateId:}"; |
| 88 | - String OFFICIAL_ACCOUNT_APP_SECRET = "${officialAccount.appSecret}"; | ||
| 89 | - String OFFICIAL_ACCOUNT_ORDER_REMINDER_TEMPLATE_ID = "${officialAccount.orderReminderTemplateId}"; | ||
| 90 | 88 | ||
| 91 | } | 89 | } | ... | ... |
| ... | @@ -8,7 +8,7 @@ import lombok.RequiredArgsConstructor; | ... | @@ -8,7 +8,7 @@ import lombok.RequiredArgsConstructor; |
| 8 | import lombok.extern.slf4j.Slf4j; | 8 | import lombok.extern.slf4j.Slf4j; |
| 9 | import org.apache.commons.lang3.StringUtils; | 9 | import org.apache.commons.lang3.StringUtils; |
| 10 | import org.springframework.beans.factory.annotation.Autowired; | 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | -import org.springframework.beans.factory.annotation.Value; | 11 | +import org.springframework.core.env.Environment; |
| 12 | import org.springframework.data.redis.core.StringRedisTemplate; | 12 | import org.springframework.data.redis.core.StringRedisTemplate; |
| 13 | import org.springframework.http.HttpEntity; | 13 | import org.springframework.http.HttpEntity; |
| 14 | import org.springframework.http.HttpHeaders; | 14 | import org.springframework.http.HttpHeaders; |
| ... | @@ -17,13 +17,12 @@ import org.springframework.http.ResponseEntity; | ... | @@ -17,13 +17,12 @@ import org.springframework.http.ResponseEntity; |
| 17 | import org.springframework.stereotype.Service; | 17 | import org.springframework.stereotype.Service; |
| 18 | import org.springframework.web.client.RestTemplate; | 18 | import org.springframework.web.client.RestTemplate; |
| 19 | 19 | ||
| 20 | +import javax.annotation.PostConstruct; | ||
| 21 | + | ||
| 20 | import java.util.HashMap; | 22 | import java.util.HashMap; |
| 21 | import java.util.Map; | 23 | import java.util.Map; |
| 22 | import java.util.concurrent.TimeUnit; | 24 | import java.util.concurrent.TimeUnit; |
| 23 | 25 | ||
| 24 | -import static com.infoloop.tianting.constant.ConfigConstants.OFFICIAL_ACCOUNT_APP_ID; | ||
| 25 | -import static com.infoloop.tianting.constant.ConfigConstants.OFFICIAL_ACCOUNT_APP_SECRET; | ||
| 26 | - | ||
| 27 | /** | 26 | /** |
| 28 | * 微信公众号 HTTP 客户端 | 27 | * 微信公众号 HTTP 客户端 |
| 29 | * 用于发送公众号一次性订阅消息 | 28 | * 用于发送公众号一次性订阅消息 |
| ... | @@ -43,15 +42,21 @@ public class WxOfficialAccountHttpClient { | ... | @@ -43,15 +42,21 @@ public class WxOfficialAccountHttpClient { |
| 43 | 42 | ||
| 44 | private final RestTemplate restTemplate; | 43 | private final RestTemplate restTemplate; |
| 45 | private final StringRedisTemplate stringRedisTemplate; | 44 | private final StringRedisTemplate stringRedisTemplate; |
| 45 | + private final Environment environment; | ||
| 46 | 46 | ||
| 47 | - @Value(OFFICIAL_ACCOUNT_APP_ID) | 47 | + /** 由 {@link #bindOfficialAccountConfig()} 从 Environment 读取;缺失配置项时为空白,不触发占位符解析 */ |
| 48 | - private String appId; | 48 | + private String appId = ""; |
| 49 | 49 | ||
| 50 | - @Value(OFFICIAL_ACCOUNT_APP_SECRET) | 50 | + private String appSecret = ""; |
| 51 | - private String appSecret; | ||
| 52 | 51 | ||
| 53 | private final Object lock = new Object(); | 52 | private final Object lock = new Object(); |
| 54 | 53 | ||
| 54 | + @PostConstruct | ||
| 55 | + void bindOfficialAccountConfig() { | ||
| 56 | + this.appId = StringUtils.trimToEmpty(environment.getProperty("officialAccount.appId")); | ||
| 57 | + this.appSecret = StringUtils.trimToEmpty(environment.getProperty("officialAccount.appSecret")); | ||
| 58 | + } | ||
| 59 | + | ||
| 55 | private boolean isOfficialAccountConfigured() { | 60 | private boolean isOfficialAccountConfigured() { |
| 56 | return StringUtils.isNotBlank(appId) && StringUtils.isNotBlank(appSecret); | 61 | return StringUtils.isNotBlank(appId) && StringUtils.isNotBlank(appSecret); |
| 57 | } | 62 | } | ... | ... |
-
Please register or login to post a comment