From d40b977e906d723f686468bb2159d462d2188585 Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Thu, 7 May 2026 14:20:51 +0800 Subject: [PATCH] fix: proxy preview channels through backend to avoid CORS --- internal/web/ui.go | 22 ++++++++++++++++++++++ internal/web/ui/templates/monitor.html | 7 +++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/web/ui.go b/internal/web/ui.go index 702997e..8103146 100644 --- a/internal/web/ui.go +++ b/internal/web/ui.go @@ -662,6 +662,7 @@ func (u *UI) Routes() (chi.Router, error) { r.Get("/diagnostics", u.pageDiagnostics) r.Get("/alarms", u.pageAlarms) r.Get("/monitor", u.pageMonitor) + r.Get("/api/monitor/channels", u.apiMonitorChannels) r.Get("/recognition", u.pageRecognition) r.Get("/logs", u.pageLogs) @@ -3869,3 +3870,24 @@ func (u *UI) pageMonitor(w http.ResponseWriter, r *http.Request) { u.ensureDevicesLoaded() u.render(w, r, "monitor", PageData{Title: "视频监控", Devices: u.registry.GetDevices()}) } + +func (u *UI) apiMonitorChannels(w http.ResponseWriter, r *http.Request) { + deviceID := r.URL.Query().Get("device_id") + if deviceID == "" { + http.Error(w, "missing device_id", http.StatusBadRequest) + return + } + dev, ok := u.findDevice(deviceID) + if !ok { + http.Error(w, "device not found", http.StatusNotFound) + return + } + body, code, err := u.agent.Do("GET", dev.IP, dev.AgentPort, "/v1/preview/channels", nil) + if err != nil { + http.Error(w, err.Error(), http.StatusBadGateway) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + w.Write(body) +} diff --git a/internal/web/ui/templates/monitor.html b/internal/web/ui/templates/monitor.html index 40b7c3d..250f9bd 100644 --- a/internal/web/ui/templates/monitor.html +++ b/internal/web/ui/templates/monitor.html @@ -19,10 +19,9 @@ (function() { var grid = document.getElementById("channels-grid"); - function loadChannels(deviceId, deviceIP, agentPort) { - var url = "http://" + deviceIP + ":" + agentPort + "/v1/preview/channels"; + function loadChannels(deviceId) { grid.innerHTML = '
加载通道中…
'; - fetch("http://" + deviceIP + ":" + agentPort + "/v1/preview/channels") + fetch('/ui/api/monitor/channels?device_id=' + encodeURIComponent(deviceId)) .then(function(r) { return r.json(); }) .then(function(data) { var chs = data.channels || []; @@ -58,7 +57,7 @@ var html = ''; {{range .Devices}} {{if .Online}} - html += ''; + html += ''; {{end}} {{end}} html += '
设备地址操作
{{.DisplayName}}{{.IP}}:{{.AgentPort}}
{{.DisplayName}}{{.IP}}:{{.AgentPort}}
';