mirror of
https://gitee.com/likeadmin/likeadmin_java.git
synced 2026-06-25 14:37:40 +08:00
优化通知驱动类
This commit is contained in:
@@ -5,50 +5,35 @@ import com.mdd.common.entity.notice.NoticeSetting;
|
||||
import com.mdd.common.exception.OperateException;
|
||||
import com.mdd.common.mapper.notice.NoticeSettingMapper;
|
||||
import com.mdd.common.plugin.notice.engine.SmsNotice;
|
||||
import com.mdd.common.plugin.notice.template.SmsTemplate;
|
||||
import com.mdd.common.utils.SpringUtil;
|
||||
import com.mdd.common.utils.StringUtil;
|
||||
import com.mdd.common.utils.ToolsUtil;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 通知驱动
|
||||
*/
|
||||
public class NoticeDriver {
|
||||
|
||||
public void handle(Map<String, String> config, Map<String, String> params) {
|
||||
// 获取通知场景
|
||||
if (StringUtil.isNull(config.get("scene"))) {
|
||||
throw new OperateException("scene参数缺失!");
|
||||
}
|
||||
|
||||
public void handle(NoticeParams noticeParams) {
|
||||
// 获取场景模板
|
||||
NoticeSettingMapper noticeSettingMapper = SpringUtil.getBean(NoticeSettingMapper.class);
|
||||
NoticeSetting noticeSetting = noticeSettingMapper.selectOne(
|
||||
new QueryWrapper<NoticeSetting>()
|
||||
.eq("scene", Integer.parseInt(config.get("scene")))
|
||||
.eq("is_delete", 0)
|
||||
.last("limit 1"));
|
||||
.eq("scene", noticeParams.getScene())
|
||||
.eq("is_delete", 0)
|
||||
.last("limit 1"));
|
||||
|
||||
if (StringUtil.isNull(noticeSetting)) {
|
||||
throw new OperateException("消息场景不存在!");
|
||||
}
|
||||
|
||||
// 短信通知
|
||||
Map<String, String> smsTemplate = ToolsUtil.jsonToMap(noticeSetting.getSmsNotice());
|
||||
if (StringUtil.isNotEmpty(smsTemplate.get("status")) && Integer.parseInt(smsTemplate.get("status")) == 1) {
|
||||
(new SmsNotice()).send(config, params, smsTemplate, noticeSetting);
|
||||
SmsTemplate smsTemplate = new SmsTemplate();
|
||||
smsTemplate.setType(noticeSetting.getType());
|
||||
smsTemplate.setParams(noticeSetting.getSmsNotice());
|
||||
if (StringUtil.isNotNull(smsTemplate.getStatus()) && smsTemplate.getStatus().equals(1)) {
|
||||
(new SmsNotice()).send(noticeParams, smsTemplate);
|
||||
}
|
||||
|
||||
// 小程序订阅通知 todo
|
||||
// Map<String, String> mnpTemplate = ToolsUtil.jsonToMap(noticeSetting.getMnpNotice());
|
||||
// if (StringUtil.isNotEmpty(mnpTemplate.get("status")) && Integer.parseInt(mnpTemplate.get("status")) == 1) {
|
||||
// (new MpNotice()).send(config, params, mnpTemplate);
|
||||
// }
|
||||
|
||||
// 公众号订阅通知 todo
|
||||
// Map<String, String> oaTemplate = ToolsUtil.jsonToMap(noticeSetting.getOaNotice());
|
||||
// if (StringUtil.isNotEmpty(oaTemplate.get("status")) && Integer.parseInt(oaTemplate.get("status")) == 1) {
|
||||
// (new OaNotice()).send(config, params, oaTemplate);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.mdd.common.plugin.notice;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class NoticeParams {
|
||||
|
||||
private Integer scene;
|
||||
private String mobile;
|
||||
private String[] params;
|
||||
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.mdd.common.plugin.notice.engine;
|
||||
|
||||
import com.mdd.common.config.GlobalConfig;
|
||||
import com.mdd.common.entity.notice.NoticeSetting;
|
||||
import com.mdd.common.plugin.notice.NoticeParams;
|
||||
import com.mdd.common.plugin.notice.template.SmsTemplate;
|
||||
import com.mdd.common.plugin.sms.SmsDriver;
|
||||
import com.mdd.common.utils.ConfigUtil;
|
||||
import com.mdd.common.utils.RedisUtil;
|
||||
@@ -9,31 +10,43 @@ import com.mdd.common.utils.StringUtil;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 短信通知
|
||||
*/
|
||||
public class SmsNotice {
|
||||
|
||||
/**
|
||||
* 发送短信通知
|
||||
*
|
||||
* @author fzr
|
||||
* @param config 基础配置
|
||||
* @param params 短信参数
|
||||
* @param smsTemplate 短信模板
|
||||
* @param noticeSetting 通知设置
|
||||
* @param noticeParams 基础配置
|
||||
* @param smsTemplate 短信模板
|
||||
*/
|
||||
public void send(Map<String, String> config, Map<String, String> params, Map<String, String> smsTemplate, NoticeSetting noticeSetting) {
|
||||
String mobile = config.getOrDefault("mobile", "");
|
||||
String scene = config.getOrDefault("scene", "");
|
||||
public void send(NoticeParams noticeParams, SmsTemplate smsTemplate) {
|
||||
String mobile = noticeParams.getMobile();
|
||||
Integer scene = noticeParams.getScene();
|
||||
|
||||
if (StringUtil.isNotEmpty(mobile) && StringUtil.isNotEmpty(scene)) {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
if (StringUtil.isNotNull(noticeParams.getParams())) {
|
||||
for (String s : noticeParams.getParams()) {
|
||||
String[] arr = s.split(":");
|
||||
String key = arr[0].trim();
|
||||
String val = arr[1].trim();
|
||||
params.put(key, val);
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtil.isNotEmpty(mobile)) {
|
||||
(new SmsDriver())
|
||||
.setScene(scene)
|
||||
.setMobile(mobile)
|
||||
.setTemplateCode(smsTemplate.getOrDefault("templateId", ""))
|
||||
.setTemplateParam(this.getSmsParams(params, smsTemplate))
|
||||
.setSmsContent(this.getContent(params, smsTemplate))
|
||||
.setTemplateCode(smsTemplate.getTemplateId())
|
||||
.setTemplateParam(this.getSmsParams(params, smsTemplate.getContent()))
|
||||
.setSmsContent(this.getContent(params, smsTemplate.getContent()))
|
||||
.sendSms();
|
||||
|
||||
// 1=业务通知, 2=验证码
|
||||
if (noticeSetting.getType() == 2 && StringUtil.isNotNull(params.get("code"))) {
|
||||
// 通知类型: [1=业务, 2=验证码]
|
||||
if (smsTemplate.getType().equals(2) && StringUtil.isNotNull(params.get("code"))) {
|
||||
String code = params.get("code").toLowerCase();
|
||||
RedisUtil.set(GlobalConfig.redisSmsCode+scene+":"+mobile, code);
|
||||
}
|
||||
@@ -45,11 +58,10 @@ public class SmsNotice {
|
||||
*
|
||||
* @author fzr
|
||||
* @param params 短信参数
|
||||
* @param smsTemplate 短信模板
|
||||
* @param content 短信模板
|
||||
* @return String 短信内容
|
||||
*/
|
||||
private String getContent(Map<String, String> params, Map<String, String> smsTemplate) {
|
||||
String content = smsTemplate.getOrDefault("content", "");
|
||||
private String getContent(Map<String, String> params, String content) {
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String searchReplace = "\\$\\{" + entry.getKey() + "}";
|
||||
content = content.replaceAll(searchReplace, entry.getValue());
|
||||
@@ -62,9 +74,11 @@ public class SmsNotice {
|
||||
* 腾讯云参数处理
|
||||
*
|
||||
* @author fzr
|
||||
* @param params 短信参数
|
||||
* @param content 短信内容
|
||||
* @return Map<String, String>
|
||||
*/
|
||||
private Map<String, String> getSmsParams(Map<String, String> params, Map<String, String> smsTemplate) {
|
||||
private Map<String, String> getSmsParams(Map<String, String> params, String content) {
|
||||
String engine = ConfigUtil.get("sms", "default", "");
|
||||
if (!engine.equals("tencent")) {
|
||||
return params;
|
||||
@@ -72,7 +86,6 @@ public class SmsNotice {
|
||||
|
||||
// 获取内容变量
|
||||
List<String> arr = new LinkedList<>();
|
||||
String content = smsTemplate.getOrDefault("content", "");
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String search = "\\$\\{" + entry.getKey() + "}";
|
||||
if (content.indexOf(search) != 1 && !arr.contains(entry.getKey())) {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.mdd.common.plugin.notice.template;
|
||||
|
||||
import com.mdd.common.utils.ToolsUtil;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class SmsTemplate {
|
||||
|
||||
/** 通知类型: 1=业务,2=验证码 */
|
||||
private Integer type;
|
||||
|
||||
/** 短信模板内容 */
|
||||
private String templateId;
|
||||
|
||||
/** 短信模板内容 */
|
||||
private String content;
|
||||
|
||||
/** 短信模板状态 */
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 设置参数
|
||||
*/
|
||||
public void setParams(String smsNotice) {
|
||||
Map<String, String> config = ToolsUtil.jsonToMap(smsNotice);
|
||||
this.setTemplateId(config.getOrDefault("templateId", ""));
|
||||
this.setContent(config.getOrDefault("content", ""));
|
||||
this.setStatus(Integer.parseInt(config.getOrDefault("status", "0")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,6 +15,7 @@ public class SmsDriver {
|
||||
|
||||
private final SystemLogSmsMapper systemLogSmsMapper;
|
||||
|
||||
private Integer scene; // 场景编码
|
||||
private String mobile; // 手机号码
|
||||
private String templateCode; // 短信模板
|
||||
private String smsContent = ""; // 短信内容
|
||||
@@ -79,6 +80,18 @@ public class SmsDriver {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置场景编码
|
||||
*
|
||||
* @author fzr
|
||||
* @param scene 场景编码
|
||||
* @return SmsDriver
|
||||
*/
|
||||
public SmsDriver setScene(Integer scene) {
|
||||
this.scene = scene;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
@@ -143,6 +156,7 @@ public class SmsDriver {
|
||||
private void updateSmsLog(Integer id, Integer status, String result) {
|
||||
SystemLogSms model = new SystemLogSms();
|
||||
model.setId(id);
|
||||
model.setScene(String.valueOf(this.scene));
|
||||
model.setMobile(this.mobile);
|
||||
model.setStatus(status);
|
||||
model.setResults(result);
|
||||
|
||||
Reference in New Issue
Block a user