更新自述文件

This commit is contained in:
椰子 2023-01-30 11:58:06 +08:00
parent e0162e100e
commit 7025b92d2c

66
README.md Normal file
View File

@ -0,0 +1,66 @@
# 消息通知组件
## 如何使用
`com.simaek.notify.NotifyConfigManager`
实现getNotifyConfig(NotifyType notifyType, String id),从数据库或者配置文件读取通知器配置
`com.simaek.notify.template.AbstractTemplateManager`
实现方法getProperties(NotifyType notifyType, String id),从数据库或者配置文件读取模板配置
`com.simaek.notify.AbstractNotifierManager`
默认实现所有的方法,直接继承,有需要再覆写。
## 消息发送
需要实现或者覆写的功能全都完成了,直接使用即可,有两种发送方式。
1、使用预配置的模板发送信息适用于定时提醒、验证码等较为常用的信息
需要提供两个参数notifierIdtemplateId。
```
// 通过配置信息的ID和通知类型获取通知器
Notifier<Template> notifier = notifierManager.getNotifier(DefaultNotifyType.email, notifierId);
// 通过模板管理器获取模板
Template template = templateManager.getTemplate(DefaultNotifyType.email, templateId);
// 指定使用的消息模板和上下文,上下文用于对模板中的形如${name}的变量进行提换
notifier.send(template, new HashMap<>());
```
2、使用自定义模板发送信息适用于自定义消息以短信为例。
需要提供一个参数notifierId。
```
// 创建短信模板
TelecomSmsTemplate smsTemplate = new TelecomSmsTemplate();
smsTemplate.setMobile("18605120786,13047986669");
smsTemplate.setContent("你好,您的验证码是:${code}验证码有效期5分钟。");
// 创建模板配置文件
TemplateProperties templateProperties = new TemplateProperties();
// 定义模板类型为短信
templateProperties.setType(DefaultNotifyType.sms.getId());
// 选择短信服务提供商
templateProperties.setProvider(SmsProvider.js139.getId());
// 配置文件为了兼容不同模板是使用JSON字符串进行存储的需要转换一下
templateProperties.setTemplate(JSON.toJSONString(smsTemplate));
// 使用通知管理器创建模板,这一步会对模板进行渲染
Template template = templateManager.createTemplate(DefaultNotifyType.sms, templateProperties);
Map<String, Object> context = new HashMap<>();
context.put("code", "123456");
// 获取短信通知器并发送信息
Notifier<Template> notifier = notifierManager.getNotifier(DefaultNotifyType.sms, notifierId);
notifier.send(template, context);
```
## 其他
目前对接的通知服务有钉钉、微信公众号、企业微信、阿里云短信、云信通短信、电子邮件。如果想实现自己的通知器实现core模块中的接口即可。