Commit f788258309d73d835761cf87ba426eb8a96c0819

Authored by fengdan
1 parent 89a5ea19

refactor(yudao-module-mp-biz): 微信公众号配置重构

yudao-module-wechat/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/config/WxMpConfig.java
@@ -9,52 +9,45 @@ import cn.iocoder.yudao.module.mp.service.account.WxAccountService; @@ -9,52 +9,45 @@ import cn.iocoder.yudao.module.mp.service.account.WxAccountService;
9 import com.google.common.collect.Maps; 9 import com.google.common.collect.Maps;
10 import lombok.extern.slf4j.Slf4j; 10 import lombok.extern.slf4j.Slf4j;
11 import me.chanjar.weixin.common.api.WxConsts; 11 import me.chanjar.weixin.common.api.WxConsts;
12 -import me.chanjar.weixin.common.redis.JedisWxRedisOps;  
13 import me.chanjar.weixin.mp.api.WxMpMessageRouter; 12 import me.chanjar.weixin.mp.api.WxMpMessageRouter;
14 import me.chanjar.weixin.mp.api.WxMpService; 13 import me.chanjar.weixin.mp.api.WxMpService;
15 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; 14 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
16 import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; 15 import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
17 -import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;  
18 import me.chanjar.weixin.mp.constant.WxMpEventConstants; 16 import me.chanjar.weixin.mp.constant.WxMpEventConstants;
19 -import org.springframework.beans.factory.InitializingBean;  
20 -import org.springframework.beans.factory.annotation.Autowired;  
21 -import org.springframework.boot.context.properties.EnableConfigurationProperties; 17 +import org.springframework.context.annotation.Configuration;
22 import org.springframework.scheduling.annotation.Scheduled; 18 import org.springframework.scheduling.annotation.Scheduled;
23 -import org.springframework.stereotype.Component;  
24 import org.springframework.util.CollectionUtils; 19 import org.springframework.util.CollectionUtils;
25 -import redis.clients.jedis.JedisPool;  
26 -import redis.clients.jedis.JedisPoolConfig;  
27 20
  21 +import javax.annotation.PostConstruct;
  22 +import javax.annotation.Resource;
