Explorar el Código

优化web-ui.service,新增查看升级服务配置的接口,版本升级到v0.0.1.0

niujiuru hace 1 día
padre
commit
541e8c0543

+ 1 - 1
web-ui.service/main.go

@@ -12,7 +12,7 @@ import (
 )
 
 var (
-	Version   = "0.0.0.6"
+	Version   = "0.0.1.0"
 	BuildTime = ""
 )
 

+ 13 - 1
web-ui.service/page1_handler.go

@@ -515,7 +515,7 @@ func serviceVersionSwitchHandler(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	if req.Password != "yfkj123456" {
+	if req.Password != authPassword {
 		json.NewEncoder(w).Encode(VersionResp{Status: "error", Message: "输入的密码不正确"})
 		return
 	}
@@ -670,3 +670,15 @@ func downloadUpgradeLog(w http.ResponseWriter, r *http.Request) {
 
 	http.ServeFile(w, r, tmpFile)
 }
+
+func getUpgradeConfig(w http.ResponseWriter, r *http.Request) {
+	switch r.Method {
+	case http.MethodGet:
+		if !checkOperationPassword(w, r) {
+			return
+		}
+		callRPCResponse(w, r, 7005, "core.getUpgradeConf", nil)
+	default:
+		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+	}
+}

+ 20 - 1
web-ui.service/web/app.html

@@ -18,6 +18,7 @@
 <link rel="stylesheet" href="static/css/page6.css?v={{V}}">
 <link rel="stylesheet" href="static/css/page7.css?v={{V}}">
 <link rel="stylesheet" href="static/css/page8.css?v={{V}}">
+<link rel="stylesheet" href="static/css/jsondlg.css?v={{V}}">
 </head>
 
 <body>
@@ -40,6 +41,8 @@
   <symbol id="i-disk" viewBox="0 0 24 24"><line x1="22" y1="12" x2="2" y2="12"/><path d="M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"/><line x1="6" y1="16" x2="6.01" y2="16"/><line x1="10" y1="16" x2="10.01" y2="16"/></symbol>
   <symbol id="i-trend" viewBox="0 0 24 24"><polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/><polyline points="17 6 23 6 23 12"/></symbol>
   <symbol id="i-refresh" viewBox="0 0 24 24"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></symbol>
+  <symbol id="i-unlock" viewBox="0 0 24 24"><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 9.9-1"/></symbol>
+  <symbol id="i-json-file" viewBox="0 0 24 24"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><path d="M14 2v6h6"/><path d="M11 11c-2 0-2 1-2 2v1c0 1-1 1-2 1 1 0 2 0 2 1v1c0 1 0 2 2 2M13 11c2 0 2 1 2 2v1c0 1 1 1 2 1-1 0-2 0-2 1v1c0 1 0 2-2 2"/></symbol>
 </svg>
 
 <div id="loading-mask">
@@ -48,8 +51,9 @@
     <div class="loading-text">页面加载中,请稍微...</div>
   </div>
 </div>
+
 <div class="app">
-   <header class="topbar">
+  <header class="topbar">
     <div class="device">
       <span class="icon-box"><svg class="icon"><use href="#i-logo"/></svg></span>
       <div class="device-info">
@@ -93,12 +97,27 @@
     </main>
   </div>
 </div>
+
+<!-- JSON查看弹窗 -->
+<div class="jsondlg" id="jsondlg">
+  <div class="jsondlg-dialog">
+    <div class="jsondlg-header">
+      <h2 id="jsondlg-title">配置查看</h2>
+      <button class="jsondlg-close" type="button" title="关闭" onclick="closeJsonDlg()">&times;</button>
+    </div>
+    <div class="jsondlg-body">
+      <pre id="jsondlg-content"></pre>
+    </div>
+  </div>
+</div>
+
 <!-- 兼容层:顺序不可调换(fetch 依赖 Promise),且须在业务 JS 之前 -->
 <script src="static/js/promise.min.js?v={{V}}"></script>
 <script src="static/js/fetch.min.js?v={{V}}"></script>
 <script src="static/js/eventsource.min.js?v={{V}}"></script>
 <script src="static/js/compat.js?v={{V}}"></script>
 <script src="static/js/util.js?v={{V}}"></script>
+<script src="static/js/jsondlg.js?v={{V}}"></script>
 <script src="static/js/echarts.min.js?v={{V}}"></script>
 <script src="static/js/xterm.js?v={{V}}"></script>
 <script src="static/js/xterm-addon-fit.js?v={{V}}"></script>

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

@@ -176,7 +176,7 @@
 
         <div class="info-item">
           <div class="info-info">
-            <div class="info-name">设备自动升级</div>
+            <div class="info-name clickable" onclick="viewUpgradeConfig()">设备自动升级</div>
             <div class="info-desc">upgrade.service</div>
           </div>
           <span class="info-value" id="var9">--</span>

+ 76 - 0
web-ui.service/web/static/css/jsondlg.css

@@ -0,0 +1,76 @@
+/* JSON查看弹窗 */
+.jsondlg {
+    display: none;
+    position: fixed;
+    inset: 0;
+    z-index: 1000;
+    padding: 20px;
+    background: rgba(0, 0, 0, 0.65);
+    align-items: center;
+    justify-content: center;
+}
+
+.jsondlg.show {
+    display: flex;
+}
+
+.jsondlg-dialog {
+    display: flex;
+    flex-direction: column;
+    width: 100%;
+    max-width: 760px;
+    max-height: 85vh;
+    background: #17243a;
+    border: 1px solid #30435f;
+    border-radius: 10px;
+    overflow: hidden;
+}
+
+.jsondlg-header {
+    display: flex;
+    align-items: center;
+    justify-content: space-between;
+    flex: none;
+    padding: 14px 18px;
+    border-bottom: 1px solid #30435f;
+}
+
+.jsondlg-header h2 {
+    min-width: 0;
+    font-size: 16px;
+    font-weight: 500;
+    color: #e2eaf5;
+    overflow-wrap: anywhere;
+}
+
+.jsondlg-close {
+    flex: none;
+    margin-left: 16px;
+    padding: 0;
+    border: none;
+    background: none;
+    color: #9aacc5;
+    font-size: 26px;
+    line-height: 1;
+    cursor: pointer;
+}
+
+.jsondlg-close:hover {
+    color: #ffffff;
+}
+
+.jsondlg-body {
+    flex: 1;
+    min-height: 0;
+    overflow: auto;
+    padding: 16px;
+}
+
+.jsondlg-body pre {
+    margin: 0;
+    color: #b9d5f5;
+    font-family: Consolas, "Courier New", monospace;
+    font-size: 13px;
+    line-height: 1.7;
+    white-space: pre;
+}

+ 4 - 0
web-ui.service/web/static/css/page1.css

@@ -71,6 +71,10 @@
     font-size: 14px;
 }
 
+.page1 .info-name.clickable {
+    cursor: pointer;
+}
+
 .page1 .info-desc {
     color: #64748b;
     font-size: 12px;

+ 29 - 0
web-ui.service/web/static/js/jsondlg.js

@@ -0,0 +1,29 @@
+function showJsonDlg(title, data) {
+    var dlg = document.getElementById("jsondlg");
+    var content = document.getElementById("jsondlg-content");
+
+    if (data === undefined || data === null) {
+        content.textContent = "暂无内容";
+    } else if (typeof data === "string") {
+        try {
+            content.textContent = JSON.stringify(JSON.parse(data), null, 2);
+        } catch (e) {
+            content.textContent = data;
+        }
+    } else {
+        try {
+            content.textContent = JSON.stringify(data, null, 2);
+        } catch (e) {
+            content.textContent = String(data);
+        }
+    }
+
+    document.getElementById("jsondlg-title").textContent = title || "配置查看";
+
+    dlg.classList.add("show");
+    content.parentNode.scrollTop = 0;
+}
+
+function closeJsonDlg() {
+    document.getElementById("jsondlg").classList.remove("show");
+}

+ 29 - 0
web-ui.service/web/static/js/page1.js

@@ -669,3 +669,32 @@ function dashSetUptime(text) {
         el.textContent = text || "--";
     }
 }
+
+function viewUpgradeConfig() {
+    var password = prompt("请输入操作密码:");
+    if (password === null) {
+        return;
+    }
+
+    fetch("/api/upgrade/getUpgradeConfig", {
+        headers: {
+            "X-Operation-Password": password
+        }
+    })
+        .then(function (r) {
+            if (!r.ok) {
+                if (r.status === 401) {
+                    throw new Error("操作密码错误");
+                }
+                throw new Error("HTTP " + r.status);
+            }
+            return r.json();
+        })
+        .then(function (data) {
+            showJsonDlg("设备自动升级配置", data);
+        })
+        .catch(function (e) {
+            console.error("viewUpgradeConfig() failed:", e);
+            alert("获取设备自动升级配置失败:" + e.message);
+        });
+}

+ 12 - 1
web-ui.service/web_handler.go

@@ -3,6 +3,7 @@ package main
 import (
 	"bufio"
 	"context"
+	"crypto/subtle"
 	"encoding/json"
 	"fmt"
 	"io/fs"
@@ -19,6 +20,7 @@ import (
 )
 
 const noValue = "--"
+const authPassword = "yfkj123456"
 
 type LoginReq struct {
 	Username string `json:"username"`
@@ -484,7 +486,7 @@ func serviceControlHandler(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	if req.Secret != "yfkj123456" {
+	if req.Secret != authPassword {
 		http.Error(w, "密码错误", http.StatusForbidden)
 		return
 	}
@@ -505,3 +507,12 @@ func serviceControlHandler(w http.ResponseWriter, r *http.Request) {
 
 	json.NewEncoder(w).Encode(map[string]bool{"ok": true})
 }
+
+func checkOperationPassword(w http.ResponseWriter, r *http.Request) bool {
+	password := r.Header.Get("X-Operation-Password")
+	if subtle.ConstantTimeCompare([]byte(password), []byte(authPassword)) != 1 {
+		http.Error(w, "操作密码错误", http.StatusUnauthorized)
+		return false
+	}
+	return true
+}

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

@@ -71,6 +71,11 @@ var routes = []Route{
 	},
 
 	{
+		"/api/upgrade/getUpgradeConfig",
+		getUpgradeConfig,
+	},
+
+	{
 		"/api/network/interfaces",
 		netInterfaces,
 	},