增加环境变量

This commit is contained in:
wanggeng 2022-09-22 19:10:15 +08:00
parent 0271a882ae
commit 2cc0729bdc
16 changed files with 1157 additions and 1 deletions

View File

@ -0,0 +1,51 @@
package com.cm.common.controller.api.env;
import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.pojo.dtos.env.EnvDTO;
import com.cm.common.pojo.vos.env.EnvListVO;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.service.env.IEnvService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @ClassName: EnvController
* @Description: 环境变量
* @Author: wanggeng
* @Date: 2022/5/16 10:50
* @Version: 1.0
*/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "环境变量")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/env")
public class EnvController extends AbstractController {
@Autowired
private IEnvService envService;
@ApiOperation(value = "更新", notes = "更新")
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PutMapping("update")
@CheckRequestBodyAnnotation
public SuccessResult update(@RequestBody EnvListVO envListVO) {
envService.update(envListVO);
return new SuccessResult();
}
@ApiOperation(value = "获取配置", notes = "获取配置接口")
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@GetMapping("list")
public List<EnvDTO> list() {
return envService.list();
}
}

View File

@ -0,0 +1,28 @@
package com.cm.common.controller.route.env;
import com.cm.common.constants.ISystemConstant;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
/**
* @ClassName: EnvController
* @Description: 环境变量
* @Author: wanggeng
* @Date: 2022/5/16 10:50
* @Version: 1.0
*/
@Api(tags = ISystemConstant.ROUTE_TAGS_PREFIX + "环境变量")
@RestController
@RequestMapping(ISystemConstant.ROUTE_PREFIX + "/env")
public class EnvRouteController {
@GetMapping("update")
public ModelAndView update() {
ModelAndView modelAndView = new ModelAndView("env/update");
return modelAndView;
}
}

View File

@ -0,0 +1,65 @@
package com.cm.common.dao.env;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.UpdateException;
import com.cm.common.pojo.dtos.env.EnvDTO;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
* @ClassName: IEnvDao
* @Description: 环境变量
* @Author: wanggeng
* @Date: 2022/5/16 10:24
* @Version: 1.0
*/
@Repository
public interface IEnvDao {
void createTable();
/**
* 新增
*
* @param params
* @throws SaveException
*/
void save(Map<String, Object> params) throws SaveException;
/**
* 修改
*
* @param params
* @throws UpdateException
*/
void update(Map<String, Object> params) throws UpdateException;
/**
* 删除
*
* @throws RemoveException
*/
void delete() throws RemoveException;
/**
* 列表
*
* @return
* @throws SearchException
*/
List<EnvDTO> list() throws SearchException;
/**
* 详情
*
* @param envKey
* @return
* @throws SearchException
*/
EnvDTO get(String envKey) throws SearchException;
}

View File

@ -0,0 +1,83 @@
package com.cm.common.manager.env;
import com.cm.common.dao.env.IEnvDao;
import com.cm.common.pojo.Env;
import com.cm.common.pojo.dtos.env.EnvDTO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @ClassName: EnvManager
* @Description: 环境变量管理
* @Author: wanggeng
* @Date: 2022/5/16 10:47
* @Version: 1.0
*/
public class EnvManager {
private static final Logger LOG = LoggerFactory.getLogger(EnvManager.class);
private static EnvManager envManager = EnvManagerBuilder.envManager;
private static Pattern ENV_VARIABLE = Pattern.compile("%[a-zA-Z\\d\\_\\-]+%");
private IEnvDao envDao;
private Env env = new Env();
private EnvManager() {}
public static EnvManager getInstance() {
return envManager;
}
/**
* 刷新环境变量
*/
public void refreshEnv() {
env.clear();
List<EnvDTO> envDTOS = envDao.list();
envDTOS.forEach(envDTO -> {
env.put(envDTO.getEnvKey(), envDTO.getEnvValue());
});
}
/**
* 获得环境变量
*
* @return
*/
public Env getEnv() {
return env;
}
public boolean isKeyExist(String key) {
return env.containsKey(key);
}
public String getValue(String key) {
String value = env.get(key);
value = value == null ? "" : value;
// 替换变量
Matcher matcher = ENV_VARIABLE.matcher(value);
while (matcher.find()) {
String variable = matcher.group();
String variableValue = env.get(variable.substring(1, variable.length() - 1));
if (StringUtils.isBlank(variableValue)) {
continue;
}
value = value.replaceFirst(variable, variableValue);
}
return value;
}
public void setEnvDao(IEnvDao envDao) {
this.envDao = envDao;
}
private static class EnvManagerBuilder {
public static final EnvManager envManager = new EnvManager();
}
}

