ソースを参照

编辑新增webui代码

niujiuru 3 週間 前
コミット
abd43e6802

+ 1 - 1
web-ui.service/web/pages/page2.html

@@ -78,7 +78,7 @@
               <span style="margin:0 1px;"></span>
               <button class="btn" title="复制当前联网日志" onclick="copyLog('text4')">复制</button>
               <span style="margin:0 1px;"></span>
-              <button class="btn" title="清除当前联网日志" onclick="clearLog('text4')">清除</button>
+              <button class="btn" title="清屏当前联网日志" onclick="clearLog('text4')">清屏</button>
               <span style="margin:0 1px;"></span>
               <button class="btn" title="下载联网日志文件" onclick="downloadNetLogFile()">下载</button>
               <span style="margin:0 1px;"></span>

+ 79 - 0
web-ui.service/web/static/js/page2.js

@@ -92,6 +92,85 @@ function getNetLogLevel() {
   });
 }
 
+function downloadNetLogFile() {
+  const btn = event.currentTarget;
+  btn.disabled = true;
+
+  fetch("/api/network/logfile")
+  .then(async r => {
+    if (!r.ok) {
+      throw new Error("HTTP " + r.status);
+    }
+
+    const disposition = r.headers.get("Content-Disposition");
+    let filename = "network.log.tar.gz";
+    if (disposition) {
+      const match = disposition.match(/filename="?([^";]+)"?/);
+      if (match) {
+        filename = match[1];
+      }
+    }
+
+    return {
+      blob: await r.blob(),
+      filename
+    };
+  })
+  .then(({ blob, filename }) => {
+    const url = URL.createObjectURL(blob);
+
+    const a = document.createElement("a");
+    a.href = url;
+    a.download = filename;
+
+    document.body.appendChild(a);
+    a.click();
+    a.remove();
+
+    setTimeout(() => {
+      URL.revokeObjectURL(url);
+    }, 1000);
+
+    alert("下载成功");
+  })
+  .catch(e => {
+    console.error("download failed:", e);
+    alert("下载失败: " + e.message);
+  })
+  .finally(() => {
+    btn.disabled = false;
+    btn.textContent = "下载";
+  });
+}
+
+function restartNetService() {
+  if (!confirm("确定重启联网服务吗?")) {
+    return;
+  }
+
+  const btn = event.currentTarget;
+  btn.disabled = true;
+
+  fetch("/api/network/restart", {
+    method: "POST"
+  })
+  .then(async r => {
+    if (!r.ok) {
+      throw new Error(await r.text());
+    }
+    logLevel.value = "info";
+    netLogLevel = logLevel.value;
+    alert("重启成功");
+  })
+  .catch(e => {
+    console.error("restart failed:", e);
+    alert("重启失败: " + e.message);
+  })
+  .finally(() => {
+    btn.disabled = false;
+  });
+}
+
 function initPage2() {
   getNetInfo1();
   getNetInfo2();

+ 36 - 0
web-ui.service/web_handler.go

@@ -9,6 +9,7 @@ import (
 	"os"
 	"os/exec"
 	"strings"
+	"time"
 
 	"hnyfkj.com.cn/rtu/linux/utils/jsonrpc2"
 )
@@ -253,3 +254,38 @@ func serviceLogLevelHandler(port int) http.HandlerFunc {
 		serviceLogLevel(w, r, port)
 	}
 }
+
+func DownloadNetworkLog(w http.ResponseWriter, r *http.Request) {
+	filename := fmt.Sprintf("network.log.%s.tar.gz", time.Now().Format("20060102150405"))
+	tmpFile := "/tmp/" + filename
+
+	cmd := exec.Command("tar", "-czf", tmpFile, "-C", "/opt/yfkj/networkd.service/log", ".")
+	if out, err := cmd.CombinedOutput(); err != nil {
+		http.Error(w, string(out), http.StatusInternalServerError)
+		return
+	}
+
+	defer os.Remove(tmpFile)
+
+	w.Header().Set("Content-Type", "application/gzip")
+	w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
+
+	http.ServeFile(w, r, tmpFile)
+}
+
+func RestartNetworkService(w http.ResponseWriter, r *http.Request) {
+	if r.Method != http.MethodPost {
+		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+		return
+	}
+
+	cmd := exec.Command("systemctl", "restart", "yfkj-networkd.service")
+
+	if out, err := cmd.CombinedOutput(); err != nil {
+		http.Error(w, string(out)+err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	w.Header().Set("Content-Type", "application/json")
+	w.Write([]byte(`{"status":"ok"}`))
+}

+ 10 - 0
web-ui.service/web_route.go

@@ -47,6 +47,16 @@ var routes = []Route{
 		"/api/network/loglevel",
 		serviceLogLevelHandler(7000),
 	},
+
+	{
+		"/api/network/logfile",
+		DownloadNetworkLog,
+	},
+
+	{
+		"/api/network/restart",
+		RestartNetworkService,
+	},
 }
 
 func registerRoutes(mux *http.ServeMux) {