|
|
@@ -28,50 +28,43 @@ import java.util.stream.Collectors;
|
|
|
@Configuration
|
|
|
@EnableConfigurationProperties(WxMaProperties.class)
|
|
|
public class WxMaConfiguration {
|
|
|
- private WxMaProperties properties;
|
|
|
+ private static WxMaProperties properties;
|
|
|
|
|
|
- private static Map<String, WxMaMessageRouter> routers = Maps.newHashMap();
|
|
|
- private static Map<String, WxMaService> maServices = Maps.newHashMap();
|
|
|
+ private static WxMaMessageRouter router;
|
|
|
+ private static WxMaService maService;
|
|
|
|
|
|
@Autowired
|
|
|
public WxMaConfiguration(WxMaProperties properties) {
|
|
|
this.properties = properties;
|
|
|
}
|
|
|
|
|
|
- public static WxMaService getMaService(String appid) {
|
|
|
- WxMaService wxService = maServices.get(appid);
|
|
|
- if (wxService == null) {
|
|
|
- throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ if (properties == null) {
|
|
|
+ throw new RuntimeException("看下nacos是否有相关配置,注意别配错了!");
|
|
|
}
|
|
|
-
|
|
|
- return wxService;
|
|
|
+ WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
|
|
|
+ config.setAppid(properties.getAppid());
|
|
|
+ config.setSecret(properties.getSecret());
|
|
|
+ config.setToken(properties.getToken());
|
|
|
+ config.setAesKey(properties.getAesKey());
|
|
|
+ config.setMsgDataFormat(properties.getMsgDataFormat());
|
|
|
+ maService = new WxMaServiceImpl();
|
|
|
+ maService.setWxMaConfig(config);
|
|
|
+ router = newRouter(maService);
|
|
|
}
|
|
|
|
|
|
- public static WxMaMessageRouter getRouter(String appid) {
|
|
|
- return routers.get(appid);
|
|
|
- }
|
|
|
+ public static WxMaService getMaService() {
|
|
|
|
|
|
- @PostConstruct
|
|
|
- public void init() {
|
|
|
- List<WxMaProperties.Config> configs = this.properties.getConfigs();
|
|
|
- if (configs == null) {
|
|
|
- throw new RuntimeException("看下nacos是否有相关配置,注意别配错了!");
|
|
|
+ if (maService == null) {
|
|
|
+ throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", properties.getAppid()));
|
|
|
}
|
|
|
|
|
|
- maServices = configs.stream()
|
|
|
- .map(a -> {
|
|
|
- WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
|
|
|
- config.setAppid(a.getAppid());
|
|
|
- config.setSecret(a.getSecret());
|
|
|
- config.setToken(a.getToken());
|
|
|
- config.setAesKey(a.getAesKey());
|
|
|
- config.setMsgDataFormat(a.getMsgDataFormat());
|
|
|
-
|
|
|
- WxMaService service = new WxMaServiceImpl();
|
|
|
- service.setWxMaConfig(config);
|
|
|
- routers.put(a.getAppid(), this.newRouter(service));
|
|
|
- return service;
|
|
|
- }).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a));
|
|
|
+ return maService;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static WxMaMessageRouter getRouter(String appid) {
|
|
|
+ return router;
|
|
|
}
|
|
|
|
|
|
private WxMaMessageRouter newRouter(WxMaService service) {
|