mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 02:08:43 +08:00 
			
		
		
		
	refactor(yudao-module-mp-biz): 微信公众号配置重构
This commit is contained in:
		| @@ -9,52 +9,45 @@ import cn.iocoder.yudao.module.mp.service.account.WxAccountService; | ||||
| import com.google.common.collect.Maps; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import me.chanjar.weixin.common.api.WxConsts; | ||||
| import me.chanjar.weixin.common.redis.JedisWxRedisOps; | ||||
| import me.chanjar.weixin.mp.api.WxMpMessageRouter; | ||||
| import me.chanjar.weixin.mp.api.WxMpService; | ||||
| import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; | ||||
| import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; | ||||
| import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl; | ||||
| import me.chanjar.weixin.mp.constant.WxMpEventConstants; | ||||
| import org.springframework.beans.factory.InitializingBean; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.scheduling.annotation.Scheduled; | ||||
| import org.springframework.stereotype.Component; | ||||
| import org.springframework.util.CollectionUtils; | ||||
| import redis.clients.jedis.JedisPool; | ||||
| import redis.clients.jedis.JedisPoolConfig; | ||||
|  | ||||
| import javax.annotation.PostConstruct; | ||||
| import javax.annotation.Resource; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.stream.Collectors; | ||||
|  | ||||
| // TODO @芋艿:思考有没更好的处理方式 | ||||
| @Component | ||||
| @EnableConfigurationProperties(WxMpProperties.class) | ||||
| @Slf4j | ||||
| public class WxMpConfig implements InitializingBean { | ||||
| @Configuration | ||||
| public class WxMpConfig { | ||||
|  | ||||
|     private static Map<String, WxMpMessageRouter> routers = Maps.newHashMap(); | ||||
|     private static Map<String, WxMpService> mpServices = Maps.newHashMap(); | ||||
|  | ||||
|     @Autowired | ||||
|     @Resource | ||||
|     private WxAccountService wxAccountService; | ||||
|     @Autowired | ||||
|     private WxMpProperties wxMpProperties; | ||||
|  | ||||
|     private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L; | ||||
|  | ||||
|     /** | ||||
|      * 初始化公众号配置 | ||||
|      */ | ||||
|     @PostConstruct | ||||
|     public synchronized void initWxConfig() { | ||||
|         WxAccountExportReqVO req = new WxAccountExportReqVO(); | ||||
|         List<WxAccountDO> wxAccountList = wxAccountService.getWxAccountList(req); | ||||
|         if (CollectionUtils.isEmpty(wxAccountList)) { | ||||
|             return; | ||||
|         } | ||||
|         WxMpConfig.init(wxAccountList, wxMpProperties); | ||||
|         WxMpConfig.init(wxAccountList); | ||||
|         log.info("加载公众号配置成功"); | ||||
|     } | ||||
|  | ||||
| @@ -64,20 +57,12 @@ public class WxMpConfig implements InitializingBean { | ||||
|         initWxConfig(); | ||||
|     } | ||||
|  | ||||
|     public static void init(List<WxAccountDO> wxAccountDOS, WxMpProperties properties) { | ||||
|     public static void init(List<WxAccountDO> wxAccountDOS) { | ||||
|         mpServices = wxAccountDOS.stream().map(wxAccountDO -> { | ||||
|             // TODO 亚洲:使用 WxMpInMemoryConfigStorage 的话,多节点会不会存在 accessToken 冲突 | ||||
|  | ||||
|             WxMpDefaultConfigImpl configStorage; | ||||
|             if (properties.isUseRedis()) { | ||||
|                 final WxMpProperties.RedisConfig redisConfig = properties.getRedisConfig(); | ||||
|                 JedisPoolConfig poolConfig = new JedisPoolConfig(); | ||||
|                 JedisPool jedisPool = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(), | ||||
|                         redisConfig.getTimeout(), redisConfig.getPassword()); | ||||
|                 configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), wxAccountDO.getAppId()); | ||||
|             } else { | ||||
|                 configStorage = new WxMpDefaultConfigImpl(); | ||||
|             } | ||||
|             configStorage = new WxMpDefaultConfigImpl(); | ||||
|  | ||||
|             configStorage.setAppId(wxAccountDO.getAppId()); | ||||
|             configStorage.setSecret(wxAccountDO.getAppSecret()); | ||||
| @@ -157,9 +142,4 @@ public class WxMpConfig implements InitializingBean { | ||||
|  | ||||
|         return newRouter; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void afterPropertiesSet() throws Exception { | ||||
|         initWxConfig(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -7,7 +7,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; | ||||
|  * wechat mp properties | ||||
|  */ | ||||
| @Data | ||||
| @ConfigurationProperties(prefix = "wx.mp") | ||||
| //@ConfigurationProperties(prefix = "wx.mp") | ||||
| public class WxMpProperties { | ||||
|     /** | ||||
|      * 是否使用redis存储access token | ||||
|   | ||||
| @@ -34,13 +34,6 @@ import java.util.Map; | ||||
| @Component | ||||
| @Slf4j | ||||
| public class MsgHandler implements WxMpMessageHandler { | ||||
|  | ||||
|     @Autowired | ||||
|     private WxReceiveTextService wxReceiveTextService; | ||||
|  | ||||
|     @Autowired | ||||
|     private WxTextTemplateService wxTextTemplateService; | ||||
|  | ||||
|     @Autowired | ||||
|     private WxAccountService wxAccountService; | ||||
|  | ||||
| @@ -50,9 +43,6 @@ public class MsgHandler implements WxMpMessageHandler { | ||||
|     @Resource | ||||
|     private FileApi fileApi; | ||||
|  | ||||
|     @Autowired | ||||
|     private WxMpProperties wxMpProperties; | ||||
|  | ||||
|     @Override | ||||
|     public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, | ||||
|                                     Map<String, Object> context, WxMpService weixinService, | ||||
| @@ -147,8 +137,7 @@ public class MsgHandler implements WxMpMessageHandler { | ||||
|         } | ||||
|  | ||||
|         //组装默认回复消息 | ||||
|         String content = wxMpProperties.getDefaultContent();//默认 | ||||
|         return new TextBuilder().build(content, wxMessage, weixinService); | ||||
|         return new TextBuilder().build("测试", wxMessage, weixinService); | ||||
|     } | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 fengdan
					fengdan