优化通知驱动类
This commit is contained in:
parent
7a37ac18ac
commit
6db7255ed3
|
|
@ -13,8 +13,8 @@ public class AdminConfig {
|
|||
|
||||
// 免登录验证
|
||||
public static String[] notLoginUri = new String[]{
|
||||
"system:login", // 登录接口
|
||||
"common:index:config" // 配置接口
|
||||
"system:login", // 登录接口
|
||||
"index:config" // 配置接口
|
||||
};
|
||||
|
||||
// 免权限验证
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import java.util.Map;
|
|||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("api/common/index")
|
||||
@RequestMapping("api/index")
|
||||
public class IndexController {
|
||||
|
||||
@Resource
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package com.mdd.admin.service.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.mdd.admin.service.ISettingNoticeService;
|
||||
import com.mdd.admin.vo.setting.SettingNoticeDetailVo;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,14 @@
|
|||
package com.mdd.front.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.mdd.common.core.AjaxResult;
|
||||
import com.mdd.common.entity.system.SystemLogSms;
|
||||
import com.mdd.common.exception.OperateException;
|
||||
import com.mdd.common.mapper.system.SystemLogSmsMapper;
|
||||
import com.mdd.common.plugin.notice.NoticeDriver;
|
||||
import com.mdd.common.plugin.notice.NoticeParams;
|
||||
import com.mdd.common.utils.StringUtil;
|
||||
import com.mdd.common.utils.ToolsUtil;
|
||||
import com.mdd.front.validate.SmsValidate;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
|
@ -10,8 +17,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 短信管理
|
||||
|
|
@ -20,6 +27,9 @@ import java.util.Map;
|
|||
@RequestMapping("/api/sms")
|
||||
public class SmsController {
|
||||
|
||||
@Resource
|
||||
SystemLogSmsMapper systemLogSmsMapper;
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
|
|
@ -29,12 +39,30 @@ public class SmsController {
|
|||
*/
|
||||
@PostMapping("/send")
|
||||
public AjaxResult<Object> send(@Validated @RequestBody SmsValidate smsValidate) {
|
||||
Map<String, String> config = new LinkedHashMap<>();
|
||||
config.put("scene", smsValidate.getScene());
|
||||
config.put("mobile", smsValidate.getMobile());
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("code", ToolsUtil.randomInt(4));
|
||||
(new NoticeDriver()).handle(config, params);
|
||||
Assert.notNull(smsValidate.getMobile(), "mobile参数缺失!");
|
||||
Assert.notNull(smsValidate.getScene(), "scene参数缺失!");
|
||||
|
||||
SystemLogSms systemLogSms = systemLogSmsMapper.selectOne(new QueryWrapper<SystemLogSms>()
|
||||
.eq("mobile", smsValidate.getMobile())
|
||||
.eq("scene", smsValidate.getScene())
|
||||
.in("status", Arrays.asList(0, 1))
|
||||
.orderByDesc("id")
|
||||
.last("limit 1"));
|
||||
|
||||
if (StringUtil.isNotNull(systemLogSms)) {
|
||||
if (systemLogSms.getCreateTime() >= (System.currentTimeMillis() / 1000 - 60)){
|
||||
throw new OperateException("操作频繁,请稍后再试!");
|
||||
}
|
||||
}
|
||||
|
||||
NoticeParams params = new NoticeParams()
|
||||
.setScene(smsValidate.getScene())
|
||||
.setMobile(smsValidate.getMobile())
|
||||
.setParams(new String[] {
|
||||
"code:" + ToolsUtil.randomInt(4)
|
||||
});
|
||||
|
||||
(new NoticeDriver()).handle(params);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,8 +14,7 @@ public class SmsValidate implements Serializable {
|
|||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@NotNull(message = "scene参数缺失")
|
||||
@NotEmpty(message = "场景不能为空")
|
||||
private String scene;
|
||||
private Integer scene;
|
||||
|
||||
@NotNull(message = "mobile参数缺失")
|
||||
@NotEmpty(message = "手机号不能为空")
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ spring:
|
|||
static-path-pattern: /api/static/**
|
||||
# 数据源配置
|
||||
datasource:
|
||||
url: jdbc:mysql://localhost:3306/likeadmin?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
|
||||
url: jdbc:mysql://localhost:3306/local_likeadmin?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
|
||||
type: com.zaxxer.hikari.HikariDataSource # 数据源类型
|
||||
driver-class-name: com.mysql.jdbc.Driver # MySql的驱动
|
||||
username: root # 数据库账号
|
||||
|
|
|
|||
Loading…
Reference in New Issue