短信提交 2021-03-24,调整下短信发送 API

This commit is contained in:
YunaiV 2021-03-24 00:52:40 +08:00
parent 162d604644
commit 3acdec965d
52 changed files with 219 additions and 378 deletions

View File

@ -1,29 +0,0 @@
package cn.iocoder.dashboard.framework.redis.core.util;
import cn.iocoder.dashboard.modules.system.redis.stream.sms.SmsSendMessage;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.core.RedisTemplate;
/**
* Redis 消息工具类
*
* @author 芋道源码
*/
public class RedisStreamUtils {
public static final String KEY_SMS_SEND = "stream_sms_send";
public static final String GROUP_SMS_SEND = "group_sms_send";
/**
* 发送 Redis 消息基于 Redis pub/sub 实现
*
* @param redisTemplate Redis 操作模板
* @param message 消息
*/
public static void sendChannelMessage(RedisTemplate<String, ?> redisTemplate, SmsSendMessage message) {
redisTemplate.opsForStream().add(StreamRecords.newRecord().ofObject(message).withStreamKey(KEY_SMS_SEND));
}
}

View File

@ -102,7 +102,7 @@ public class SmsClientFactory {
public String getTemplateApiIdByCode(String templateCode) {
SmsTemplateProperty smsTemplateProperty = templatePropertyMap.get(templateCode);
if (smsTemplateProperty == null) {
throw new ServiceException(SMS_TEMPLATE_NOT_FOUND);
throw new ServiceException(SMS_TEMPLATE_NOT_EXISTS);
}
return smsTemplateProperty.getApiTemplateId();
}

View File

@ -5,7 +5,7 @@ import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelPageReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.resp.SmsChannelEnumRespVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

View File

@ -1,19 +1,14 @@
package cn.iocoder.dashboard.modules.system.controller.sms;
import cn.iocoder.dashboard.framework.sms.core.SmsBody;
import cn.iocoder.dashboard.modules.system.redis.stream.sms.SmsSendStreamProducer;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import java.util.Arrays;
import java.util.Map;
/**
* 短信默认回调接口

View File

@ -7,7 +7,7 @@ import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelAllVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.resp.SmsChannelEnumRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

View File

@ -3,8 +3,8 @@ package cn.iocoder.dashboard.modules.system.convert.sms;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.sms.core.property.SmsTemplateProperty;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsTemplateVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsTemplateDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms;
package cn.iocoder.dashboard.modules.system.dal.dataobject.sms;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.TableName;

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms;
package cn.iocoder.dashboard.modules.system.dal.dataobject.sms;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms;
package cn.iocoder.dashboard.modules.system.dal.dataobject.sms;
import cn.iocoder.dashboard.modules.system.enums.sms.SmsSendStatusEnum;
import com.baomidou.mybatisplus.annotation.TableName;

View File

@ -1,11 +1,14 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms;
package cn.iocoder.dashboard.modules.system.dal.dataobject.sms;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
import java.util.List;
/**
* 短信模板
@ -44,7 +47,7 @@ public class SysSmsTemplateDO extends BaseDO {
private String bizCode;
/**
* 编码
* 模板编码
*/
private String code;
@ -66,7 +69,8 @@ public class SysSmsTemplateDO extends BaseDO {
/**
* 参数数组(自动根据内容生成)
*/
private String params;
@TableField(typeHandler = JacksonTypeHandler.class)
private List<String> params;
/**
* 备注
@ -74,7 +78,9 @@ public class SysSmsTemplateDO extends BaseDO {
private String remark;
/**
* 启用状态0正常 1停用
* 启用状态
*
* 枚举 {@link CommonStatusEnum}
*/
private Integer status;

View File

@ -1,10 +1,10 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dao.sms;
package cn.iocoder.dashboard.modules.system.dal.mysql.sms;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelPageReqVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dao.sms;
package cn.iocoder.dashboard.modules.system.dal.mysql.sms;
import cn.iocoder.dashboard.common.enums.DefaultBitFieldEnum;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsQueryLogDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsQueryLogDO;
import cn.iocoder.dashboard.modules.system.enums.sms.SmsSendStatusEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

View File

@ -1,6 +1,6 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dao.sms;
package cn.iocoder.dashboard.modules.system.dal.mysql.sms;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsSendLogDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsSendLogDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

View File

@ -1,15 +1,18 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dao.sms;
package cn.iocoder.dashboard.modules.system.dal.mysql.sms;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsTemplateDO;
import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface SysSmsTemplateMapper extends BaseMapper<SysSmsTemplateDO> {
public interface SysSmsTemplateMapper extends BaseMapperX<SysSmsTemplateDO> {
default SysSmsTemplateDO selectOneByCode(String code) {
return selectOne("code", code);
}
/**
* 根据短信渠道id查询短信模板集合

View File

@ -79,11 +79,15 @@ public interface SysErrorCodeConstants {
ErrorCode FILE_IS_EMPTY= new ErrorCode(1002009003, "文件为空");
// ========== 消息 1003001000 ==========
// ========== 短信模板 1002010000 ==========
ErrorCode SMS_TEMPLATE_NOT_EXISTS = new ErrorCode(1002010000, "短信模板不存在");
// ========== 短信发送 1002011000 ==========
ErrorCode SMS_SEND_MOBILE_NOT_EXISTS = new ErrorCode(1002011000, "手机号不存在");
ErrorCode SMS_CHANNEL_NOT_INIT = new ErrorCode(1003001001,
"短信渠道没有初始化, 请调用SmsClientWrapper#initSmsClient()或SmsClientWrapper#addSmsClient");
ErrorCode SMS_CHANNEL_NOT_FOUND = new ErrorCode(1003001002, "没有短信渠道信息, 请初始化sms_channel表数据。");
ErrorCode SMS_TEMPLATE_NOT_FOUND = new ErrorCode(1003001003, "没有短信模板信息, 请初始化sms_template表数据。");
ErrorCode SMS_SENDER_NOT_FOUND = new ErrorCode(1003001004, "没有找到对应的短信发送对象请检查sms_channel表和sms_template表数据");
ErrorCode INVALID_CHANNEL_CODE = new ErrorCode(1003001005, "非法的短信渠道code请检查sms_channel表的code值是否与SmsChannelEnum中的code值一致。");
ErrorCode PARAM_VALUE_IS_NULL = new ErrorCode(1003001006, "参数【{}】不能为空");

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.consumer.dept;
package cn.iocoder.dashboard.modules.system.mq.consumer.dept;
import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.dashboard.modules.system.redis.mq.message.dept.SysDeptRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.dept.SysDeptRefreshMessage;
import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.consumer.dict;
package cn.iocoder.dashboard.modules.system.mq.consumer.dict;
import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.dashboard.modules.system.redis.mq.message.dict.SysDictDataRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.dict.SysDictDataRefreshMessage;
import cn.iocoder.dashboard.modules.system.service.dict.SysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.consumer.permission;
package cn.iocoder.dashboard.modules.system.mq.consumer.permission;
import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.dashboard.modules.system.redis.mq.message.permission.SysMenuRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysMenuRefreshMessage;
import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.consumer.permission;
package cn.iocoder.dashboard.modules.system.mq.consumer.permission;
import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.dashboard.modules.system.redis.mq.message.permission.SysRoleMenuRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysRoleMenuRefreshMessage;
import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.consumer.permission;
package cn.iocoder.dashboard.modules.system.mq.consumer.permission;
import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.dashboard.modules.system.redis.mq.message.permission.SysRoleRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysRoleRefreshMessage;
import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.redis.stream.sms;
package cn.iocoder.dashboard.modules.system.mq.consumer.sms;
import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient;
import cn.iocoder.dashboard.framework.sms.core.SmsBody;

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.redis.mq.message.dept;
package cn.iocoder.dashboard.modules.system.mq.message.dept;
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.redis.mq.message.dict;
package cn.iocoder.dashboard.modules.system.mq.message.dict;
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.redis.mq.message.permission;
package cn.iocoder.dashboard.modules.system.mq.message.permission;
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.redis.mq.message.permission;
package cn.iocoder.dashboard.modules.system.mq.message.permission;
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.redis.mq.message.permission;
package cn.iocoder.dashboard.modules.system.mq.message.permission;
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
import lombok.Data;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.producer.dept;
package cn.iocoder.dashboard.modules.system.mq.producer.dept;
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
import cn.iocoder.dashboard.modules.system.redis.mq.message.dept.SysDeptRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.dept.SysDeptRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.producer.dict;
package cn.iocoder.dashboard.modules.system.mq.producer.dict;
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
import cn.iocoder.dashboard.modules.system.redis.mq.message.dict.SysDictDataRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.dict.SysDictDataRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.producer.permission;
package cn.iocoder.dashboard.modules.system.mq.producer.permission;
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
import cn.iocoder.dashboard.modules.system.redis.mq.message.permission.SysMenuRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysMenuRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.producer.permission;
package cn.iocoder.dashboard.modules.system.mq.producer.permission;
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
import cn.iocoder.dashboard.modules.system.redis.mq.message.permission.SysRoleMenuRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysRoleMenuRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.redis.mq.producer.permission;
package cn.iocoder.dashboard.modules.system.mq.producer.permission;
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
import cn.iocoder.dashboard.modules.system.redis.mq.message.permission.SysRoleRefreshMessage;
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysRoleRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

View File

@ -0,0 +1,42 @@
package cn.iocoder.dashboard.modules.system.mq.producer.sms;
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
import cn.iocoder.dashboard.modules.system.mq.message.sms.SysSmsSendMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
/**
* 短信发送流消息监听器
*
* @author zzf
* @date 2021/3/9 16:35
*/
@Slf4j
@Component
public class SmsSendStreamProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* 发送短信 Message
*
* @param mobile 手机号
* @param templateCode 短信模板编号
* @param templateParams 短信模板参数
* @param userId 用户编号
* @param userType 用户类型
*/
public void sendSmsSendMessage(String mobile, String templateCode, Map<String, Object> templateParams,
Integer userId, Integer userType) {
SysSmsSendMessage message = new SysSmsSendMessage();
message.setMobile(mobile).setTemplateCode(templateCode).setTemplateParams(templateParams);
message.setUserId(userId).setUserType(userType);
RedisMessageUtils.sendStreamMessage(stringRedisTemplate, message);
}
}

View File

@ -1,56 +0,0 @@
//package cn.iocoder.dashboard.modules.system.redis.mq.consumer.sms;
//
//import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
//import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient;
//import cn.iocoder.dashboard.framework.sms.core.SmsResult;
//import cn.iocoder.dashboard.modules.system.redis.mq.message.dept.SysDeptRefreshMessage;
//import cn.iocoder.dashboard.modules.system.redis.stream.sms.SmsSendMessage;
//import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService;
//import cn.iocoder.dashboard.modules.system.service.sms.SysSmsQueryLogService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.connection.stream.Consumer;
//import org.springframework.data.redis.connection.stream.ObjectRecord;
//import org.springframework.data.redis.connection.stream.ReadOffset;
//import org.springframework.data.redis.connection.stream.StreamOffset;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//
///**
// * 针对 {@link SysDeptRefreshMessage} 的消费者
// *
// * @author 芋道源码
// */
//@Component
//@Slf4j
//public class SmsSendConsumer extends AbstractChannelMessageListener<SmsSendMessage> {
//
// @Resource
// private SysSmsChannelService smsChannelService;
//
// @Resource
// private SysSmsQueryLogService smsQueryLogService;
//
// @Autowired
// StringRedisTemplate redisTemplate;
//
// @Override
// public void onMessage(SmsSendMessage message) {
//
// redisTemplate.opsForStream().add(ObjectRecord.create("String", message));
//
// redisTemplate.opsForStream().read(Consumer.from("",""), StreamOffset.create("", ReadOffset.lastConsumed()));
//
//
//
// log.info("[onMessage][收到 发送短信 消息], content: " + message.toString());
// AbstractSmsClient smsClient = smsChannelService.getSmsClient(message.getSmsBody().getTemplateCode());
// String templateApiId = smsChannelService.getSmsTemplateApiIdByCode(message.getSmsBody().getTemplateCode());
//
// SmsResult result = smsClient.send(templateApiId, message.getSmsBody(), message.getTargetPhone());
// smsQueryLogService.afterSendLog(message.getSmsBody().getSmsLogId(), result);
// }
//
//}

View File

@ -1,31 +0,0 @@
//package cn.iocoder.dashboard.modules.system.redis.mq.producer.sms;
//
//import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
//import cn.iocoder.dashboard.framework.sms.core.SmsBody;
//import cn.iocoder.dashboard.modules.system.redis.stream.sms.SmsSendMessage;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//
///**
// * 短信的 Producer
// */
//@Component
//public class SmsProducer {
//
// @Resource
// private StringRedisTemplate stringRedisTemplate;
//
// /**
// * 发送 {@link SmsSendMessage} 消息
// */
// public void sendSmsSendMessage(SmsBody smsBody, String targetPhone) {
// SmsSendMessage message = new SmsSendMessage();
// message.setSmsBody(smsBody);
// message.setTargetPhone(targetPhone);
// // TODO FROM 芋艿 TO ZZF这块等未来改哈这个方法目前是广播消费会导致每个节点都发送一次等后续封装出 redis stream 消息
// RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
// }
//
//}

View File

@ -1,93 +0,0 @@
package cn.iocoder.dashboard.modules.system.redis.stream;
import cn.iocoder.dashboard.framework.redis.core.util.RedisStreamUtils;
import cn.iocoder.dashboard.modules.system.redis.stream.sms.SmsSendMessage;
import cn.iocoder.dashboard.modules.system.redis.stream.sms.SmsSendStreamConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.stream.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.stream.StreamMessageListenerContainer;
import org.springframework.data.redis.stream.StreamMessageListenerContainer.StreamMessageListenerContainerOptions;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.ErrorHandler;
import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.Duration;
@Slf4j
@Component
public class StreamConsumerRunner implements ApplicationRunner, DisposableBean {
@Resource
RedisConnectionFactory redisConnectionFactory;
@Resource
ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Resource
SmsSendStreamConsumer streamMessageListener;
@Resource
StringRedisTemplate stringRedisTemplate;
private StreamMessageListenerContainer<String, ObjectRecord<String, SmsSendMessage>> streamMessageListenerContainer;
@Override
public void run(ApplicationArguments args) throws UnknownHostException {
StreamInfo.XInfoGroups groups = stringRedisTemplate.opsForStream().groups(RedisStreamUtils.KEY_SMS_SEND);
if (groups.isEmpty()) {
stringRedisTemplate.opsForStream().createGroup(RedisStreamUtils.KEY_SMS_SEND, RedisStreamUtils.GROUP_SMS_SEND);
}
// 创建配置对象
StreamMessageListenerContainerOptions<String, ObjectRecord<String, SmsSendMessage>> streamMessageListenerContainerOptions = StreamMessageListenerContainerOptions
.builder()
// 一次性最多拉取多少条消息
.batchSize(10)
// 执行消息轮询的执行器
.executor(this.threadPoolTaskExecutor)
// 消息消费异常的handler
.errorHandler(new ErrorHandler() {
@Override
public void handleError(Throwable t) {
// throw new RuntimeException(t);
t.printStackTrace();
}
})
// 超时时间设置为0表示不超时超时后会抛出异常
.pollTimeout(Duration.ZERO)
// 序列化器
.serializer(new StringRedisSerializer())
.targetType(SmsSendMessage.class)
.build();
// 根据配置对象创建监听容器对象
StreamMessageListenerContainer<String, ObjectRecord<String, SmsSendMessage>> streamMessageListenerContainer = StreamMessageListenerContainer
.create(this.redisConnectionFactory, streamMessageListenerContainerOptions);
// 使用监听容器对象开始监听消费使用的是手动确认方式
streamMessageListenerContainer.receive(Consumer.from(RedisStreamUtils.GROUP_SMS_SEND, InetAddress.getLocalHost().getHostName()),
StreamOffset.create(RedisStreamUtils.KEY_SMS_SEND, ReadOffset.lastConsumed()), this.streamMessageListener);
this.streamMessageListenerContainer = streamMessageListenerContainer;
// 启动监听
this.streamMessageListenerContainer.start();
}
@Override
public void destroy() throws Exception {
this.streamMessageListenerContainer.stop();
}
}

View File

@ -1,16 +0,0 @@
package cn.iocoder.dashboard.modules.system.redis.stream.sms;
import cn.iocoder.dashboard.framework.sms.core.SmsBody;
import lombok.Data;
/**
* 部门数据刷新 Message
*/
@Data
public class SmsSendMessage {
private SmsBody smsBody;
private String targetPhone;
}

View File

@ -1,35 +0,0 @@
package cn.iocoder.dashboard.modules.system.redis.stream.sms;
import cn.iocoder.dashboard.framework.redis.core.util.RedisStreamUtils;
import cn.iocoder.dashboard.framework.sms.core.SmsBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 短信发送流消息监听器
*
* @author zzf
* @date 2021/3/9 16:35
*/
@Slf4j
@Component
public class SmsSendStreamProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* 发送 {@link SmsSendMessage} 消息
*/
public void sendSmsSendMessage(SmsBody smsBody, String targetPhone) {
SmsSendMessage message = new SmsSendMessage();
message.setSmsBody(smsBody);
message.setTargetPhone(targetPhone);
RedisStreamUtils.sendChannelMessage(stringRedisTemplate, message);
}
}

View File

@ -11,7 +11,7 @@ import cn.iocoder.dashboard.modules.system.convert.dept.SysDeptConvert;
import cn.iocoder.dashboard.modules.system.dal.mysql.dept.SysDeptMapper;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO;
import cn.iocoder.dashboard.modules.system.enums.dept.DeptIdEnum;
import cn.iocoder.dashboard.modules.system.redis.mq.producer.dept.SysDeptProducer;
import cn.iocoder.dashboard.modules.system.mq.producer.dept.SysDeptProducer;
import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;

View File

@ -12,7 +12,7 @@ import cn.iocoder.dashboard.modules.system.convert.dict.SysDictDataConvert;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dict.SysDictDataDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dict.SysDictTypeDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dict.SysDictDataMapper;
import cn.iocoder.dashboard.modules.system.redis.mq.producer.dict.SysDictDataProducer;
import cn.iocoder.dashboard.modules.system.mq.producer.dict.SysDictDataProducer;
import cn.iocoder.dashboard.modules.system.service.dict.SysDictDataService;
import cn.iocoder.dashboard.modules.system.service.dict.SysDictTypeService;
import com.google.common.annotations.VisibleForTesting;

View File

@ -11,7 +11,7 @@ import cn.iocoder.dashboard.modules.system.dal.mysql.permission.SysMenuMapper;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysMenuDO;
import cn.iocoder.dashboard.modules.system.enums.permission.MenuIdEnum;
import cn.iocoder.dashboard.modules.system.enums.permission.MenuTypeEnum;
import cn.iocoder.dashboard.modules.system.redis.mq.producer.permission.SysMenuProducer;
import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysMenuProducer;
import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService;
import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
import cn.iocoder.dashboard.util.collection.CollectionUtils;

View File

@ -3,7 +3,6 @@ package cn.iocoder.dashboard.modules.system.service.permission.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.dashboard.modules.system.dal.mysql.permission.SysRoleMenuMapper;
import cn.iocoder.dashboard.modules.system.dal.mysql.permission.SysUserRoleMapper;
@ -11,7 +10,7 @@ import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysMenuDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysRoleDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysRoleMenuDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysUserRoleDO;
import cn.iocoder.dashboard.modules.system.redis.mq.producer.permission.SysPermissionProducer;
import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysPermissionProducer;
import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService;
import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService;

View File

@ -15,7 +15,7 @@ import cn.iocoder.dashboard.modules.system.dal.mysql.permission.SysRoleMapper;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysRoleDO;
import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum;
import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum;
import cn.iocoder.dashboard.modules.system.redis.mq.producer.permission.SysRoleProducer;
import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysRoleProducer;
import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService;
import com.google.common.collect.ImmutableMap;

View File

@ -2,14 +2,12 @@ package cn.iocoder.dashboard.modules.system.service.sms;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient;
import cn.iocoder.dashboard.framework.sms.core.SmsResultDetail;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelAllVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelPageReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.resp.SmsChannelEnumRespVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
import javax.servlet.ServletRequest;
import java.util.List;
/**

View File

@ -1,13 +1,8 @@
package cn.iocoder.dashboard.modules.system.service.sms;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.iocoder.dashboard.framework.sms.core.SmsBody;
import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum;
import javax.servlet.ServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* 短信Service接口
@ -18,39 +13,11 @@ import java.util.List;
*/
public interface SysSmsService {
/**
* 发送消息
*
* @param smsBody 消息内容
* @param targetPhones 发送对象手机号列表
*/
default void send(SmsBody smsBody, List<String> targetPhones) {
if (CollectionUtil.isEmpty(targetPhones)) {
return;
}
targetPhones.forEach(s -> this.send(smsBody, s));
}
void sendSingleSms(String mobile, Long userId, Integer userType,
String templateCode, Map<String, Object> templateParams);
/**
* 发送消息
*
* @param smsBody 消息内容
* @param targetPhone 发送对象手机号
*/
void send(SmsBody smsBody, String targetPhone);
/**
* 发送消息
*
* @param smsBody 消息内容
* @param targetPhones 发送对象手机号数组
*/
default void send(SmsBody smsBody, String... targetPhones) {
if (ArrayUtil.isEmpty(targetPhones)) {
return;
}
send(smsBody, Arrays.asList(targetPhones));
}
void sendBatchSms(List<String> mobiles, List<Long> userIds, Integer userType,
String templateCode, Map<String, Object> templateParams);
/**
* 处理短信发送回调函数

View File

@ -1,10 +1,21 @@
package cn.iocoder.dashboard.modules.system.service.sms;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
/**
* 短信渠道Service接口
* 短信模板 Service 接口
*
* @author zzf
* @date 2021/1/25 9:24
*/
public interface SysSmsTemplateService {
/**
* 获得短信模板
*
* @param code 模板编码
* @return 短信模板
*/
SysSmsTemplateDO getSmsTemplateByCode(String code);
}

View File

@ -8,7 +8,6 @@ import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum;
import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperty;
import cn.iocoder.dashboard.framework.sms.core.property.SmsTemplateProperty;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelAllVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsTemplateVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelPageReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.resp.SmsChannelEnumRespVO;
@ -16,10 +15,9 @@ import cn.iocoder.dashboard.modules.system.convert.sms.SmsChannelConvert;
import cn.iocoder.dashboard.modules.system.convert.sms.SmsTemplateConvert;
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.sms.SysSmsChannelMapper;
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.sms.SysSmsTemplateMapper;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsTemplateDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;

View File

@ -6,7 +6,7 @@ import cn.iocoder.dashboard.framework.sms.core.SmsResult;
import cn.iocoder.dashboard.framework.sms.core.SmsResultDetail;
import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperty;
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.sms.SysSmsQueryLogMapper;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsQueryLogDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsQueryLogDO;
import cn.iocoder.dashboard.modules.system.enums.sms.SmsSendStatusEnum;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsQueryLogService;
import org.springframework.stereotype.Service;

View File

@ -2,11 +2,10 @@ package cn.iocoder.dashboard.modules.system.service.sms.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient;
import cn.iocoder.dashboard.framework.sms.core.SmsResultDetail;
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.sms.SysSmsQueryLogMapper;
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.sms.SysSmsSendLogMapper;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsQueryLogDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsSendLogDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsQueryLogDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsSendLogDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsQueryLogMapper;
import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsSendLogMapper;
import cn.iocoder.dashboard.modules.system.enums.sms.SmsSendStatusEnum;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsSendLogService;

View File

@ -1,17 +1,29 @@
package cn.iocoder.dashboard.modules.system.service.sms.impl;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.dashboard.common.enums.UserTypeEnum;
import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient;
import cn.iocoder.dashboard.framework.sms.core.SmsBody;
import cn.iocoder.dashboard.framework.sms.core.SmsClientFactory;
import cn.iocoder.dashboard.framework.sms.core.SmsResultDetail;
import cn.iocoder.dashboard.modules.system.redis.stream.sms.SmsSendStreamProducer;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.dashboard.modules.system.mq.producer.sms.SmsSendStreamProducer;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsQueryLogService;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
import cn.iocoder.dashboard.modules.system.service.user.SysUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
/**
* 短信日志Service实现类
@ -22,9 +34,15 @@ import javax.servlet.ServletRequest;
@Service
public class SysSmsServiceImpl implements SysSmsService {
@Resource
private SysSmsTemplateService smsTemplateService;
@Resource
private SysUserService userService;
@Resource
private SysSmsChannelService channelService;
@Resource
private SysSmsQueryLogService logService;
@ -34,6 +52,54 @@ public class SysSmsServiceImpl implements SysSmsService {
@Resource
private SmsClientFactory smsClientFactory;
@Override
public void sendSingleSms(String mobile, Long userId, Integer userType,
String templateCode, Map<String, Object> templateParams) {
// 校验短信模板是否存在
SysSmsTemplateDO template = this.checkSmsTemplateExists(templateCode);
// 校验手机号码是否存在
mobile = this.checkMobile(mobile, userId, userType);
}
@Override
public void sendBatchSms(List<String> mobiles, List<Long> userIds, Integer userType,
String templateCode, Map<String, Object> templateParams) {
// 校验短信模板是否存在
SysSmsTemplateDO template = this.checkSmsTemplateExists(templateCode);
}
private SysSmsTemplateDO checkSmsTemplateExists(String templateCode) {
SysSmsTemplateDO template = smsTemplateService.getSmsTemplateByCode(templateCode);
if (template == null) {
throw exception(SMS_TEMPLATE_NOT_EXISTS);
}
return template;
}
private String checkMobile(String mobile, Long userId, Integer userType) {
mobile = getMobile(mobile, userId, userType);
if (StrUtil.isEmpty(mobile)) {
throw exception(SMS_SEND_MOBILE_NOT_EXISTS);
}
return mobile;
}
private String getMobile(String mobile, Long userId, Integer userType) {
// 如果已经有手机号则直接返回
if (StrUtil.isNotEmpty(mobile)) {
return mobile;
}
// 没有手机号则基于 userId 检索
if (userId == null || userType == null) {
return null;
}
if (Objects.equals(userType, UserTypeEnum.ADMIN.getValue())) {
SysUserDO user = userService.getUser(userId);
return user != null ? user.getMobile() : null;
}
return null;
}
@Override
public void send(SmsBody smsBody, String targetPhone) {
AbstractSmsClient client = channelService.getSmsClient(smsBody.getTemplateCode());

View File

@ -1,8 +1,12 @@
package cn.iocoder.dashboard.modules.system.service.sms.impl;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsTemplateMapper;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* 短信模板Service实现类
*
@ -11,4 +15,13 @@ import org.springframework.stereotype.Service;
*/
@Service
public class SysSmsTemplateServiceImpl implements SysSmsTemplateService {
@Resource
private SysSmsTemplateMapper smsTemplateMapper;
@Override
public SysSmsTemplateDO getSmsTemplateByCode(String code) {
return smsTemplateMapper.selectOneByCode(code);
}
}

View File

@ -8,7 +8,7 @@ import cn.iocoder.dashboard.modules.system.controller.dept.vo.dept.SysDeptUpdate
import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dept.SysDeptMapper;
import cn.iocoder.dashboard.modules.system.enums.dept.DeptIdEnum;
import cn.iocoder.dashboard.modules.system.redis.mq.producer.dept.SysDeptProducer;
import cn.iocoder.dashboard.modules.system.mq.producer.dept.SysDeptProducer;
import cn.iocoder.dashboard.modules.system.service.dept.impl.SysDeptServiceImpl;
import cn.iocoder.dashboard.util.collection.ArrayUtils;
import cn.iocoder.dashboard.util.object.ObjectUtils;

View File

@ -10,7 +10,7 @@ import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataUp
import cn.iocoder.dashboard.modules.system.dal.dataobject.dict.SysDictDataDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dict.SysDictTypeDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dict.SysDictDataMapper;
import cn.iocoder.dashboard.modules.system.redis.mq.producer.dict.SysDictDataProducer;
import cn.iocoder.dashboard.modules.system.mq.producer.dict.SysDictDataProducer;
import cn.iocoder.dashboard.modules.system.service.dict.impl.SysDictDataServiceImpl;
import cn.iocoder.dashboard.util.collection.ArrayUtils;
import cn.iocoder.dashboard.util.object.ObjectUtils;