refactor(yudao-module-mp-biz): 微信公众号配置重构
parent
89a5ea1939
commit
f788258309
|
@ -9,52 +9,45 @@ import cn.iocoder.yudao.module.mp.service.account.WxAccountService;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.chanjar.weixin.common.api.WxConsts;
|
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.WxMpMessageRouter;
|
||||||
import me.chanjar.weixin.mp.api.WxMpService;
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
||||||
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
||||||
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
|
|
||||||
import me.chanjar.weixin.mp.constant.WxMpEventConstants;
|
import me.chanjar.weixin.mp.constant.WxMpEventConstants;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
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.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
// TODO @芋艿:思考有没更好的处理方式
|
// TODO @芋艿:思考有没更好的处理方式
|
||||||
@Component
|
|
||||||
@EnableConfigurationProperties(WxMpProperties.class)
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class WxMpConfig implements InitializingBean {
|
@Configuration
|
||||||
|
public class WxMpConfig {
|
||||||
|
|
||||||
private static Map<String, WxMpMessageRouter> routers = Maps.newHashMap();
|
private static Map<String, WxMpMessageRouter> routers = Maps.newHashMap();
|
||||||
private static Map<String, WxMpService> mpServices = Maps.newHashMap();
|
private static Map<String, WxMpService> mpServices = Maps.newHashMap();
|
||||||
|
|
||||||
@Autowired
|
@Resource
|
||||||
private WxAccountService wxAccountService;
|
private WxAccountService wxAccountService;
|
||||||
@Autowired
|
|
||||||
private WxMpProperties wxMpProperties;
|
|
||||||
|
|
||||||
private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
|
private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化公众号配置
|
* 初始化公众号配置
|
||||||
*/
|
*/
|
||||||
|
@PostConstruct
|
||||||
public synchronized void initWxConfig() {
|
public synchronized void initWxConfig() {
|
||||||
WxAccountExportReqVO req = new WxAccountExportReqVO();
|
WxAccountExportReqVO req = new WxAccountExportReqVO();
|
||||||
List<WxAccountDO> wxAccountList = wxAccountService.getWxAccountList(req);
|
List<WxAccountDO> wxAccountList = wxAccountService.getWxAccountList(req);
|
||||||
if (CollectionUtils.isEmpty(wxAccountList)) {
|
if (CollectionUtils.isEmpty(wxAccountList)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
WxMpConfig.init(wxAccountList, wxMpProperties);
|
WxMpConfig.init(wxAccountList);
|
||||||
log.info("加载公众号配置成功");
|
log.info("加载公众号配置成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,20 +57,12 @@ public class WxMpConfig implements InitializingBean {
|
||||||
initWxConfig();
|
initWxConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void init(List<WxAccountDO> wxAccountDOS, WxMpProperties properties) {
|
public static void init(List<WxAccountDO> wxAccountDOS) {
|
||||||
mpServices = wxAccountDOS.stream().map(wxAccountDO -> {
|
mpServices = wxAccountDOS.stream().map(wxAccountDO -> {
|
||||||
// TODO 亚洲:使用 WxMpInMemoryConfigStorage 的话,多节点会不会存在 accessToken 冲突
|
// TODO 亚洲:使用 WxMpInMemoryConfigStorage 的话,多节点会不会存在 accessToken 冲突
|
||||||
|
|
||||||
WxMpDefaultConfigImpl configStorage;
|
WxMpDefaultConfigImpl configStorage;
|
||||||
if (properties.isUseRedis()) {
|
configStorage = new WxMpDefaultConfigImpl();
|
||||||
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.setAppId(wxAccountDO.getAppId());
|
||||||
configStorage.setSecret(wxAccountDO.getAppSecret());
|
configStorage.setSecret(wxAccountDO.getAppSecret());
|
||||||
|
@ -157,9 +142,4 @@ public class WxMpConfig implements InitializingBean {
|
||||||
|
|
||||||
return newRouter;
|
return newRouter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() throws Exception {
|
|
||||||
initWxConfig();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
* wechat mp properties
|
* wechat mp properties
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@ConfigurationProperties(prefix = "wx.mp")
|
//@ConfigurationProperties(prefix = "wx.mp")
|
||||||
public class WxMpProperties {
|
public class WxMpProperties {
|
||||||
/**
|
/**
|
||||||
* 是否使用redis存储access token
|
* 是否使用redis存储access token
|
||||||
|
|
|
@ -34,13 +34,6 @@ import java.util.Map;
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MsgHandler implements WxMpMessageHandler {
|
public class MsgHandler implements WxMpMessageHandler {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private WxReceiveTextService wxReceiveTextService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private WxTextTemplateService wxTextTemplateService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WxAccountService wxAccountService;
|
private WxAccountService wxAccountService;
|
||||||
|
|
||||||
|
@ -50,9 +43,6 @@ public class MsgHandler implements WxMpMessageHandler {
|
||||||
@Resource
|
@Resource
|
||||||
private FileApi fileApi;
|
private FileApi fileApi;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private WxMpProperties wxMpProperties;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
|
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
|
||||||
Map<String, Object> context, WxMpService weixinService,
|
Map<String, Object> context, WxMpService weixinService,
|
||||||
|
@ -147,8 +137,7 @@ public class MsgHandler implements WxMpMessageHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
//组装默认回复消息
|
//组装默认回复消息
|
||||||
String content = wxMpProperties.getDefaultContent();//默认
|
return new TextBuilder().build("测试", wxMessage, weixinService);
|
||||||
return new TextBuilder().build(content, wxMessage, weixinService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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 文档
|
wx: # 参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md 文档
|
||||||
mp:
|
mp:
|
||||||
# 公众号配置(必填)
|
# 公众号配置(必填)
|
||||||
app-id: wx041349c6f39b268b
|
appId: wx041349c6f39b268b
|
||||||
secret: 5abee519483bc9f8cb37ce280e814bd0
|
secret: 5abee519483bc9f8cb37ce280e814bd0
|
||||||
# 存储配置,解决 AccessToken 的跨节点的共享
|
# 存储配置,解决 AccessToken 的跨节点的共享
|
||||||
config-storage:
|
config-storage:
|
||||||
type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取
|
type: RedisTemplate # 配置类型: Memory(默认), Jedis, RedisTemplate
|
||||||
key-prefix: wx # Redis Key 的前缀 TODO 芋艿:解决下 Redis key 管理的配置
|
key-prefix: wx # Redis Key 的前缀 TODO 芋艿:解决下 Redis key 管理的配置
|
||||||
http-client-type: HttpClient # 采用 HttpClient 请求微信公众号平台
|
http-client-type: HttpClient # http客户端类型: HttpClient(默认), OkHttp, JoddHttp
|
||||||
|
|
||||||
--- #################### 芋道相关配置 ####################
|
--- #################### 芋道相关配置 ####################
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue