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 9 import com.google.common.collect.Maps;
10 10 import lombok.extern.slf4j.Slf4j;
11 11 import me.chanjar.weixin.common.api.WxConsts;
12   -import me.chanjar.weixin.common.redis.JedisWxRedisOps;
13 12 import me.chanjar.weixin.mp.api.WxMpMessageRouter;
14 13 import me.chanjar.weixin.mp.api.WxMpService;
15 14 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
16 15 import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
17   -import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
18 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 18 import org.springframework.scheduling.annotation.Scheduled;
23   -import org.springframework.stereotype.Component;
24 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 23 import java.util.List;
29 24 import java.util.Map;
30 25 import java.util.stream.Collectors;
31 26  
32 27 // TODO @芋艿:思考有没更好的处理方式
33   -@Component
34   -@EnableConfigurationProperties(WxMpProperties.class)
35 28 @Slf4j
36   -public class WxMpConfig implements InitializingBean {
  29 +@Configuration
  30 +public class WxMpConfig {
37 31  
38 32 private static Map<String, WxMpMessageRouter> routers = Maps.newHashMap();
39 33 private static Map<String, WxMpService> mpServices = Maps.newHashMap();
40 34  
41   - @Autowired
  35 + @Resource
42 36 private WxAccountService wxAccountService;
43   - @Autowired
44   - private WxMpProperties wxMpProperties;
45 37  
46 38 private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
47 39  
48 40 /**
49 41 * 初始化公众号配置
50 42 */
  43 + @PostConstruct
51 44 public synchronized void initWxConfig() {
52 45 WxAccountExportReqVO req = new WxAccountExportReqVO();
53 46 List<WxAccountDO> wxAccountList = wxAccountService.getWxAccountList(req);
54 47 if (CollectionUtils.isEmpty(wxAccountList)) {
55 48 return;
56 49 }
57   - WxMpConfig.init(wxAccountList, wxMpProperties);
  50 + WxMpConfig.init(wxAccountList);
58 51 log.info("加载公众号配置成功");
59 52 }
60 53  
... ... @@ -64,20 +57,12 @@ public class WxMpConfig implements InitializingBean {
64 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 61 mpServices = wxAccountDOS.stream().map(wxAccountDO -> {
69 62 // TODO 亚洲:使用 WxMpInMemoryConfigStorage 的话,多节点会不会存在 accessToken 冲突
70 63  
71 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 67 configStorage.setAppId(wxAccountDO.getAppId());
83 68 configStorage.setSecret(wxAccountDO.getAppSecret());
... ... @@ -157,9 +142,4 @@ public class WxMpConfig implements InitializingBean {
157 142  
158 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 * wechat mp properties
8 8 */
9 9 @Data
10   -@ConfigurationProperties(prefix = "wx.mp")
  10 +//@ConfigurationProperties(prefix = "wx.mp")
11 11 public class WxMpProperties {
12 12 /**
13 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 34 @Component
35 35 @Slf4j
36 36 public class MsgHandler implements WxMpMessageHandler {
37   -
38   - @Autowired
39   - private WxReceiveTextService wxReceiveTextService;
40   -
41   - @Autowired
42   - private WxTextTemplateService wxTextTemplateService;
43   -
44 37 @Autowired
45 38 private WxAccountService wxAccountService;
46 39  
... ... @@ -50,9 +43,6 @@ public class MsgHandler implements WxMpMessageHandler {
50 43 @Resource
51 44 private FileApi fileApi;
52 45  
53   - @Autowired
54   - private WxMpProperties wxMpProperties;
55   -
56 46 @Override
57 47 public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
58 48 Map<String, Object> context, WxMpService weixinService,
... ... @@ -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 177 wx: # 参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md 文档
178 178 mp:
179 179 # 公众号配置(必填)
180   - app-id: wx041349c6f39b268b
  180 + appId: wx041349c6f39b268b
181 181 secret: 5abee519483bc9f8cb37ce280e814bd0
182 182 # 存储配置,解决 AccessToken 的跨节点的共享
183 183 config-storage:
184   - type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取
  184 + type: RedisTemplate # 配置类型: Memory(默认), Jedis, RedisTemplate
185 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  
... ...