From 2d8dc64971a203e5cb2485bf1714892a8005fc0f Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 30 十二月 2024 14:54:52 +0800
Subject: [PATCH] 1

---
 se-modules/se-system/src/main/java/com/se/system/service/NacosService.java         |   63 ++++++++-
 se-modules/se-system/src/main/java/com/se/system/utils/StringUtils.java            |   24 ++++
 se-modules/se-system/src/main/resources/bootstrap.yml                              |    2 
 se-modules/se-system/src/main/java/com/se/system/domain/vo/NacosConfigVo.java      |  199 +++++++++++++++++++++++++++++++++
 se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java |   17 ++
 5 files changed, 294 insertions(+), 11 deletions(-)

diff --git a/se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java b/se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java
index 76154c2..2732a6b 100644
--- a/se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java
+++ b/se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java
@@ -5,6 +5,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import com.se.system.domain.vo.NacosConfigVo;
 import com.se.system.service.NacosService;
 import org.omg.CORBA.ServerRequest;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -103,6 +104,7 @@
         return toAjax(sysSoftService.deleteSysSoftBySoftIds(softIds));
     }
 
+    @Log(title = "杞欢-鑾峰彇閰嶇疆", businessType = BusinessType.OTHER)
     @RequiresPermissions("system:soft:query")
     @GetMapping(value = "getNacosConfig")
     public void getNacosConfig(String dataId, HttpServletRequest req, HttpServletResponse res) {
@@ -116,4 +118,19 @@
             }
         }
     }
+
+    @Log(title = "杞欢-鏇存柊閰嶇疆", businessType = BusinessType.UPDATE)
+    @RequiresPermissions("system:soft:edit")
+    @PostMapping(value = "updateNacosConfig")
+    public void updateNacosConfig(@RequestBody NacosConfigVo vo, HttpServletRequest req, HttpServletResponse res) {
+        try {
+            nacosService.updateNacosConfig(vo, req, res);
+        } catch (Exception e) {
+            try {
+                res.getWriter().print(e.getMessage());
+            } catch (Exception ex) {
+                logger.error(ex.getMessage(), ex);
+            }
+        }
+    }
 }
