提交 030c022e 编写于 作者: doc_wei's avatar doc_wei

feat: 新增开发环境与正式环境的区分

上级 81353331
......@@ -148,6 +148,12 @@
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.27</version>
</dependency>
</dependencies>
<build>
......
......@@ -4,11 +4,21 @@
package com.skyeye;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.skyeye.common.filter.PropertiesUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
import java.util.Map;
import java.io.ByteArrayInputStream;
import java.util.*;
import java.util.stream.Collectors;
/**
* @ClassName: ConfigProperties
......@@ -19,20 +29,85 @@ import java.util.Map;
* 注意:本内容仅限购买后使用.禁止私自外泄以及用于其他的商业目的
*/
@Component
@ConfigurationProperties(prefix = "skyeye.configuation")
public class ConfigProperties {
public ConfigProperties() {
System.setProperty("skyeye.year", String.valueOf(DateUtil.thisYear()));
}
@Value("${spring.cloud.nacos.config.server-addr}")
private String serverAddr;
@Value("${spring.cloud.nacos.config.namespace}")
private String namespace;
@Value("${spring.application.name}.${spring.cloud.nacos.config.file-extension}")
private String dataId;
@Value("${spring.cloud.nacos.config.group}")
private String group;
public Map<String, String> bindPropertiesToObject(String env, String... prefix) {
try {
// 创建 Nacos 客户端
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverAddr);
properties.setProperty(PropertyKeyConst.NAMESPACE, namespace);
// 获取配置服务
ConfigService configService = NacosFactory.createConfigService(properties);
private Map<String, String> config;
// 获取配置
String content = configService.getConfig(dataId, group, 5000);
public Map<String, String> getConfig() {
return config;
// 使用SnakeYAML解析配置内容为Map
Yaml yaml = new Yaml();
ByteArrayInputStream stream = new ByteArrayInputStream(content.getBytes());
Map<String, Object> configMap = yaml.load(stream);
Map<String, String> result = new HashMap<>();
loadYamlConfig(result, configMap, prefix);
Map<String, String> temp = new HashMap<>();
String zuulApiKey = StrUtil.isEmpty(env) ? "${skyeye.zuulApi}" : "${skyeye." + env + ".zuulApi}";
String zuulApi = PropertiesUtil.getPropertiesValue(zuulApiKey);
temp.put(zuulApiKey, zuulApi);
temp.put("${skyeye.year}", String.valueOf(DateUtil.thisYear()));
result.forEach((key, value) -> {
temp.forEach((k, v) -> {
if (value.contains(k)) {
result.put(key, value.replace(k, v));
}
});
});
return result;
} catch (NacosException e) {
e.printStackTrace();
}
return null;
}
public void setConfig(Map<String, String> config) {
this.config = config;
public void loadYamlConfig(Map<String, String> result, Map<String, Object> configMap, String... keys) {
List<String> keyList = Arrays.asList(keys).stream().filter(StrUtil::isNotBlank).collect(Collectors.toList());
if (CollectionUtil.isEmpty(keyList)) {
configMap.forEach((key, value) -> {
result.put(key, value.toString());
});
return;
}
String key = keyList.get(0);
if (configMap.containsKey(key)) {
Object value = configMap.get(key);
if (value instanceof Map) {
if (keyList.size() == 1) {
loadYamlConfig(result, (Map<String, Object>) value, StrUtil.EMPTY);
} else {
loadYamlConfig(result, (Map<String, Object>) value, Arrays.copyOfRange(keys, 1, keys.length));
}
} else {
result.put(key, value.toString());
}
}
}
public Map<String, String> getConfig(String env) {
Map<String, String> map = bindPropertiesToObject(env, "skyeye", "configuation", "config", env);
return map;
}
}
......@@ -6,6 +6,7 @@ package com.skyeye;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
......@@ -25,8 +26,8 @@ public class ConfigrationController {
private ConfigProperties configProperties;
@GetMapping(value = "/getConfigRation")
public Map<String, String> getConfigRation() {
return configProperties.getConfig();
public Map<String, String> getConfigRation(@RequestParam("env") String env) {
return configProperties.getConfig(env);
}
}
......@@ -17,4 +17,5 @@ spring:
# 指定nacos server的地址
server-addr: 172.18.92.40:9000
file-extension: yml
namespace: ${spring.profiles.active} # 配置命名空间
\ No newline at end of file
namespace: ${spring.profiles.active} # 配置命名空间
group: DEFAULT_GROUP # 配置分组
\ No newline at end of file
......@@ -34,10 +34,13 @@ var customerJS = {
"schoolUtil": "../../assets/lib/layui/customer/skyeye/schoolUtil.js", // 学校模块工具类
};
// 登录界面赋值
var env = '';
//系统基础信息
var sysMainMation = {}; // 系统基础信息json
if (isNull(localStorage.getItem("sysMainMation"))) {
jsGetJsonFile("../../configRation.json", function(data) {
jsGetJsonFile("../../configRation.json?env=" + env, function(data) {
sysMainMation = data;
localStorage.setItem("sysMainMation", JSON.stringify(sysMainMation));
initBaseParams();
......
......@@ -9,9 +9,10 @@ layui.config({
winui.renderColor();
layui.use(['form'], function (form) {
var $ = layui.$;
env = GetUrlParam("env");
// 系统配置文件
jsGetJsonFile("../../configRation.json", function(data) {
jsGetJsonFile("../../configRation.json?env=" + env, function(data) {
sysMainMation = data;
localStorage.setItem("sysMainMation", JSON.stringify(sysMainMation));
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册