移除消息模块

This commit is contained in:
阿沐 2024-06-25 17:12:38 +08:00
parent d5a4dc704f
commit 7e7c6df959
14 changed files with 0 additions and 483 deletions

View File

@ -1,42 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>net.maku</groupId>
<artifactId>maku-boot-module</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>maku-module-message</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>net.maku</groupId>
<artifactId>maku-framework</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>net.maku</groupId>
<artifactId>maku-boot-api</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,23 +0,0 @@
package net.maku.message.convert;
import net.maku.message.entity.SmsLogEntity;
import net.maku.message.vo.SmsLogVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 短信日志
*
* @author 阿沐 babamu@126.com
*/
@Mapper
public interface SmsLogConvert {
SmsLogConvert INSTANCE = Mappers.getMapper(SmsLogConvert.class);
SmsLogVO convert(SmsLogEntity entity);
List<SmsLogVO> convertList(List<SmsLogEntity> list);
}

View File

@ -1,30 +0,0 @@
package net.maku.message.convert;
import net.maku.message.entity.SmsPlatformEntity;
import net.maku.message.sms.config.SmsConfig;
import net.maku.message.vo.SmsPlatformVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 短信平台
*
* @author 阿沐 babamu@126.com
*/
@Mapper
public interface SmsPlatformConvert {
SmsPlatformConvert INSTANCE = Mappers.getMapper(SmsPlatformConvert.class);
SmsPlatformEntity convert(SmsPlatformVO vo);
SmsPlatformVO convert(SmsPlatformEntity entity);
List<SmsPlatformVO> convertList(List<SmsPlatformEntity> list);
SmsConfig convert2(SmsPlatformEntity entity);
List<SmsConfig> convertList2(List<SmsPlatformEntity> list);
}

View File

@ -1,15 +0,0 @@
package net.maku.message.dao;
import net.maku.framework.mybatis.dao.BaseDao;
import net.maku.message.entity.SmsLogEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* 短信日志
*
* @author 阿沐 babamu@126.com
*/
@Mapper
public interface SmsLogDao extends BaseDao<SmsLogEntity> {
}

View File

@ -1,15 +0,0 @@
package net.maku.message.dao;
import net.maku.framework.mybatis.dao.BaseDao;
import net.maku.message.entity.SmsPlatformEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* 短信平台
*
* @author 阿沐 babamu@126.com
*/
@Mapper
public interface SmsPlatformDao extends BaseDao<SmsPlatformEntity> {
}

View File

@ -1,23 +0,0 @@
package net.maku.message.query;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import net.maku.framework.common.query.Query;
/**
* 短信平台查询
*
* @author 阿沐 babamu@126.com
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Schema(description = "短信平台查询")
public class SmsPlatformQuery extends Query {
@Schema(description = "平台类型 0阿里云 1腾讯云")
private Integer platform;
@Schema(description = "短信签名")
private String signName;
}

View File

@ -1,19 +0,0 @@
package net.maku.message.service;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.BaseService;
import net.maku.message.entity.SmsLogEntity;
import net.maku.message.query.SmsLogQuery;
import net.maku.message.vo.SmsLogVO;
/**
* 短信日志
*
* @author 阿沐 babamu@126.com
* <a href="https://maku.net">MAKU</a>
*/
public interface SmsLogService extends BaseService<SmsLogEntity> {
PageResult<SmsLogVO> page(SmsLogQuery query);
}

View File

@ -1,33 +0,0 @@
package net.maku.message.service;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.BaseService;
import net.maku.message.entity.SmsPlatformEntity;
import net.maku.message.query.SmsPlatformQuery;
import net.maku.message.sms.config.SmsConfig;
import net.maku.message.vo.SmsPlatformVO;
import java.util.List;
/**
* 短信平台
*
* @author 阿沐 babamu@126.com
* <a href="https://maku.net">MAKU</a>
*/
public interface SmsPlatformService extends BaseService<SmsPlatformEntity> {
PageResult<SmsPlatformVO> page(SmsPlatformQuery query);
/**
* 启用的短信平台列表
*/
List<SmsConfig> listByEnable();
void save(SmsPlatformVO vo);
void update(SmsPlatformVO vo);
void delete(List<Long> idList);
}

View File