View File

@ -0,0 +1,83 @@
package com.cm.common.pojo;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* @ClassName: Env
* @Description: 环境变量
* @Author: wanggeng
* @Date: 2022/5/16 14:46
* @Version: 1.0
*/
public class Env implements Map<String, String> {
Map<String, String> envMap = new ConcurrentHashMap<>();
@Override
public int size() {
return envMap.size();
}
@Override
public boolean isEmpty() {
return envMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return envMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return envMap.containsValue(value);
}
@Override
public String get(Object key) {
return envMap.get(key);
}
@Override
public String put(String key, String value) {
return envMap.put(key, value);
}
@Override
public String remove(Object key) {
return envMap.remove(key);
}
@Override
public void putAll(Map<? extends String, ? extends String> m) {
envMap.putAll(m);
}
@Override
public void clear() {
envMap.clear();
}
@Override
public Set<String> keySet() {
return envMap.keySet();
}
@Override
public Collection<String> values() {
return envMap.values();
}
@Override
public Set<Entry<String, String>> entrySet() {
return envMap.entrySet();
}
@Override
public String toString() {
return envMap.toString();
}
}

View File

@ -0,0 +1,46 @@
package com.cm.common.pojo.dtos.env;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
/**
* @ClassName: EnvDTO
* @Description: 环境变量
* @Author: wanggeng
* @Date: 2022/5/16 10:36
* @Version: 1.0
*/
public class EnvDTO implements Serializable {
@ApiModelProperty(name = "envKey", value = "环境变量")
private String envKey;
@ApiModelProperty(name = "envExplain", value = "环境变量说明")
private String envExplain;
@ApiModelProperty(name = "envValue", value = "环境变量值")
private String envValue;
public String getEnvKey() {
return envKey == null ? "" : envKey.trim();
}
public void setEnvKey(String envKey) {
this.envKey = envKey;
}
public String getEnvExplain() {
return envExplain == null ? "" : envExplain.trim();
}
public void setEnvExplain(String envExplain) {
this.envExplain = envExplain;
}
public String getEnvValue() {
return envValue == null ? "" : envValue.trim();
}
public void setEnvValue(String envValue) {
this.envValue = envValue;
}
}

View File

