From 77f6d157a5dae32940a4c336295d166fbabea4a3 Mon Sep 17 00:00:00 2001 From: xiao-fajia <1665375861@qq.com> Date: Fri, 23 Aug 2024 19:56:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=91=E6=8F=B4APP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/CommonErrorCodeConstants.java | 1 + .../yudao/module/constant/CacheConstants.java | 44 ++++ .../yudao/module/constant/UserConstants.java | 78 +++++++ .../controller/admin/SysConfigController.java | 128 +++++++++++ .../yudao/module/rescue/domain/SysConfig.java | 36 +++ .../rescue/mapper/RescueSysConfigMapper.java | 78 +++++++ .../rescue/service/ISysConfigService.java | 90 ++++++++ .../service/impl/SysConfigServiceImpl.java | 212 ++++++++++++++++++ .../mapper/rescue/RescueSysConfigMapper.xml | 104 +++++++++ 9 files changed, 771 insertions(+) create mode 100644 dl-module-rescue/src/main/java/cn/iocoder/yudao/module/constant/CacheConstants.java create mode 100644 dl-module-rescue/src/main/java/cn/iocoder/yudao/module/constant/UserConstants.java create mode 100644 dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/controller/admin/SysConfigController.java create mode 100644 dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/domain/SysConfig.java create mode 100644 dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/mapper/RescueSysConfigMapper.java create mode 100644 dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/service/ISysConfigService.java create mode 100644 dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/service/impl/SysConfigServiceImpl.java create mode 100644 dl-module-rescue/src/main/resources/mapper/rescue/RescueSysConfigMapper.xml diff --git a/dl-module-base/src/main/java/cn/iocoder/yudao/common/CommonErrorCodeConstants.java b/dl-module-base/src/main/java/cn/iocoder/yudao/common/CommonErrorCodeConstants.java index a1712095..cbafc1a1 100644 --- a/dl-module-base/src/main/java/cn/iocoder/yudao/common/CommonErrorCodeConstants.java +++ b/dl-module-base/src/main/java/cn/iocoder/yudao/common/CommonErrorCodeConstants.java @@ -11,4 +11,5 @@ public interface CommonErrorCodeConstants extends ErrorCodeConstants { ErrorCode STAFF_NOT_CHANGE = new ErrorCode(2_002_000_003, "该员工还有工作未交接,不可删除"); ErrorCode LOGIN_ACCOUNT_EXIST = new ErrorCode(2_002_000_004, "登录账号已存在"); ErrorCode LOGIN_ACCOUNT_NOT_EXIST = new ErrorCode(2_002_000_005, "登录账号不存在"); + ErrorCode SYSTEM_ERROR = new ErrorCode(2_002_000_005, "系统错误"); } diff --git a/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/constant/CacheConstants.java b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/constant/CacheConstants.java new file mode 100644 index 00000000..42789c7e --- /dev/null +++ b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/constant/CacheConstants.java @@ -0,0 +1,44 @@ +package cn.iocoder.yudao.module.constant; + +/** + * 缓存的key 常量 + * + * @author ruoyi + */ +public class CacheConstants +{ + /** + * 登录用户 redis key + */ + public static final String LOGIN_TOKEN_KEY = "login_tokens:"; + + /** + * 验证码 redis key + */ + public static final String CAPTCHA_CODE_KEY = "captcha_codes:"; + + /** + * 参数管理 cache key + */ + public static final String SYS_CONFIG_KEY = "sys_config:"; + + /** + * 字典管理 cache key + */ + public static final String SYS_DICT_KEY = "sys_dict:"; + + /** + * 防重提交 redis key + */ + public static final String REPEAT_SUBMIT_KEY = "repeat_submit:"; + + /** + * 限流 redis key + */ + public static final String RATE_LIMIT_KEY = "rate_limit:"; + + /** + * 登录账户密码错误次数 redis key + */ + public static final String PWD_ERR_CNT_KEY = "pwd_err_cnt:"; +} diff --git a/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/constant/UserConstants.java b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/constant/UserConstants.java new file mode 100644 index 00000000..4adefaa3 --- /dev/null +++ b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/constant/UserConstants.java @@ -0,0 +1,78 @@ +package cn.iocoder.yudao.module.constant; + +/** + * 用户常量信息 + * + * @author ruoyi + */ +public class UserConstants +{ + /** + * 平台内系统用户的唯一标志 + */ + public static final String SYS_USER = "SYS_USER"; + + /** 正常状态 */ + public static final String NORMAL = "0"; + + /** 异常状态 */ + public static final String EXCEPTION = "1"; + + /** 用户封禁状态 */ + public static final String USER_DISABLE = "1"; + + /** 角色封禁状态 */ + public static final String ROLE_DISABLE = "1"; + + /** 部门正常状态 */ + public static final String DEPT_NORMAL = "0"; + + /** 部门停用状态 */ + public static final String DEPT_DISABLE = "1"; + + /** 字典正常状态 */ + public static final String DICT_NORMAL = "0"; + + /** 是否为系统默认(是) */ + public static final String YES = "Y"; + + /** 是否菜单外链(是) */ + public static final String YES_FRAME = "0"; + + /** 是否菜单外链(否) */ + public static final String NO_FRAME = "1"; + + /** 菜单类型(目录) */ + public static final String TYPE_DIR = "M"; + + /** 菜单类型(菜单) */ + public static final String TYPE_MENU = "C"; + + /** 菜单类型(按钮) */ + public static final String TYPE_BUTTON = "F"; + + /** Layout组件标识 */ + public final static String LAYOUT = "Layout"; + + /** ParentView组件标识 */ + public final static String PARENT_VIEW = "ParentView"; + + /** InnerLink组件标识 */ + public final static String INNER_LINK = "InnerLink"; + + /** 校验是否唯一的返回标识 */ + public final static boolean UNIQUE = true; + public final static boolean NOT_UNIQUE = false; + + /** + * 用户名长度限制 + */ + public static final int USERNAME_MIN_LENGTH = 2; + public static final int USERNAME_MAX_LENGTH = 20; + + /** + * 密码长度限制 + */ + public static final int PASSWORD_MIN_LENGTH = 5; + public static final int PASSWORD_MAX_LENGTH = 20; +} diff --git a/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/controller/admin/SysConfigController.java b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/controller/admin/SysConfigController.java new file mode 100644 index 00000000..45056e53 --- /dev/null +++ b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/controller/admin/SysConfigController.java @@ -0,0 +1,128 @@ +package cn.iocoder.yudao.module.rescue.controller.admin; + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; +import cn.iocoder.yudao.module.rescue.core.controller.BaseController; +import cn.iocoder.yudao.module.rescue.core.page.TableDataInfo; +import cn.iocoder.yudao.module.rescue.domain.SysConfig; +import cn.iocoder.yudao.module.rescue.service.ISysConfigService; +import cn.iocoder.yudao.module.rescue.utils.ExcelUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +/** + * 参数配置 信息操作处理 + * + * @author ruoyi + */ +@RestController +@RequestMapping("/system/config") +public class SysConfigController extends BaseController +{ + @Autowired + @Lazy + private ISysConfigService configService; + + /** + * 获取参数配置列表 + */ + @PreAuthorize("@ss.hasPermi('system:config:list')") + @GetMapping("/list") + public TableDataInfo list(SysConfig config) + { + startPage(); + List list = configService.selectConfigList(config); + return getDataTable(list); + } + +// @Log(title = "参数管理", businessType = BusinessType.EXPORT) + @PreAuthorize("@ss.hasPermi('system:config:export')") + @PostMapping("/export") + public void export(HttpServletResponse response, SysConfig config) + { + List list = configService.selectConfigList(config); + ExcelUtil util = new ExcelUtil(SysConfig.class); + util.exportExcel(response, list, "参数数据"); + } + + /** + * 根据参数编号获取详细信息 + */ + @PreAuthorize("@ss.hasPermi('system:config:query')") + @GetMapping(value = "/{configId}") + public CommonResult getInfo(@PathVariable Long configId) + { + return success(configService.selectConfigById(configId)); + } + + /** + * 根据参数键名查询参数值 + */ + @GetMapping(value = "/configKey/{configKey}") + public CommonResult getConfigKey(@PathVariable String configKey) + { + return success(configService.selectConfigByKey(configKey)); + } + + /** + * 新增参数配置 + */ + @PreAuthorize("@ss.hasPermi('system:config:add')") +// @Log(title = "参数管理", businessType = BusinessType.INSERT) + @PostMapping + public CommonResult add(@Validated @RequestBody SysConfig config) + { + if (!configService.checkConfigKeyUnique(config)) + { + return error("新增参数'" + config.getConfigName() + "'失败,参数键名已存在"); + } + config.setCreator(SecurityFrameworkUtils.getLoginUserNickname()); + return toAjax(configService.insertConfig(config)); + } + + /** + * 修改参数配置 + */ + @PreAuthorize("@ss.hasPermi('system:config:edit')") +// @Log(title = "参数管理", businessType = BusinessType.UPDATE) + @PutMapping + public CommonResult edit(@Validated @RequestBody SysConfig config) + { + if (!configService.checkConfigKeyUnique(config)) + { + return error("修改参数'" + config.getConfigName() + "'失败,参数键名已存在"); + } + config.setUpdater(SecurityFrameworkUtils.getLoginUserNickname()); + return toAjax(configService.updateConfig(config)); + } + + /** + * 删除参数配置 + */ + @PreAuthorize("@ss.hasPermi('system:config:remove')") +// @Log(title = "参数管理", businessType = BusinessType.DELETE) + @DeleteMapping("/{configIds}") + public CommonResult remove(@PathVariable Long[] configIds) + { + configService.deleteConfigByIds(configIds); + return success(); + } + + /** + * 刷新参数缓存 + */ + @PreAuthorize("@ss.hasPermi('system:config:remove')") +// @Log(title = "参数管理", businessType = BusinessType.CLEAN) + @DeleteMapping("/refreshCache") + public CommonResult refreshCache() + { + configService.resetConfigCache(); + return success(); + } +} diff --git a/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/domain/SysConfig.java b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/domain/SysConfig.java new file mode 100644 index 00000000..2174b137 --- /dev/null +++ b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/domain/SysConfig.java @@ -0,0 +1,36 @@ +package cn.iocoder.yudao.module.rescue.domain; + +import cn.iocoder.yudao.annotation.Excel; +import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; +import lombok.Data; + +/** + * 参数配置表 sys_config + * + * @author ruoyi + */ +@Data +public class SysConfig extends TenantBaseDO +{ + private static final long serialVersionUID = 1L; + + /** 参数主键 */ + @Excel(name = "参数主键", cellType = Excel.ColumnType.NUMERIC) + private Long configId; + + /** 参数名称 */ + @Excel(name = "参数名称") + private String configName; + + /** 参数键名 */ + @Excel(name = "参数键名") + private String configKey; + + /** 参数键值 */ + @Excel(name = "参数键值") + private String configValue; + + /** 系统内置(Y是 N否) */ + @Excel(name = "系统内置", readConverterExp = "Y=是,N=否") + private String configType; +} diff --git a/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/mapper/RescueSysConfigMapper.java b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/mapper/RescueSysConfigMapper.java new file mode 100644 index 00000000..a1e04dae --- /dev/null +++ b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/mapper/RescueSysConfigMapper.java @@ -0,0 +1,78 @@ +package cn.iocoder.yudao.module.rescue.mapper; + +import cn.iocoder.yudao.module.rescue.domain.SysConfig; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * 参数配置 数据层 + * + * @author ruoyi + */ +@Mapper +public interface RescueSysConfigMapper { + /** + * 查询参数配置信息 + * + * @param config 参数配置信息 + * @return 参数配置信息 + */ + public SysConfig selectConfig(SysConfig config); + + /** + * 通过ID查询配置 + * + * @param configId 参数ID + * @return 参数配置信息 + */ + public SysConfig selectConfigById(Long configId); + + /** + * 查询参数配置列表 + * + * @param config 参数配置信息 + * @return 参数配置集合 + */ + public List selectConfigList(SysConfig config); + + /** + * 根据键名查询参数配置信息 + * + * @param configKey 参数键名 + * @return 参数配置信息 + */ + public SysConfig checkConfigKeyUnique(String configKey); + + /** + * 新增参数配置 + * + * @param config 参数配置信息 + * @return 结果 + */ + public int insertConfig(SysConfig config); + + /** + * 修改参数配置 + * + * @param config 参数配置信息 + * @return 结果 + */ + public int updateConfig(SysConfig config); + + /** + * 删除参数配置 + * + * @param configId 参数ID + * @return 结果 + */ + public int deleteConfigById(Long configId); + + /** + * 批量删除参数信息 + * + * @param configIds 需要删除的参数ID + * @return 结果 + */ + public int deleteConfigByIds(Long[] configIds); +} diff --git a/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/service/ISysConfigService.java b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/service/ISysConfigService.java new file mode 100644 index 00000000..d189cc88 --- /dev/null +++ b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/service/ISysConfigService.java @@ -0,0 +1,90 @@ +package cn.iocoder.yudao.module.rescue.service; + +import cn.iocoder.yudao.module.rescue.domain.SysConfig; + +import java.util.List; + +/** + * 参数配置 服务层 + * + * @author ruoyi + */ +public interface ISysConfigService +{ + /** + * 查询参数配置信息 + * + * @param configId 参数配置ID + * @return 参数配置信息 + */ + public SysConfig selectConfigById(Long configId); + + /** + * 根据键名查询参数配置信息 + * + * @param configKey 参数键名 + * @return 参数键值 + */ + public String selectConfigByKey(String configKey); + + /** + * 获取验证码开关 + * + * @return true开启,false关闭 + */ + public boolean selectCaptchaEnabled(); + + /** + * 查询参数配置列表 + * + * @param config 参数配置信息 + * @return 参数配置集合 + */ + public List selectConfigList(SysConfig config); + + /** + * 新增参数配置 + * + * @param config 参数配置信息 + * @return 结果 + */ + public int insertConfig(SysConfig config); + + /** + * 修改参数配置 + * + * @param config 参数配置信息 + * @return 结果 + */ + public int updateConfig(SysConfig config); + + /** + * 批量删除参数信息 + * + * @param configIds 需要删除的参数ID + */ + public void deleteConfigByIds(Long[] configIds); + + /** + * 加载参数缓存数据 + */ + public void loadingConfigCache(); + + /** + * 清空参数缓存数据 + */ + public void clearConfigCache(); + + /** + * 重置参数缓存数据 + */ + public void resetConfigCache(); + + /** + * 校验参数键名是否唯一 + * + * @param config 参数信息 + * @return 结果 + */ + public boolean checkConfigKeyUnique(SysConfig config); +} diff --git a/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/service/impl/SysConfigServiceImpl.java b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/service/impl/SysConfigServiceImpl.java new file mode 100644 index 00000000..33950773 --- /dev/null +++ b/dl-module-rescue/src/main/java/cn/iocoder/yudao/module/rescue/service/impl/SysConfigServiceImpl.java @@ -0,0 +1,212 @@ +package cn.iocoder.yudao.module.rescue.service.impl; + +import cn.iocoder.yudao.common.CommonErrorCodeConstants; +import cn.iocoder.yudao.module.constant.CacheConstants; +import cn.iocoder.yudao.module.constant.UserConstants; +import cn.iocoder.yudao.module.rescue.core.redis.RedisCache; +import cn.iocoder.yudao.module.rescue.core.text.Convert; +import cn.iocoder.yudao.module.rescue.domain.SysConfig; +import cn.iocoder.yudao.module.rescue.mapper.RescueSysConfigMapper; +import cn.iocoder.yudao.module.rescue.service.ISysConfigService; +import cn.iocoder.yudao.module.rescue.utils.StringUtils; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.util.Collection; +import java.util.List; + +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; + +/** + * 参数配置 服务层实现 + * + * @author ruoyi + */ +@Service +public class SysConfigServiceImpl implements ISysConfigService { + + @Resource + @Lazy + private RescueSysConfigMapper configMapper2; + + @Resource + private RedisCache redisCache2; + + /** + * 项目启动时,初始化参数到缓存 + */ + @PostConstruct + public void init() { + loadingConfigCache(); + } + + /** + * 查询参数配置信息 + * + * @param configId 参数配置ID + * @return 参数配置信息 + */ + @Override +// @DataSource(DataSourceType.MASTER) + public SysConfig selectConfigById(Long configId) { + SysConfig config = new SysConfig(); + config.setConfigId(configId); + return configMapper2.selectConfig(config); + } + + /** + * 根据键名查询参数配置信息 + * + * @param configKey 参数key + * @return 参数键值 + */ + @Override + public String selectConfigByKey(String configKey) { + String configValue = Convert.toStr(redisCache2.getCacheObject(getCacheKey(configKey))); + if (StringUtils.isNotEmpty(configValue)) { + return configValue; + } + SysConfig config = new SysConfig(); + config.setConfigKey(configKey); + SysConfig retConfig = configMapper2.selectConfig(config); + if (StringUtils.isNotNull(retConfig)) { + redisCache2.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue()); + return retConfig.getConfigValue(); + } + return StringUtils.EMPTY; + } + + /** + * 获取验证码开关 + * + * @return true开启,false关闭 + */ + @Override + public boolean selectCaptchaEnabled() { + String captchaEnabled = selectConfigByKey("sys.account.captchaEnabled"); + if (StringUtils.isEmpty(captchaEnabled)) { + return true; + } + return Convert.toBool(captchaEnabled); + } + + /** + * 查询参数配置列表 + * + * @param config 参数配置信息 + * @return 参数配置集合 + */ + @Override + public List selectConfigList(SysConfig config) { + return configMapper2.selectConfigList(config); + } + + /** + * 新增参数配置 + * + * @param config 参数配置信息 + * @return 结果 + */ + @Override + public int insertConfig(SysConfig config) { + int row = configMapper2.insertConfig(config); + if (row > 0) { + redisCache2.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue()); + } + return row; + } + + /** + * 修改参数配置 + * + * @param config 参数配置信息 + * @return 结果 + */ + @Override + public int updateConfig(SysConfig config) { + SysConfig temp = configMapper2.selectConfigById(config.getConfigId()); + if (!StringUtils.equals(temp.getConfigKey(), config.getConfigKey())) { + redisCache2.deleteObject(getCacheKey(temp.getConfigKey())); + } + + int row = configMapper2.updateConfig(config); + if (row > 0) { + redisCache2.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue()); + } + return row; + } + + /** + * 批量删除参数信息 + * + * @param configIds 需要删除的参数ID + */ + @Override + public void deleteConfigByIds(Long[] configIds) { + for (Long configId : configIds) { + SysConfig config = selectConfigById(configId); + if (StringUtils.equals(UserConstants.YES, config.getConfigType())) { + throw exception(CommonErrorCodeConstants.SYSTEM_ERROR); + } + configMapper2.deleteConfigById(configId); + redisCache2.deleteObject(getCacheKey(config.getConfigKey())); + } + } + + /** + * 加载参数缓存数据 + */ + @Override + public void loadingConfigCache() { + List configsList = configMapper2.selectConfigList(new SysConfig()); + for (SysConfig config : configsList) { + redisCache2.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue()); + } + } + + /** + * 清空参数缓存数据 + */ + @Override + public void clearConfigCache() { + Collection keys = redisCache2.keys(CacheConstants.SYS_CONFIG_KEY + "*"); + redisCache2.deleteObject(keys); + } + + /** + * 重置参数缓存数据 + */ + @Override + public void resetConfigCache() { + clearConfigCache(); + loadingConfigCache(); + } + + /** + * 校验参数键名是否唯一 + * + * @param config 参数配置信息 + * @return 结果 + */ + @Override + public boolean checkConfigKeyUnique(SysConfig config) { + Long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId(); + SysConfig info = configMapper2.checkConfigKeyUnique(config.getConfigKey()); + if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue()) { + return UserConstants.NOT_UNIQUE; + } + return UserConstants.UNIQUE; + } + + /** + * 设置cache key + * + * @param configKey 参数键 + * @return 缓存键key + */ + private String getCacheKey(String configKey) { + return CacheConstants.SYS_CONFIG_KEY + configKey; + } +} diff --git a/dl-module-rescue/src/main/resources/mapper/rescue/RescueSysConfigMapper.xml b/dl-module-rescue/src/main/resources/mapper/rescue/RescueSysConfigMapper.xml new file mode 100644 index 00000000..6a6b1b68 --- /dev/null +++ b/dl-module-rescue/src/main/resources/mapper/rescue/RescueSysConfigMapper.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + select config_id, config_name, config_key, config_value, config_type, create_time, update_time, remark + from sys_config + + + + + + + and config_id = #{configId} + + + and config_key = #{configKey} + + + + + + + + + + + + + + insert into sys_config ( + config_name, + config_key, + config_value, + config_type, + remark, + create_time + )values( + #{configName}, + #{configKey}, + #{configValue}, + #{configType}, + #{remark}, + sysdate() + ) + + + + update sys_config + + config_name = #{configName}, + config_key = #{configKey}, + config_value = #{configValue}, + config_type = #{configType}, + remark = #{remark}, + update_time = sysdate() + + where config_id = #{configId} + + + + delete from sys_config where config_id = #{configId} + + + + delete from sys_config where config_id in + + #{configId} + + + + \ No newline at end of file