feat: 渲染短信模板

This commit is contained in:
椰子 2021-07-16 10:36:30 +08:00
parent cb3529ce4c
commit 9570df8b14
3 changed files with 19 additions and 3 deletions

View File

@ -14,6 +14,8 @@ import com.cicdi.notify.Provider;
public interface TemplateProvider { public interface TemplateProvider {
/** /**
* 获取通知类型
*
* @return 通知类型 * @return 通知类型
* @see NotifyType * @see NotifyType
* @see DefaultNotifyType * @see DefaultNotifyType

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.cicdi.notify.*; import com.cicdi.notify.*;
import com.cicdi.notify.sms.SmsProvider; import com.cicdi.notify.sms.SmsProvider;
import com.cicdi.notify.template.TemplateManager; import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.util.ExpressionUtils;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
@ -57,7 +58,7 @@ public class TelecomSmsNotifier implements Notifier<TelecomSmsTemplate> {
try { try {
URI sendMsgURI = new URIBuilder(notifyApi).addParameter("userId", configuration.getUserId()) URI sendMsgURI = new URIBuilder(notifyApi).addParameter("userId", configuration.getUserId())
.addParameter("password", configuration.getPassword()) .addParameter("password", configuration.getPassword())
.addParameter("content", template.getContent()) .addParameter("content", render(template.getContent(), context))
.addParameter("mobile", template.getMobile()) .addParameter("mobile", template.getMobile())
.build(); .build();
HttpPost httpPost = new HttpPost(sendMsgURI); HttpPost httpPost = new HttpPost(sendMsgURI);
@ -84,6 +85,17 @@ public class TelecomSmsNotifier implements Notifier<TelecomSmsTemplate> {
} }
} }
/**
* 使用变量替和SpEL表达式渲染模板
*
* @param expression 带有变量或者表达式的模板
* @param context 用于替换的上下文
* @return 渲染后的模板
*/
private String render(String expression, Map<String, Object> context) {
return ExpressionUtils.analytical(expression, context);
}
/** /**
* 处理HTTP请求响应信息 * 处理HTTP请求响应信息
* <p>将URL参数转换成键值对</p> * <p>将URL参数转换成键值对</p>

View File

@ -44,7 +44,7 @@ public class TelecomSmsTest {
templateProperties.setProvider(SmsProvider.js139.getId()); templateProperties.setProvider(SmsProvider.js139.getId());
TelecomSmsTemplate template = new TelecomSmsTemplate(); TelecomSmsTemplate template = new TelecomSmsTemplate();
template.setContent("Hello World!"); template.setContent("${code}有效期为5分钟。");
template.setMobile("13047689449,18605120786"); template.setMobile("13047689449,18605120786");
templateProperties.setTemplate(JSON.toJSONString(template)); templateProperties.setTemplate(JSON.toJSONString(template));
@ -62,6 +62,8 @@ public class TelecomSmsTest {
templateManager.register(telecomSmsNotifierProvider); templateManager.register(telecomSmsNotifierProvider);
Notifier<Template> notifier = notifierManager.getNotifier(DefaultNotifyType.sms, "123"); Notifier<Template> notifier = notifierManager.getNotifier(DefaultNotifyType.sms, "123");
notifier.send(templateManager.getTemplate(DefaultNotifyType.sms, "124"), new HashMap<>()); Map<String, Object> context = new HashMap<>();
context.put("code", "54876812");
notifier.send(templateManager.getTemplate(DefaultNotifyType.sms, "124"), context);
} }
} }