@ -0,0 +1,38 @@
package com.cm.common.pojo.vos.env;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: EnvPO
* @Description: 环境变量
* @Author: wanggeng
* @Date: 2022/5/16 10:36
* @Version: 1.0
*/
@ApiModel
public class EnvListVO {
@ApiModelProperty(name = "envs", value = "环境变量列表")
private List<EnvVO> envs;
public List<EnvVO> getEnvs() {
return envs == null ? new ArrayList() : envs;
}
public void setEnvs(List<EnvVO> envs) {
this.envs = envs;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"envs\":")
.append(envs);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,59 @@
package com.cm.common.pojo.vos.env;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* @ClassName: EnvPO
* @Description: 环境变量
* @Author: wanggeng
* @Date: 2022/5/16 10:36
* @Version: 1.0
*/
@ApiModel
public class EnvVO {
@ApiModelProperty(name = "envKey", value = "环境变量")
private String envKey;
@ApiModelProperty(name = "envExplain", value = "环境变量说明")
private String envExplain;
@ApiModelProperty(name = "envValue", value = "环境变量值")
private String envValue;
public String getEnvKey() {
return envKey == null ? "" : envKey.trim();
}
public void setEnvKey(String envKey) {
this.envKey = envKey;
}
public String getEnvExplain() {
return envExplain == null ? "" : envExplain.trim();
}
public void setEnvExplain(String envExplain) {
this.envExplain = envExplain;
}
public String getEnvValue() {
return envValue == null ? "" : envValue.trim();
}
public void setEnvValue(String envValue) {
this.envValue = envValue;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"envKey\":\"")
.append(envKey).append('\"');
sb.append(",\"envExplain\":\"")
.append(envExplain).append('\"');
sb.append(",\"envValue\":\"")
.append(envValue).append('\"');
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,32 @@
package com.cm.common.service.env;
import com.cm.common.pojo.dtos.env.EnvDTO;
import com.cm.common.pojo.vos.env.EnvListVO;
import java.util.List;
/**
* @ClassName: IEnvService
* @Description: 环境变量
* @Author: wanggeng
* @Date: 2022/5/16 10:48
* @Version: 1.0
*/
public interface IEnvService {
/**
* 更新
*
* @param envMap
*/
void update(EnvListVO envListVO);
/**
* 详情
*
* @return
*/
List<EnvDTO> list();
}

View File

@ -0,0 +1,52 @@
package com.cm.common.service.env.impl;
import com.cm.common.base.AbstractService;
import com.cm.common.dao.env.IEnvDao;
import com.cm.common.manager.env.EnvManager;
import com.cm.common.pojo.Env;
import com.cm.common.pojo.dtos.env.EnvDTO;
import com.cm.common.pojo.vos.env.EnvListVO;
import com.cm.common.pojo.vos.env.EnvVO;
import com.cm.common.service.env.IEnvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @ClassName: EnvServiceImpl
* @Description: 环境变量
* @Author: wanggeng
* @Date: 2022/5/16 10:48
* @Version: 1.0
*/
@Service
public class EnvServiceImpl extends AbstractService implements IEnvService {
@Autowired
private IEnvDao envDao;
@Override
public void update(EnvListVO envListVO) {
// 清空
envDao.delete();
// 新增
Map<String, Object> params = getHashMap(5);
for (EnvVO envVO : envListVO.getEnvs()) {
params.put("envKey", envVO.getEnvKey());
params.put("envExplain", envVO.getEnvExplain());
params.put("envValue", envVO.getEnvValue());
envDao.save(params);
}
// 刷新缓存
EnvManager.getInstance().refreshEnv();
}
@Override
public List<EnvDTO> list() {
Env env = EnvManager.getInstance().getEnv();
System.out.println(env.toString());
return envDao.list();
}
}

View File

@ -0,0 +1,35 @@
package com.cm.common.start;
import com.cm.common.dao.env.IEnvDao;
import com.cm.common.manager.env.EnvManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* @ClassName: CommonStartUp
* @Description: 启动
* @Author: wanggeng
* @Date: 2022/9/22 17:32
* @Version: 1.0
*/
@Component
public class CommonStartUp implements ApplicationRunner {
private static final Logger LOG = LoggerFactory.getLogger(CommonStartUp.class);
@Autowired
private IEnvDao envDao;
@Override
public void run(ApplicationArguments args) throws Exception {
LOG.debug(">>>>>> 初始化环境变量");
envDao.createTable();
EnvManager envManager = EnvManager.getInstance();
envManager.setEnvDao(envDao);
envManager.refreshEnv();
}
}

View File

@ -98,6 +98,10 @@ public class RegexUtil {
* 强密码
*/
private static final Pattern PASSWORD_STRONG = Pattern.compile(String.format("(?=.*[A-Za-z])(?=.*[0-9])(?=.*[%s]).{%d,%d}", SPECIAL_CHARACTERS, PASSWORD_LENGTH_MIN, PASSWORD_LENGTH_MAX));
/**
* 字母和数字
*/
private static final Pattern PATTERN_LETTER_OR_NUMBER = Pattern.compile("[a-zA-Z0-9]+");
/**
* 判断弱密码强度
@ -289,4 +293,14 @@ public class RegexUtil {
return customPattern.matcher(input).matches();
}
/**
* 判断是字母或数字
*
* @param input
* @return
*/
public static boolean isLetterOrNumber(String input) {
return PATTERN_LETTER_OR_NUMBER.matcher(input).matches();
}
}

View File

@ -7,7 +7,10 @@ import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -282,4 +285,108 @@ public class WStringUtil {
return strBuf.toString();
}
/**
* 从字符串中随机取出新的字符串
*
* @param input 输入字符串
* @param outLength 新字符串长度
* @return
*/
public static String randomSubStr(String input, int outLength) {
if (input == null || input.isEmpty()) {
return null;
}
if (outLength >= input.length()) {
outLength = input.length();
}
if (outLength <= 0) {
outLength = 6;
}
StringBuilder subStrSB = new StringBuilder();
Random random = new Random();
for (int i = 0; i < outLength; i++) {
subStrSB.append(input.charAt(random.nextInt(input.length())));
}
return subStrSB.toString();
}
/**
* string对象转数组
*
* @param strObj stirng 对象
* @param separator 分隔符
* @return 字符串数组
*/
public static String[] strObjToArray(Object strObj, String separator) {
if (strObj == null || separator == null) {
throw new RuntimeException("参数不能为空!");
}
if (!(strObj instanceof String)) {
throw new RuntimeException("第一个参数只能是String类型");
}
return strObj.toString().split(separator);
}
/**
* 字符串列表转字符串
*
* @param strings 字符串列表
* @param separator 分隔符
* @return
*/
public static String listToStr(List<String> strings, String separator) {
if (strings == null || strings.isEmpty()) {
return null;
}
StringBuilder sb = new StringBuilder();
for (String str : strings) {
if (sb.length() > 0) {
sb.append(separator);
}
sb.append(str);
}
return sb.toString();
}
/**
* 倒序截断重复字符
*
* @return
*/
public static String cutContinuityRepeatCharDesc(String str, char repeatChar) {
if (StringUtils.isBlank(str)) {
return str;
}
int unRepeatCharIndex = 0;
for (int i = str.length() - 1; i >= 0; i--) {
if (str.charAt(i) != repeatChar) {
unRepeatCharIndex = i;
break;
}
}
return str.substring(0, unRepeatCharIndex + 1);
}
/**
* 倒叙截取重复字段
*
* @param str
* @param repeatChar
* @param minRepeatCount
* @return
*/
public static String cutContinuityRepeatCharDesc(String str, char repeatChar, int minRepeatCount) {
int repeatCount = 0;
for (int i = str.length() - 1; i >= 0; i--) {
if (str.charAt(i) != repeatChar) {
break;
}
repeatCount++;
}
if (repeatCount < minRepeatCount) {
return str;
}
return cutContinuityRepeatCharDesc(str, repeatChar);
}
}

View File

@ -1,6 +1,5 @@
package com.cm.common.utils.point;
import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@ -163,6 +162,146 @@ public class PointUtil {
return new BigDecimal(distance).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 计算面积
*
* @param points
* @return
*/
public static double getSqua(List<Point> points) {
double area = 0;
int size = points.size();
if (size > 2) {
double LowX = 0.0;
double LowY = 0.0;
double MiddleX = 0.0;
double MiddleY = 0.0;
double HighX = 0.0;
double HighY = 0.0;
double AM = 0.0;
double BM = 0.0;
double CM = 0.0;
double AL = 0.0;
double BL = 0.0;
double CL = 0.0;
double AH = 0.0;
double BH = 0.0;
double CH = 0.0;
double CoefficientL = 0.0;
double CoefficientH = 0.0;
double ALtangent = 0.0;
double BLtangent = 0.0;
double CLtangent = 0.0;
double AHtangent = 0.0;
double BHtangent = 0.0;
double CHtangent = 0.0;
double ANormalLine = 0.0;
double BNormalLine = 0.0;
double CNormalLine = 0.0;
double OrientationValue = 0.0;
double AngleCos = 0.0;
double Sum1 = 0.0;
double Sum2 = 0.0;
double Count2 = 0;
double Count1 = 0;
double Sum = 0.0;
double Radius = 6378000;
for (int i = 0; i < size; i++) {
if (i == 0) {
LowX = points.get(size - 1).getX() * Math.PI / 180;
LowY = points.get(size - 1).getY() * Math.PI / 180;
MiddleX = points.get(0).getX() * Math.PI / 180;
MiddleY = points.get(0).getY() * Math.PI / 180;
HighX = points.get(1).getX() * Math.PI / 180;
HighY = points.get(1).getY() * Math.PI / 180;
} else if (i == size - 1) {
LowX = points.get(size - 2).getX() * Math.PI / 180;
LowY = points.get(size - 2).getY() * Math.PI / 180;
MiddleX = points.get(size - 1).getX() * Math.PI / 180;
MiddleY = points.get(size - 1).getY() * Math.PI / 180;
HighX = points.get(0).getX() * Math.PI / 180;
HighY = points.get(0).getY() * Math.PI / 180;
} else {
LowX = points.get(i - 1).getX() * Math.PI / 180;
LowY = points.get(i - 1).getY() * Math.PI / 180;
MiddleX = points.get(i).getX() * Math.PI / 180;
MiddleY = points.get(i).getY() * Math.PI / 180;
HighX = points.get(i + 1).getX() * Math.PI / 180;
HighY = points.get(i + 1).getY() * Math.PI / 180;
}
AM = Math.cos(MiddleY) * Math.cos(MiddleX);
BM = Math.cos(MiddleY) * Math.sin(MiddleX);
CM = Math.sin(MiddleY);
AL = Math.cos(LowY) * Math.cos(LowX);
BL = Math.cos(LowY) * Math.sin(LowX);
CL = Math.sin(LowY);
AH = Math.cos(HighY) * Math.cos(HighX);
BH = Math.cos(HighY) * Math.sin(HighX);
CH = Math.sin(HighY);
CoefficientL = (AM * AM + BM * BM + CM * CM) / (AM * AL + BM * BL + CM * CL);
CoefficientH = (AM * AM + BM * BM + CM * CM) / (AM * AH + BM * BH + CM * CH);
ALtangent = CoefficientL * AL - AM;
BLtangent = CoefficientL * BL - BM;
CLtangent = CoefficientL * CL - CM;
AHtangent = CoefficientH * AH - AM;
BHtangent = CoefficientH * BH - BM;
CHtangent = CoefficientH * CH - CM;
AngleCos = (AHtangent * ALtangent + BHtangent * BLtangent + CHtangent * CLtangent) / (
Math.sqrt(AHtangent * AHtangent + BHtangent * BHtangent + CHtangent * CHtangent)
* Math.sqrt(ALtangent * ALtangent + BLtangent * BLtangent
+ CLtangent * CLtangent));
AngleCos = Math.acos(AngleCos);
ANormalLine = BHtangent * CLtangent - CHtangent * BLtangent;
BNormalLine = 0 - (AHtangent * CLtangent - CHtangent * ALtangent);
CNormalLine = AHtangent * BLtangent - BHtangent * ALtangent;
if (AM != 0) {
OrientationValue = ANormalLine / AM;
} else if (BM != 0) {
OrientationValue = BNormalLine / BM;
} else {
OrientationValue = CNormalLine / CM;
}
if (OrientationValue > 0) {
Sum1 += AngleCos;
Count1++;
} else {
Sum2 += AngleCos;
Count2++;
//Sum +=2*Math.PI-AngleCos;
}
}
if (Sum1 > Sum2) {
Sum = Sum1 + (2 * Math.PI * Count2 - Sum2);
} else {
Sum = (2 * Math.PI * Count1 - Sum1) + Sum2;
}
//平方米
area = (Sum - (size - 2) * Math.PI) * Radius * Radius;
}
return Math.abs(area);
}
public static void main(String[] args) {
Point point = new Point(111.770495, 40.871839);
List<Point> points = new ArrayList<>();

View File

@ -0,0 +1,72 @@
<?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="com.cm.common.dao.env.IEnvDao">
<resultMap id="envDTO" type="com.cm.common.pojo.dtos.env.EnvDTO">
<result column="env_key" property="envKey"/>
<result column="env_explain" property="envExplain"/>
<result column="env_value" property="envValue"/>
</resultMap>
<!-- 建表 -->
<update id="createTable">
CREATE TABLE IF NOT EXISTS `sys_env` (
`env_key` varchar(255) NOT NULL COMMENT '环境变量Key',
`env_explain` varchar(255) DEFAULT NULL COMMENT '环境变量说明',
`env_value` longtext COMMENT '环境变量值',
PRIMARY KEY (`env_key`),
UNIQUE KEY `env_key` (`env_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统环境变量';
</update>
<!-- 新增环境变量 -->
<insert id="save" parameterType="map">
INSERT INTO sys_env(
env_key,
env_explain,
env_value
) VALUES(
#{envKey},
#{envExplain},
#{envValue}
)
</insert>
<!-- 删除环境变量 -->
<delete id="delete" parameterType="map">
DELETE FROM sys_env
</delete>
<!-- 更新环境变量 -->
<update id="update" parameterType="map">
UPDATE
sys_env
SET
env_value = #{envValue},
env_explain = #{envExplain}
WHERE
env_key = #{envKey}
</update>
<!-- 环境变量列表 -->
<select id="list" resultMap="envDTO">
SELECT
env_key,
env_explain,
env_value
FROM
sys_env
</select>
<!-- 获取环境变量 -->
<select id="get" parameterType="String" resultMap="envDTO">
SELECT
env_key,
env_explain,
env_value
FROM
sys_env
WHERE
env_key = #{_parameter}
</select>
</mapper>

View File

@ -0,0 +1,252 @@
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<base th:href="${#request.getContextPath() + '/'}">
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
<link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
<link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein">
<form class="layui-form" lay-filter="dataForm">
<div class="layui-card">
<div class="layui-card-header">
<span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
<a href="javascript:void(0);"><cite>环境变量(参数名唯一且只能是字母、数字、下划线与英文横线的组合。可以通过 %变量名% 的方式相互引用。)</cite></a>
</span>
<button id="envPlusBtn" type="button" class="layui-btn layui-btn-xs" style="float: right; margin-top: 12px;">
<i class="fa fa-plus" aria-hidden="true"></i>
</button>
</div>
<div id="cardBody" class="layui-card-body" style="padding: 15px; overflow: auto">
<div class="layui-form-item">
<table class="layui-table">
<colgroup>
<col width="250">
<col>
<col width="250">
<col width="60">
</colgroup>
<thead>
<tr>
<th>变量名</th>
<th>变量值</th>
<th>描述</th>
<th style="text-align: center;">操作</th>
</tr>
</thead>
<tbody id="envBody"></tbody>
</table>
</div>
</div>
</div>
<div class="layui-form-item layui-layout-admin">
<div class="layui-input-block">
<div class="layui-footer" style="left: 0;">
<button type="button" class="layui-btn" lay-submit lay-filter="submitForm">提交更新</button>
</div>
</div>
</div>
</form>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
layui.config({
base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
index: 'lib/index' //主入口模块
}).use(['index', 'form', 'laydate', 'laytpl'], function(){
var $ = layui.$;
var form = layui.form;
var $win = $(window);
var windowHeight = $(window).height();
$('#cardBody').css({
height: (windowHeight - 170) +'px'
})
var envs;
// 系统参数配置
function EnvsInit(idPrefix, classPrefix, envList) {
var envsArray = [];
var keyupSetTimeout;
function getTr(index, key, value, explain) {
return '<tr>' +
' <td>' +
' <input type="text" id="'+ idPrefix +'Key'+ index +'" placeholder="输入参数名" class="layui-input '+ classPrefix +'-key" value="'+ (key ? key : '') +'" data-index="'+ index +'">' +
' </td>' +
' <td>' +
' <input type="text" id="'+ idPrefix +'Value'+ index +'" placeholder="输入参数值" class="layui-input '+ classPrefix +'-value" value="'+ (value ? value : '') +'" data-index="'+ index +'">' +
' </td>' +
' <td>' +
' <input type="text" id="'+ idPrefix +'Explain'+ index +'" placeholder="输入描述" class="layui-input '+ classPrefix +'-explain" value="'+ (explain ? explain : '') +'" data-index="'+ index +'">' +
' </td>' +
' <td style="text-align: center;">' +
' <button type="button" class="layui-btn layui-btn-xs layui-btn-danger '+ classPrefix +'-remove-btn" data-index="'+ index +'">' +
' <i class="fa fa-times" aria-hidden="true"></i>' +
' </button>' +
' </td>' +
'</tr>';
}
function refreshTr() {
var trs = '';
for(var i = 0; i < envsArray.length; i++) {
var item = envsArray[i];
trs += getTr(i, item.envKey, item.envValue, item.envExplain);
}
$('#'+ idPrefix +'Body').empty();
$('#'+ idPrefix +'Body').append(trs);
}
function isKeyExist(index, key) {
if(!key) {
return false;
}
for(var i = 0, item; item = envsArray[i++];) {
if(key == item.envKey && index != (i - 1)) {
return true;
}
}
return false;
}
function isKeyEffective(key) {
if((/^[a-zA-Z0-9\_\-]+$/g.test(key))) {
return true;
}
return false;
}
$(document).on('keyup', '.'+ classPrefix +'-key', function() {
var self = this;
var index = this.dataset.index;
this.value = this.value.replace(/\s/g, '');
var value = this.value;
if(keyupSetTimeout) {
clearTimeout(keyupSetTimeout);
}
keyupSetTimeout = setTimeout(function() {
if(!isKeyEffective(value)) {
top.dialog.msg('参数名只能是字母、数字、下划线与英文横线组合');
self.focus();
self.value = '';
return;
}
if(isKeyExist(index, value)) {
top.dialog.msg('参数名重复');
self.focus();
self.value = '';
return;
} else {
envsArray[index].envKey = value;
}
}, 500);
});
$(document).on('keyup', '.'+ classPrefix +'-value', function() {
var index = this.dataset.index;
this.value = this.value.replace(/\s/g, '');
envsArray[index].envValue = this.value;
});
$(document).on('keyup', '.'+ classPrefix +'-explain', function() {
var index = this.dataset.index;
this.value = this.value.replace(/\s/g, '');
envsArray[index].envExplain = this.value;
});
$(document).on('click', '#'+ idPrefix +'PlusBtn', function() {
envsArray.push({
envKey: '',
envValue: '',
envExplain: '',
});
refreshTr();
})
$(document).on('click', '.'+ classPrefix +'-remove-btn', function() {
var index = this.dataset.index;
envsArray.splice(index, 1);
refreshTr();
})
this.init = function() {
if(!envList) {
return;
}
for(var i = 0, item; item = envList[i++];) {
envsArray.push({
envKey: item.envKey,
envValue: item.envValue,
envExplain: item.envExplain
})
}
refreshTr();
}
this.listEnvs = function() {
return envsArray;
}
};
// 初始化
function initData() {
var loadLayerIndex;
top.restAjax.get(top.restAjax.path('api/env/list', []), {}, null, function(code, data) {
envs = new EnvsInit('env', 'env', data);
envs.init();
}, function(code, data) {
top.dialog.msg(data.msg);
}, function() {
loadLayerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
}, function() {
top.dialog.close(loadLayerIndex);
});
}
initData();
// 提交表单
form.on('submit(submitForm)', function(formData) {
top.dialog.confirm(top.dataMessage.commit, function(index) {
top.dialog.close(index);
var loadLayerIndex;
top.restAjax.put(top.restAjax.path('api/env/update', []), {
envs: envs.listEnvs()
}, null, function(code, data) {
var layerIndex = top.dialog.msg('更新成功', {
time: 0,
btn: [top.dataMessage.button.yes],
shade: 0.3,
yes: function(index) {
top.dialog.close(index);
window.location.reload();
}
});
}, function(code, data) {
top.dialog.msg(data.msg);
}, function() {
loadLayerIndex = top.dialog.msg(top.dataMessage.committing, {icon: 16, time: 0, shade: 0.3});
}, function() {
top.dialog.close(loadLayerIndex);
});
});
return false;
});
form.verify({
positiveNumber: function(value, item) {
if(value < 0) {
return '必须是正数';
}
}
});
});
</script>
</body>
</html>