@ -1,42 +0,0 @@
package net.maku.message.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.message.convert.SmsLogConvert;
import net.maku.message.dao.SmsLogDao;
import net.maku.message.entity.SmsLogEntity;
import net.maku.message.query.SmsLogQuery;
import net.maku.message.service.SmsLogService;
import net.maku.message.vo.SmsLogVO;
import org.springframework.stereotype.Service;
/**
* 短信日志
*
* @author 阿沐 babamu@126.com
* <a href="https://maku.net">MAKU</a>
*/
@Service
@AllArgsConstructor
public class SmsLogServiceImpl extends BaseServiceImpl<SmsLogDao, SmsLogEntity> implements SmsLogService {
@Override
public PageResult<SmsLogVO> page(SmsLogQuery query) {
IPage<SmsLogEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
return new PageResult<>(SmsLogConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
}
private LambdaQueryWrapper<SmsLogEntity> getWrapper(SmsLogQuery query){
LambdaQueryWrapper<SmsLogEntity> wrapper = Wrappers.lambdaQuery();
wrapper.eq(query.getPlatform() != null, SmsLogEntity::getPlatform, query.getPlatform());
wrapper.like(query.getPlatformId() != null, SmsLogEntity::getPlatformId, query.getPlatformId());
wrapper.orderByDesc(SmsLogEntity::getId);
return wrapper;
}
}

View File

@ -1,91 +0,0 @@
package net.maku.message.service.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.message.cache.SmsPlatformCache;
import net.maku.message.convert.SmsPlatformConvert;
import net.maku.message.dao.SmsPlatformDao;
import net.maku.message.entity.SmsPlatformEntity;
import net.maku.message.query.SmsPlatformQuery;
import net.maku.message.service.SmsPlatformService;
import net.maku.message.sms.config.SmsConfig;
import net.maku.message.vo.SmsPlatformVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* 短信平台
*
* @author 阿沐 babamu@126.com
* <a href="https://maku.net">MAKU</a>
*/
@Service
@AllArgsConstructor
public class SmsPlatformServiceImpl extends BaseServiceImpl<SmsPlatformDao, SmsPlatformEntity> implements SmsPlatformService {
private final SmsPlatformCache smsPlatformCache;
@Override
public PageResult<SmsPlatformVO> page(SmsPlatformQuery query) {
IPage<SmsPlatformEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
return new PageResult<>(SmsPlatformConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
}
private LambdaQueryWrapper<SmsPlatformEntity> getWrapper(SmsPlatformQuery query){
LambdaQueryWrapper<SmsPlatformEntity> wrapper = Wrappers.lambdaQuery();
wrapper.eq(query.getPlatform() != null, SmsPlatformEntity::getPlatform, query.getPlatform());
wrapper.like(StrUtil.isNotBlank(query.getSignName()), SmsPlatformEntity::getSignName, query.getSignName());
return wrapper;
}
@Override
public List<SmsConfig> listByEnable() {
// 从缓存读取
List<SmsConfig> cacheList = smsPlatformCache.list();
// 如果缓存没有则从DB读取然后保存到缓存里
if(cacheList == null) {
List<SmsPlatformEntity> list = this.list(new LambdaQueryWrapper<SmsPlatformEntity>().in(SmsPlatformEntity::getStatus, Constant.ENABLE));
cacheList = SmsPlatformConvert.INSTANCE.convertList2(list);
smsPlatformCache.save(cacheList);
}
return cacheList;
}
@Override
public void save(SmsPlatformVO vo) {
SmsPlatformEntity entity = SmsPlatformConvert.INSTANCE.convert(vo);
baseMapper.insert(entity);
smsPlatformCache.delete();
}
@Override
public void update(SmsPlatformVO vo) {
SmsPlatformEntity entity = SmsPlatformConvert.INSTANCE.convert(vo);
updateById(entity);
smsPlatformCache.delete();
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(List<Long> idList) {
removeByIds(idList);
smsPlatformCache.delete();
}
}

View File

@ -1,108 +0,0 @@
package net.maku.message.sms.service;
import cn.hutool.core.map.MapUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.utils.ExceptionUtils;
import net.maku.framework.common.utils.JsonUtils;
import net.maku.message.entity.SmsLogEntity;
import net.maku.message.service.SmsLogService;
import net.maku.message.service.SmsPlatformService;
import net.maku.message.cache.SmsPlatformCache;
import net.maku.message.sms.SmsContext;
import net.maku.message.sms.config.SmsConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* 短信服务
*
* @author 阿沐 babamu@126.com
* <a href="https://maku.net">MAKU</a>
*/
@Slf4j
@Service
@AllArgsConstructor
public class SmsService {
private final SmsPlatformService smsPlatformService;
private final SmsLogService smsLogService;
private final SmsPlatformCache smsCacheService;
/**
* 发送短信
* @param mobile 手机号
* @return 是否发送成功
*/
public boolean send(String mobile){
return this.send(mobile, MapUtil.newHashMap());
}
/**
* 发送短信
* @param mobile 手机号
* @param params 参数
* @return 是否发送成功
*/
public boolean send(String mobile, Map<String, String> params){
SmsConfig config = roundSmsConfig();;
try {
// 发送短信
new SmsContext(config).send(mobile, params);
saveLog(config, mobile, params, null);
return true;
}catch (Exception e) {
log.error("短信发送失败,手机号:{}", mobile, e);
saveLog(config, mobile, params, e);
return false;
}
}
/**
* 保存短信日志
*/
public void saveLog(SmsConfig config, String mobile, Map<String, String> params, Exception e) {
SmsLogEntity logEntity = new SmsLogEntity();
logEntity.setPlatform(config.getPlatform());
logEntity.setPlatformId(config.getId());
logEntity.setMobile(mobile);
logEntity.setParams(JsonUtils.toJsonString(params));
if(e != null) {
String error = StringUtils.substring(ExceptionUtils.getExceptionMessage(e), 0, 2000);
logEntity.setStatus(Constant.FAIL);
logEntity.setError(error);
}else {
logEntity.setStatus(Constant.SUCCESS);
}
smsLogService.save(logEntity);
}
/**
* 通过轮询算法获取短信平台的配置
*/
private SmsConfig roundSmsConfig() {
List<SmsConfig> platformList = smsPlatformService.listByEnable();
// 是否有可用的短信平台
int count = platformList.size();
if(count == 0) {
throw new ServerException("没有可用的短信平台,请先添加");
}
// 采用轮询算法发送短信
long round = smsCacheService.getRoundValue();
return platformList.get((int)round % count);
}
}

View File

@ -1,30 +0,0 @@
package net.maku.message.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
/**
* 短信发送
*
* @author 阿沐 babamu@126.com
*/
@Data
@Schema(description = "短信发送")
public class SmsSendVO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "手机号")
private String mobile;
@Schema(description = "参数Key")
private String paramKey;
@Schema(description = "参数Value")
private String paramValue;
}

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.maku.message.dao.SmsLogDao">
</mapper>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.maku.message.dao.SmsPlatformDao">
</mapper>