Explorar o código

微信公众号获取配置内容提交

wangzl hai 7 meses
pai
achega
45f469cec0

+ 55 - 0
tr-modules/tr-module-smartFollowUp/src/main/java/cn/tr/module/smart/wx/config/WxMpConfiguration.java

@@ -0,0 +1,55 @@
+package cn.tr.module.smart.wx.config;
+
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
+import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * 微信 配置获取
+ *
+ * @author wangzl
+ */
+@Slf4j
+@Configuration
+@EnableConfigurationProperties(WxMaProperties.class)
+public class WxMpConfiguration {
+
+    private final WxMaProperties properties;
+
+    @Autowired
+    public WxMpConfiguration(WxMaProperties properties) {
+        this.properties = properties;
+    }
+
+    @Bean
+    public WxMpService wxMpService() {
+        // 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
+        List<WxMaProperties.Config> configs = this.properties.getConfigs();
+        if (configs == null) {
+            throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
+        }
+
+        WxMpService service = new WxMpServiceImpl();
+        service.setMultiConfigStorages(configs
+                .stream().map(a -> {
+                    WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
+                    configStorage.setAppId(a.getAppid());
+                    configStorage.setSecret(a.getSecret());
+                    configStorage.setToken(a.getToken());
+                    configStorage.setAesKey(a.getAesKey());
+                    return configStorage;
+                }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
+        return service;
+    }
+
+}