diff --git a/main.go b/main.go index 0e88a16..4bb0d55 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ type Task struct { InStock bool `json:"in_stock"` LastCheck time.Time `json:"last_check"` Status string `json:"status"` + Notified bool `json:"notified"` } type Config struct { @@ -32,11 +33,12 @@ type Config struct { GotifyToken string `json:"gotify_token"` Interval int `json:"interval"` Timeout int `json:"timeout"` + NotifyEnabled bool `json:"notify_enabled"` } var ( tasks = make(map[string]*Task) - config = &Config{Interval: 60, Timeout: 20} + config = &Config{Interval: 60, Timeout: 20, NotifyEnabled: true} mu sync.RWMutex configMu sync.RWMutex authToken = os.Getenv("AUTH_TOKEN") @@ -211,6 +213,9 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {

系统设置

+
@@ -286,8 +291,8 @@ func handleIndex(w http.ResponseWriter, r *http.Request) { '' + stockText + '' + '' + lastCheck + '' + '' + - '' + - '' + + '' + + '' + ''; }).join(''); }); @@ -315,6 +320,7 @@ func handleIndex(w http.ResponseWriter, r *http.Request) { document.getElementById('gotifyToken').value = cfg.gotify_token || ''; document.getElementById('interval').value = cfg.interval || 60; document.getElementById('timeout').value = cfg.timeout || 20; + document.getElementById('notifyEnabled').checked = cfg.notify_enabled !== false; document.getElementById('configModal').classList.add('show'); }); } @@ -379,7 +385,8 @@ func handleIndex(w http.ResponseWriter, r *http.Request) { gotify_url: document.getElementById('gotifyUrl').value, gotify_token: document.getElementById('gotifyToken').value, interval: parseInt(document.getElementById('interval').value), - timeout: parseInt(document.getElementById('timeout').value) + timeout: parseInt(document.getElementById('timeout').value), + notify_enabled: document.getElementById('notifyEnabled').checked }; fetch('/api/config', { method: 'POST', @@ -495,15 +502,27 @@ func checkTask(task *Task) { mu.Lock() wasInStock := task.InStock + wasNotified := task.Notified task.Status = status task.InStock = inStock task.LastCheck = now + + if !inStock { + task.Notified = false + } mu.Unlock() - saveTasks() - if inStock && !wasInStock { + configMu.RLock() + notifyEnabled := config.NotifyEnabled + configMu.RUnlock() + + if notifyEnabled && inStock && !wasInStock && !wasNotified { notify(task.Name + " 有货了!", task.URL) + mu.Lock() + task.Notified = true + mu.Unlock() + saveTasks() } }