mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 15:55:18 +08:00
添加系统参数配置
This commit is contained in:
7
src/main/java/cn/lihongjie/coal/common/Constants.java
Normal file
7
src/main/java/cn/lihongjie/coal/common/Constants.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package cn.lihongjie.coal.common;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Constants {
|
||||
public static String SYSCONFIG_ENABLE_CAPTCHA = "enable_captcha";
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package cn.lihongjie.coal.session;
|
||||
|
||||
|
||||
import cn.lihongjie.coal.common.Constants;
|
||||
import cn.lihongjie.coal.common.RequestUtils;
|
||||
import cn.lihongjie.coal.organization.entity.OrganizationEntity;
|
||||
import cn.lihongjie.coal.organization.service.OrganizationService;
|
||||
import cn.lihongjie.coal.sysconfig.service.SysConfigService;
|
||||
import cn.lihongjie.coal.user.service.UserService;
|
||||
import cn.lihongjie.coal.user.entity.UserEntity;
|
||||
import cn.lihongjie.coal.exception.BizException;
|
||||
@@ -40,6 +42,8 @@ public class SessionService {
|
||||
@Autowired
|
||||
StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Autowired
|
||||
SysConfigService sysConfigService;
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
@@ -47,6 +51,11 @@ public class SessionService {
|
||||
* @return
|
||||
*/
|
||||
public CaptchaDto genCaptcha() {
|
||||
boolean enable = sysConfigService.isEnable(Constants.SYSCONFIG_ENABLE_CAPTCHA);
|
||||
if (!enable) {
|
||||
return new CaptchaDto("", "");
|
||||
}
|
||||
|
||||
// 三个参数分别为宽、高、位数
|
||||
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
|
||||
// 设置字体
|
||||
|
||||
@@ -5,6 +5,7 @@ import cn.lihongjie.coal.base.controller.BaseController;
|
||||
import cn.lihongjie.coal.base.dto.CommonQuery;
|
||||
import cn.lihongjie.coal.base.dto.IdRequest;
|
||||
import cn.lihongjie.coal.sysconfig.dto.CreateSysConfigDto;
|
||||
import cn.lihongjie.coal.sysconfig.dto.SaveAllConfigDto;
|
||||
import cn.lihongjie.coal.sysconfig.dto.SysConfigDto;
|
||||
import cn.lihongjie.coal.sysconfig.dto.UpdateSysConfigDto;
|
||||
import cn.lihongjie.coal.sysconfig.service.SysConfigService;
|
||||
@@ -42,7 +43,12 @@ public class SysConfigController extends BaseController {
|
||||
this.service.delete(dto);
|
||||
return true;
|
||||
}
|
||||
|
||||
@PostMapping("/saveAllConfig")
|
||||
@SysLog(action = "更新系统配置")
|
||||
public Object saveAllConfig(@RequestBody SaveAllConfigDto dto) {
|
||||
this.service.saveAllConfig(dto);
|
||||
return true;
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
public Page<SysConfigDto> list(@RequestBody CommonQuery dto) {
|
||||
|
||||
@@ -11,8 +11,23 @@ public class CreateSysConfigDto extends CommonDto {
|
||||
@Comment("配置类型")
|
||||
private String type;
|
||||
|
||||
@Comment("配置类型如果是数字, 最小值")
|
||||
private Integer minValue;
|
||||
|
||||
|
||||
@Comment("配置类型如果是数字, 最小值")
|
||||
private Integer maxValue;
|
||||
|
||||
|
||||
|
||||
@Comment("配置类型如果是字符串, 正则表达式校验")
|
||||
private String regexValidator;
|
||||
|
||||
|
||||
@Comment("配置类型如果是数据字典, 字典编码")
|
||||
|
||||
private String dictCode;
|
||||
|
||||
@Comment("值")
|
||||
private String configVal;
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package cn.lihongjie.coal.sysconfig.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class SaveAllConfigDto {
|
||||
private List<CreateSysConfigDto> configs;
|
||||
}
|
||||
@@ -10,8 +10,23 @@ public class SysConfigDto extends CommonDto {
|
||||
|
||||
@Comment("配置类型")
|
||||
private String type;
|
||||
@Comment("配置类型如果是数字, 最小值")
|
||||
private Integer minValue;
|
||||
|
||||
|
||||
@Comment("配置类型如果是数字, 最小值")
|
||||
private Integer maxValue;
|
||||
|
||||
|
||||
|
||||
@Comment("配置类型如果是字符串, 正则表达式校验")
|
||||
private String regexValidator;
|
||||
|
||||
|
||||
@Comment("配置类型如果是数据字典, 字典编码")
|
||||
|
||||
private String dictCode;
|
||||
|
||||
|
||||
@Comment("值")
|
||||
private String configVal;
|
||||
|
||||
@@ -11,8 +11,23 @@ public class UpdateSysConfigDto extends CommonDto {
|
||||
@Comment("配置类型")
|
||||
private String type;
|
||||
|
||||
@Comment("配置类型如果是数字, 最小值")
|
||||
private Integer minValue;
|
||||
|
||||
|
||||
@Comment("配置类型如果是数字, 最小值")
|
||||
private Integer maxValue;
|
||||
|
||||
|
||||
|
||||
@Comment("配置类型如果是字符串, 正则表达式校验")
|
||||
private String regexValidator;
|
||||
|
||||
|
||||
@Comment("配置类型如果是数据字典, 字典编码")
|
||||
|
||||
private String dictCode;
|
||||
|
||||
@Comment("值")
|
||||
private String configVal;
|
||||
|
||||
|
||||
@@ -12,21 +12,54 @@ public class SysConfigEntity extends CommonEntity {
|
||||
|
||||
|
||||
|
||||
@Comment("配置类型")
|
||||
@Comment("配置类型 1 字符串 2 数字 3 数据字典")
|
||||
private String type;
|
||||
|
||||
@Comment("配置类型如果是数字, 最小值")
|
||||
private Integer minValue;
|
||||
|
||||
|
||||
@Comment("配置类型如果是数字, 最小值")
|
||||
private Integer maxValue;
|
||||
|
||||
|
||||
|
||||
@Comment("配置类型如果是字符串, 正则表达式校验")
|
||||
private String regexValidator;
|
||||
|
||||
|
||||
@Comment("配置类型如果是数据字典, 字典编码")
|
||||
|
||||
private String dictCode;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Comment("值")
|
||||
private String configVal;
|
||||
|
||||
|
||||
public void validate(String configVal) {
|
||||
|
||||
switch (type) {
|
||||
case "1":
|
||||
if (!configVal.matches(regexValidator)) {
|
||||
throw new RuntimeException("字符串不符合正则表达式");
|
||||
}
|
||||
break;
|
||||
case "2":
|
||||
Integer integer = Integer.valueOf(configVal);
|
||||
if (integer < minValue || integer > maxValue) {
|
||||
throw new RuntimeException("数字不符合要求");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,5 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface SysConfigRepository extends BaseRepository<SysConfigEntity> {
|
||||
SysConfigEntity findByCode(String configKey);
|
||||
}
|
||||
@@ -3,14 +3,20 @@ package cn.lihongjie.coal.sysconfig.service;
|
||||
import cn.lihongjie.coal.base.dto.CommonQuery;
|
||||
import cn.lihongjie.coal.base.dto.IdRequest;
|
||||
import cn.lihongjie.coal.base.service.BaseService;
|
||||
import cn.lihongjie.coal.common.CollectionUtils;
|
||||
import cn.lihongjie.coal.common.Constants;
|
||||
import cn.lihongjie.coal.exception.BizException;
|
||||
import cn.lihongjie.coal.sysconfig.dto.CreateSysConfigDto;
|
||||
import cn.lihongjie.coal.sysconfig.dto.SaveAllConfigDto;
|
||||
import cn.lihongjie.coal.sysconfig.dto.SysConfigDto;
|
||||
import cn.lihongjie.coal.sysconfig.dto.UpdateSysConfigDto;
|
||||
import cn.lihongjie.coal.sysconfig.entity.SysConfigEntity;
|
||||
import cn.lihongjie.coal.sysconfig.mapper.SysConfigMapper;
|
||||
import cn.lihongjie.coal.sysconfig.repository.SysConfigRepository;
|
||||
import io.vavr.Tuple2;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -18,6 +24,11 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class SysConfigService extends BaseService<SysConfigEntity, SysConfigRepository> {
|
||||
@@ -35,6 +46,67 @@ public class SysConfigService extends BaseService<SysConfigEntity, SysConfigRepo
|
||||
|
||||
}
|
||||
|
||||
public void initDefault(){
|
||||
|
||||
|
||||
Map<String, SysConfigEntity> all = StreamSupport.stream(findAll().spliterator(), false).collect(Collectors.toMap(e -> e.getCode(), e -> e));
|
||||
|
||||
|
||||
if(!all.containsKey(Constants.SYSCONFIG_ENABLE_CAPTCHA)){
|
||||
SysConfigEntity entity = new SysConfigEntity();
|
||||
entity.setName("验证码状态");
|
||||
entity.setCode(Constants.SYSCONFIG_ENABLE_CAPTCHA);
|
||||
entity.setConfigVal("1");
|
||||
entity.setDictCode("status.type");
|
||||
entity.setMaxValue(null);
|
||||
entity.setMinValue(null );
|
||||
entity.setRegexValidator(null);
|
||||
entity.setType("3");
|
||||
repository.save(entity);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getConfigVal(String configKey) {
|
||||
|
||||
SysConfigEntity config = this.repository.findByCode(configKey);
|
||||
|
||||
return config.getConfigVal();
|
||||
}
|
||||
|
||||
public boolean isEnable(String configKey){
|
||||
return StringUtils.equalsIgnoreCase(getConfigVal(configKey), "1");
|
||||
}
|
||||
|
||||
|
||||
public void saveAllConfig(SaveAllConfigDto dto) {
|
||||
|
||||
|
||||
List<SysConfigEntity> all = findAll();
|
||||
|
||||
List<Tuple2<SysConfigEntity, CreateSysConfigDto>> joins = CollectionUtils.leftHashJoin(all, dto.getConfigs(), SysConfigEntity::getCode, CreateSysConfigDto::getCode);
|
||||
|
||||
|
||||
joins.forEach(x -> {
|
||||
|
||||
if (x._2 == null) {
|
||||
throw new BizException("缺少配置项 {}", x._1.getName());
|
||||
};
|
||||
|
||||
x._1.validate(x._2.getConfigVal());
|
||||
x._1.setConfigVal(x._2.getConfigVal());
|
||||
|
||||
});
|
||||
|
||||
|
||||
this.repository.saveAll(joins.stream().map(x -> x._1).collect(Collectors.toList()));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public SysConfigDto create(CreateSysConfigDto request) {
|
||||
|
||||
|
||||
0
src/main/resources/db/migration/V4__sysconfig.sql
Normal file
0
src/main/resources/db/migration/V4__sysconfig.sql
Normal file
Reference in New Issue
Block a user