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

This commit is contained in:
fengdan 2022-06-16 11:22:48 +08:00
parent 89a5ea1939
commit f788258309
4 changed files with 15 additions and 46 deletions

View File

@ -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.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();
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -177,13 +177,13 @@ logging:
wx: # 参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md 文档
mp:
# 公众号配置(必填)
app-id: wx041349c6f39b268b
appId: wx041349c6f39b268b
secret: 5abee519483bc9f8cb37ce280e814bd0
# 存储配置,解决 AccessToken 的跨节点的共享
config-storage:
type: RedisTemplate # 采用 RedisTemplate 操作 Redis会自动从 Spring 中获取
type: RedisTemplate # 配置类型: Memory(默认), Jedis, RedisTemplate
key-prefix: wx # Redis Key 的前缀 TODO 芋艿:解决下 Redis key 管理的配置
http-client-type: HttpClient # 采用 HttpClient 请求微信公众号平台
http-client-type: HttpClient # http客户端类型: HttpClient(默认), OkHttp, JoddHttp
--- #################### 芋道相关配置 ####################