diff --git a/main.go b/main.go index 0c62091..0e88a16 100644 --- a/main.go +++ b/main.go @@ -324,8 +324,16 @@ func handleIndex(w http.ResponseWriter, r *http.Request) { } function testNotification() { - fetch('/api/test-notification', {headers}) - .then(r => r.ok ? alert('测试通知已发送,请检查 Gotify') : alert('发送失败,查看后台日志')); + const testConfig = { + gotify_url: document.getElementById('gotifyUrl').value, + gotify_token: document.getElementById('gotifyToken').value + }; + + fetch('/api/test-notification', { + method: 'POST', + headers: {...headers, 'Content-Type': 'application/json'}, + body: JSON.stringify(testConfig) + }).then(r => r.ok ? alert('测试通知已发送,请检查 Gotify') : alert('发送失败,查看后台日志')); } function editTask(id) { @@ -545,7 +553,36 @@ func notify(title, msg string) { } func handleTestNotification(w http.ResponseWriter, r *http.Request) { - notify("测试通知", "Gotify 配置正常") + var testConfig struct { + GotifyURL string `json:"gotify_url"` + GotifyToken string `json:"gotify_token"` + } + json.NewDecoder(r.Body).Decode(&testConfig) + + if testConfig.GotifyURL == "" || testConfig.GotifyToken == "" { + http.Error(w, "缺少配置", 400) + return + } + + endpoint := strings.TrimRight(testConfig.GotifyURL, "/") + "/message" + payload := `{"title":"测试通知","message":"Gotify 配置正常","priority":10}` + req, _ := http.NewRequest("POST", endpoint, strings.NewReader(payload)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Gotify-Key", testConfig.GotifyToken) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + body, _ := io.ReadAll(resp.Body) + http.Error(w, string(body), resp.StatusCode) + return + } + json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) }