diff --git a/se-modules/se-system/src/main/java/com/se/system/domain/vo/NacosConfigVo.java b/se-modules/se-system/src/main/java/com/se/system/domain/vo/NacosConfigVo.java
new file mode 100644
index 0000000..efd2af7
--- /dev/null
+++ b/se-modules/se-system/src/main/java/com/se/system/domain/vo/NacosConfigVo.java
@@ -0,0 +1,199 @@
+package com.se.system.domain.vo;
+
+@SuppressWarnings("ALL")
+public class NacosConfigVo {
+    private String id;
+
+    private String dataId;
+
+    private String group;
+
+    private String content;
+
+    private String md5;
+
+    private String encryptedDataKey;
+
+    private String tenant;
+
+    private String appName;
+
+    private String type;
+
+    private Long createTime;
+
+    private Long modifyTime;
+
+    private String createUser;
+
+    private String createIp;
+
+    private String desc;
+
+    private String use;
+
+    private String effect;
+
+    private String schema;
+
+    private String configTags;
+
+    public NacosConfigVo() {
+    }
+
+    public void setDefault() {
+        this.appName = "";
+        this.encryptedDataKey = "";
+        this.tenant = "";
+        this.modifyTime = System.currentTimeMillis();
+        this.createIp = "127.0.0.1";
+        this.use = "";
+        this.effect = "";
+        this.schema = "";
+        this.configTags = "";
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getDataId() {
+        return dataId;
+    }
+
+    public void setDataId(String dataId) {
+        this.dataId = dataId;
+    }
+
+    public String getGroup() {
+        return group;
+    }
+
+    public void setGroup(String group) {
+        this.group = group;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public String getAppName() {
+        return appName;
+    }
+
+    public void setAppName(String appName) {
+        this.appName = appName;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+
+    public void setDesc(String desc) {
+        this.desc = desc;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    public String getMd5() {
+        return md5;
+    }
+
+    public void setMd5(String md5) {
+        this.md5 = md5;
+    }
+
+    public String getEncryptedDataKey() {
+        return encryptedDataKey;
+    }
+
+    public void setEncryptedDataKey(String encryptedDataKey) {
+        this.encryptedDataKey = encryptedDataKey;
+    }
+
+    public String getTenant() {
+        return tenant;
+    }
+
+    public void setTenant(String tenant) {
+        this.tenant = tenant;
+    }
+
+    public Long getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Long createTime) {
+        this.createTime = createTime;
+    }
+
+    public Long getModifyTime() {
+        return modifyTime;
+    }
+
+    public void setModifyTime(Long modifyTime) {
+        this.modifyTime = modifyTime;
+    }
+
+    public String getCreateUser() {
+        return createUser;
+    }
+
+    public void setCreateUser(String createUser) {
+        this.createUser = createUser;
+    }
+
+    public String getCreateIp() {
+        return createIp;
+    }
+
+    public void setCreateIp(String createIp) {
+        this.createIp = createIp;
+    }
+
+    public String getUse() {
+        return use;
+    }
+
+    public void setUse(String use) {
+        this.use = use;
+    }
+
+    public String getEffect() {
+        return effect;
+    }
+
+    public void setEffect(String effect) {
+        this.effect = effect;
+    }
+
+    public String getSchema() {
+        return schema;
+    }
+
+    public void setSchema(String schema) {
+        this.schema = schema;
+    }
+
+    public String getConfigTags() {
+        return configTags;
+    }
+
+    public void setConfigTags(String configTags) {
+        this.configTags = configTags;
+    }
+}
diff --git a/se-modules/se-system/src/main/java/com/se/system/service/NacosService.java b/se-modules/se-system/src/main/java/com/se/system/service/NacosService.java
index 0a55268..5c642e4 100644
--- a/se-modules/se-system/src/main/java/com/se/system/service/NacosService.java
+++ b/se-modules/se-system/src/main/java/com/se/system/service/NacosService.java
@@ -1,12 +1,13 @@
 package com.se.system.service;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.se.system.domain.vo.NacosConfigVo;
 import com.se.system.utils.CaffeineUtils;
+import com.se.system.utils.StringUtils;
+import nonapi.io.github.classgraph.json.JSONUtils;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.ResponseEntity;
+import org.springframework.http.*;
 import org.springframework.stereotype.Component;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
@@ -16,6 +17,8 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.PrintWriter;
+import java.lang.reflect.Field;
+import java.nio.charset.StandardCharsets;
 import java.util.Map;
 
 @Component
@@ -35,16 +38,16 @@
 
     final static String key = "nacos:login:token";
 
-    private String getToken() {
+    private String getToken() throws Exception {
         Object obj = CaffeineUtils.get(key);
         if (obj instanceof String) {
             return (String) obj;
         }
 
         String token = login();
-        if (null != token) {
-            CaffeineUtils.put(key, token);
-        }
+        if (null == token) throw new Exception("Nacos浠ょ墝涓虹┖");
+
+        CaffeineUtils.put(key, token);
 
         return token;
     }
@@ -66,9 +69,8 @@
 
     public void getNacosConfig(String dataId, HttpServletRequest req, HttpServletResponse res) throws Exception {
         String token = getToken();
-        if (null == token) throw new Exception("Nacos浠ょ墝涓虹┖");
-
-        String url = "http://" + serverAddr + "/nacos/v1/cs/configs?dataId=se-system-dev.yml&group=&appName=&pageNo=1&pageSize=10&search=accurate";
+        // String url = "http://" + serverAddr + "/nacos/v1/cs/configs?dataId=" + dataId + "&group=&appName=&pageNo=1&pageSize=10&search=accurate";
+        String url = "http://" + serverAddr + "/nacos/v1/cs/configs?dataId=" + dataId + "&group=DEFAULT_GROUP&namespaceId=&tenant=&show=all&username=";
 
         HttpHeaders headers = new HttpHeaders();
         headers.set("accessToken", token);
@@ -82,4 +84,43 @@
         out.flush();
         out.close();
     }
+
+    public void updateNacosConfig(NacosConfigVo vo, HttpServletRequest req, HttpServletResponse res) throws Exception {
+        if (null == vo || null == vo.getId() || null == vo.getDataId() || null == vo.getContent())
+            throw new Exception("NacosConfigVo鏁版嵁涓嶆纭�");
+
+        String token = getToken();
+        vo.setModifyTime(System.currentTimeMillis());
+        //vo.setMd5(StringUtils.md5(vo.getContent()));
+        String url = "http://" + serverAddr + "/nacos/v1/cs/configs?username=" + username;
+
+        HttpHeaders headers = new HttpHeaders();
+        // headers.setContentType(MediaType.APPLICATION_JSON);
+        // headers.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
+        headers.setContentType(MediaType.valueOf("application/x-www-form-urlencoded"));
+        headers.set("accessToken", token);
+        headers.set("casMd5", vo.getMd5());
+
+        MultiValueMap<String, Object> map = createMap(vo);
+        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(map, headers);
+        ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
+
+        res.setContentType("application/json;charset=UTF-8");
+        PrintWriter out = res.getWriter();
+        out.print(re.getBody());
+        out.flush();
+        out.close();
+    }
+
+    private <T> MultiValueMap<String, Object> createMap(T t) throws Exception {
+        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
+
+        Field[] fields = t.getClass().getDeclaredFields();
+        for (Field field : fields) {
+            field.setAccessible(true);
+            map.add(field.getName(), field.get(t));
+        }
+
+        return map;
+    }
 }
diff --git a/se-modules/se-system/src/main/java/com/se/system/utils/StringUtils.java b/se-modules/se-system/src/main/java/com/se/system/utils/StringUtils.java
index dfe3641..d2623d1 100644
--- a/se-modules/se-system/src/main/java/com/se/system/utils/StringUtils.java
+++ b/se-modules/se-system/src/main/java/com/se/system/utils/StringUtils.java
@@ -2,6 +2,8 @@
 
 import com.se.system.domain.StaticData;
 
+import java.math.BigInteger;
+import java.security.MessageDigest;
 import java.sql.Timestamp;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -150,4 +152,26 @@
 
         return list;
     }
+
+    public static String md5(String str) {
+        if (str == null || str.length() == 0) return null;
+
+        try {
+            MessageDigest md5 = MessageDigest.getInstance("MD5");
+            md5.update(str.getBytes());
+            byte[] byteArray = md5.digest();
+
+            BigInteger bigInt = new BigInteger(1, byteArray);
+            // 鍙傛暟16琛ㄧず16杩涘埗
+            String result = bigInt.toString(16);
+            // 涓嶈冻32浣嶉珮浣嶈ˉ闆�
+            while (result.length() < 32) {
+                result = "0" + result;
+            }
+            return result;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 }
diff --git a/se-modules/se-system/src/main/resources/bootstrap.yml b/se-modules/se-system/src/main/resources/bootstrap.yml
index 1889f9a..f566814 100644
--- a/se-modules/se-system/src/main/resources/bootstrap.yml
+++ b/se-modules/se-system/src/main/resources/bootstrap.yml
@@ -25,4 +25,6 @@
         # 鍏变韩閰嶇疆
         shared-configs:
           - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
+        # 鍏佽鍒锋柊
+        refresh-enabled: true
 enableEncrypt: true
\ No newline at end of file

--
Gitblit v1.9.3