首先说下业务场景,之前做的微信公众模板推送…改需求了,也不算改需求吧,就是公司业务扩展,就不细说了。
大致就是公众号模板有两套,需要切换,为了灵活配置,临时想了这套方案。如有好的方案请在评论区留言,大家共同探讨。
首先在原来的基础上修改(原版本请移步至我上篇文章:微信公众号模板消息推送)
由于需要灵活配置,所以模板放置apollo了,apollo配置就不贴出来了,代码修改如下:
/**
* @Author: Hou_fx
* @Date: 2019/7/18 11:25
* @Description: 微信推送模板类型
*/
public enum WeChatPushType {
/**
* 微信推送模板
*/
W001(WeChatPushTemplate.getTemplateCode("W001"), "融资申请通知", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "remark"}),
W002(WeChatPushTemplate.getTemplateCode("W002"), "贷款额度评估结果通知", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "remark"}),
W004(WeChatPushTemplate.getTemplateCode("W004"), "活动报名成功通知", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "keyword3", "keyword4", "keyword5", "remark"}),
W005(WeChatPushTemplate.getTemplateCode("W005"), "注册成功通知", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "keyword3", "remark"}),
W006(WeChatPushTemplate.getTemplateCode("W006"), "账户开通提醒", "/app/wx/push", new String[]{"first", "name", "time", "remark"}),
W007(WeChatPushTemplate.getTemplateCode("W007"), "审核失败通知", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "remark"}),
W008(WeChatPushTemplate.getTemplateCode("W008"), "客户贷款进度", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "keyword3", "keyword4", "keyword5", "remark"}),
W009(WeChatPushTemplate.getTemplateCode("W009"), "登录提醒", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "remark"}),
W010(WeChatPushTemplate.getTemplateCode("W010"), "账户操作信息提醒", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "keyword3", "remark"}),
W011(WeChatPushTemplate.getTemplateCode("W011"), "申请受理通知", "/app/wx/push", new String[]{"first", "appellationtime", "publicproductname", "apptype", "amount", "remark"}),
W012(WeChatPushTemplate.getTemplateCode("W012"), "账户绑定通知", "/app/wx/push", new String[]{"first", "name1", "time", "remark"}),
W013(WeChatPushTemplate.getTemplateCode("W013"), "业务提醒", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "remark"}),
W014(WeChatPushTemplate.getTemplateCode("W014"), "项目状态通知", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "keyword3", "remark"}),
W016(WeChatPushTemplate.getTemplateCode("W016"), "审核成功通知", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "keyword3", "keyword4", "remark"}),
W017(WeChatPushTemplate.getTemplateCode("W017"), "放款失败提醒", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "remark"}),
W018(WeChatPushTemplate.getTemplateCode("W018"), "放款成功通知", "/app/wx/push", new String[]{"first", "keyword1", "keyword2", "remark"}),
;
private String templateId;
private String desc;
private String url;
private String[] keys;
WeChatPushType(){}
WeChatPushType(String templateId, String desc, String url, String[] keys) {
this.desc = desc;
this.templateId = templateId;
this.url = url;
this.keys = keys;
}
public String getTemplateId() {
return templateId;
}
public String getDesc() {
return desc;
}
public String[] getKeys() {
return keys;
}
public String getUrl() {
return url;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public void setDesc(String desc) {
this.desc = desc;
}
public void setUrl(String url) {
this.url = url;
}
public void setKeys(String[] keys) {
this.keys = keys;
}}
主要修改的地方就是:WeChatPushTemplate.getTemplateCode(“W001”),WeChatPushTemplate.getTemplateCode代码如下:
public static String getTemplateCode(String key) {
Sms s = Optional.ofNullable(template.get(key)).orElse(new Sms());
return s.getCode();
}
由于是项目是微服务的,消费者需要注入bean,否则无法获取到templateId,细心的童鞋就发现了,WeChatPushType 枚举里使用的是WeChatPushTemplate.getTemplateCode(“W001”)是static,static就涉及到服务启动的时候,apollo的数据获取问题,大家都知道 static 直接用@Value注解是获取不到的,同样,@ApolloJsonValue也是获取不到的,这种情况下,在set方法上可以获取到,如:
但是这种虽然可以注入进去,由于项目的yml是放置apollo的,所以服务是先启动,apollo是后加载,导致还是未获取到值……针对这种情况,我采用了反射机制,如下:
@ApolloJsonValue("${weChatTemplate:{}}")
public static void setTemplate(Map<String, Sms> template) {
WeChatPushTemplate.template = template;
LOG.info(">>>>>>>>>WeChatPushTemplate init:{}", WeChatPushTemplate.template);
Class c = WeChatPushType.class;
Object[] weChatPushType = c.getEnumConstants();
for (Object o : weChatPushType) {
Class cc = o.getClass();
Method set ;
try {
set = cc.getDeclaredMethod("setTemplateId", String.class);
set.invoke(o, template.get(((WeChatPushType)o).name()).getCode());
} catch (NoSuchMethodException e) {
LOG.error("not find method");
} catch (IllegalAccessException e) {
LOG.error("param error");
} catch (InvocationTargetException e) {
LOG.error("invoke error");
}
}
}
getEnumConstants() 获取枚举中所有常量,遍历常量执行setTemplateId,枚举中的值就可以动态更新啦,并且更新apollo中的值,会同步更新枚举的值
好了,到这里就结束了。
其实不提倡修改枚举中的值, 我为什么要改这个呢……主要是因为懒,如果一个个从apollo里面获取的话,涉及改动的地方较多,不太好维护,就这样改啦。
有疑问还请评论区留言~