28 import java.util.List; 23 import java.util.List;
29 import java.util.Map; 24 import java.util.Map;
30 import java.util.stream.Collectors; 25 import java.util.stream.Collectors;
31 26
32 // TODO @芋艿:思考有没更好的处理方式 27 // TODO @芋艿:思考有没更好的处理方式
33 -@Component  
34 -@EnableConfigurationProperties(WxMpProperties.class)  
35 @Slf4j 28 @Slf4j
36 -public class WxMpConfig implements InitializingBean { 29 +@Configuration
  30 +public class WxMpConfig {
37 31
38 private static Map<String, WxMpMessageRouter> routers = Maps.newHashMap(); 32 private static Map<String, WxMpMessageRouter> routers = Maps.newHashMap();
39 private static Map<String, WxMpService> mpServices = Maps.newHashMap(); 33 private static Map<String, WxMpService> mpServices = Maps.newHashMap();
40 34
41 - @Autowired 35 + @Resource
42 private WxAccountService wxAccountService; 36 private WxAccountService wxAccountService;
43 - @Autowired  
44 - private WxMpProperties wxMpProperties;  
45 37
46 private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L; 38 private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
47 39
48 /** 40 /**
49 * 初始化公众号配置 41 * 初始化公众号配置
50 */ 42 */
  43 + @PostConstruct
51 public synchronized void initWxConfig() { 44 public synchronized void initWxConfig() {
52 WxAccountExportReqVO req = new WxAccountExportReqVO(); 45 WxAccountExportReqVO req = new WxAccountExportReqVO();
53 List<WxAccountDO> wxAccountList = wxAccountService.getWxAccountList(req); 46 List<WxAccountDO> wxAccountList = wxAccountService.getWxAccountList(req);
54 if (CollectionUtils.isEmpty(wxAccountList)) { 47 if (CollectionUtils.isEmpty(wxAccountList)) {
55 return; 48 return;
56 } 49 }
57 - WxMpConfig.init(wxAccountList, wxMpProperties); 50 + WxMpConfig.init(wxAccountList);
58 log.info("加载公众号配置成功"); 51 log.info("加载公众号配置成功");
59 } 52 }
60 53
@@ -64,20 +57,12 @@ public class WxMpConfig implements InitializingBean { @@ -64,20 +57,12 @@ public class WxMpConfig implements InitializingBean {
64 initWxConfig(); 57 initWxConfig();
65 } 58 }
66 59
67 - public static void init(List<WxAccountDO> wxAccountDOS, WxMpProperties properties) { 60 + public static void init(List<WxAccountDO> wxAccountDOS) {
68 mpServices = wxAccountDOS.stream().map(wxAccountDO -> { 61 mpServices = wxAccountDOS.stream().map(wxAccountDO -> {
69 // TODO 亚洲:使用 WxMpInMemoryConfigStorage 的话,多节点会不会存在 accessToken 冲突 62 // TODO 亚洲:使用 WxMpInMemoryConfigStorage 的话,多节点会不会存在 accessToken 冲突
70 63
71 WxMpDefaultConfigImpl configStorage; 64 WxMpDefaultConfigImpl configStorage;
72 - if (properties.isUseRedis()) {  
73 - final WxMpProperties.RedisConfig redisConfig = properties.getRedisConfig();  
74 - JedisPoolConfig poolConfig = new JedisPoolConfig();  
75 - JedisPool jedisPool = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),  
76 - redisConfig.getTimeout(), redisConfig.getPassword());  
77 - configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), wxAccountDO.getAppId());  
78 - } else {  
79 - configStorage = new WxMpDefaultConfigImpl();  
80 - } 65 + configStorage = new WxMpDefaultConfigImpl();
81 66
82 configStorage.setAppId(wxAccountDO.getAppId()); 67 configStorage.setAppId(wxAccountDO.getAppId());
83 configStorage.setSecret(wxAccountDO.getAppSecret()); 68 configStorage.setSecret(wxAccountDO.getAppSecret());
@@ -157,9 +142,4 @@ public class WxMpConfig implements InitializingBean { @@ -157,9 +142,4 @@ public class WxMpConfig implements InitializingBean {
157 142
158 return newRouter; 143 return newRouter;
159 } 144 }
160 -  
161 - @Override  
162 - public void afterPropertiesSet() throws Exception {  
163 - initWxConfig();  
164 - }  
165 } 145 }
yudao-module-wechat/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/config/WxMpProperties.java
@@ -7,7 +7,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @@ -7,7 +7,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
7 * wechat mp properties 7 * wechat mp properties
8 */ 8 */
9 @Data 9 @Data
10 -@ConfigurationProperties(prefix = "wx.mp") 10 +//@ConfigurationProperties(prefix = "wx.mp")
11 public class WxMpProperties { 11 public class WxMpProperties {
12 /** 12 /**
13 * 是否使用redis存储access token 13 * 是否使用redis存储access token
yudao-module-wechat/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/handler/MsgHandler.java
@@ -34,13 +34,6 @@ import java.util.Map; @@ -34,13 +34,6 @@ import java.util.Map;
34 @Component 34 @Component
35 @Slf4j 35 @Slf4j
36 public class MsgHandler implements WxMpMessageHandler { 36 public class MsgHandler implements WxMpMessageHandler {
37 -  
38 - @Autowired  
39 - private WxReceiveTextService wxReceiveTextService;  
40 -  
41 - @Autowired  
42 - private WxTextTemplateService wxTextTemplateService;  
43 -  
44 @Autowired 37 @Autowired
45 private WxAccountService wxAccountService; 38 private WxAccountService wxAccountService;
46 39
@@ -50,9 +43,6 @@ public class MsgHandler implements WxMpMessageHandler { @@ -50,9 +43,6 @@ public class MsgHandler implements WxMpMessageHandler {
50 @Resource 43 @Resource
51 private FileApi fileApi; 44 private FileApi fileApi;
52 45
53 - @Autowired  
54 - private WxMpProperties wxMpProperties;  
55 -  
56 @Override 46 @Override
57 public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, 47 public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
58 Map<String, Object> context, WxMpService weixinService, 48 Map<String, Object> context, WxMpService weixinService,
@@ -147,8 +137,7 @@ public class MsgHandler implements WxMpMessageHandler { @@ -147,8 +137,7 @@ public class MsgHandler implements WxMpMessageHandler {
147 } 137 }
148 138
149 //组装默认回复消息 139 //组装默认回复消息
150 - String content = wxMpProperties.getDefaultContent();//默认  
151 - return new TextBuilder().build(content, wxMessage, weixinService); 140 + return new TextBuilder().build("测试", wxMessage, weixinService);
152 } 141 }
153 142
154 143
yudao-server/src/main/resources/application-local.yaml
@@ -177,13 +177,13 @@ logging: @@ -177,13 +177,13 @@ logging:
177 wx: # 参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md 文档 177 wx: # 参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md 文档
178 mp: 178 mp:
179 # 公众号配置(必填) 179 # 公众号配置(必填)
180 - app-id: wx041349c6f39b268b 180 + appId: wx041349c6f39b268b
181 secret: 5abee519483bc9f8cb37ce280e814bd0 181 secret: 5abee519483bc9f8cb37ce280e814bd0
182 # 存储配置,解决 AccessToken 的跨节点的共享 182 # 存储配置,解决 AccessToken 的跨节点的共享
183 config-storage: 183 config-storage:
184 - type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取 184 + type: RedisTemplate # 配置类型: Memory(默认), Jedis, RedisTemplate
185 key-prefix: wx # Redis Key 的前缀 TODO 芋艿:解决下 Redis key 管理的配置 185 key-prefix: wx # Redis Key 的前缀 TODO 芋艿:解决下 Redis key 管理的配置
186 - http-client-type: HttpClient # 采用 HttpClient 请求微信公众号平台 186 + http-client-type: HttpClient # http客户端类型: HttpClient(默认), OkHttp, JoddHttp
187 187
188 --- #################### 芋道相关配置 #################### 188 --- #################### 芋道相关配置 ####################
189 189