Fix :更新通知逻辑

This commit is contained in:
XOF
2025-11-29 03:33:46 +08:00
parent 2496006170
commit f4b1f88360

31
main.go
View File

@@ -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) {
<h2>系统设置</h2>
<form id="configForm">
<div class="form-group">
<label>
<input type="checkbox" id="notifyEnabled"> 启用通知推送
</label>
<label>Gotify URL</label>
<input id="gotifyUrl" placeholder="https://gotify.example.com">
</div>
@@ -286,8 +291,8 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
'<td>' + stockText + '</td>' +
'<td>' + lastCheck + '</td>' +
'<td>' +
'<button class="btn" onclick="editTask(\'' + t.id + '\')">编辑</button>' +
'<button class="btn btn-del" onclick="delTask(\'' + t.id + '\')">删除</button>' +
'<button class="btn" onclick="editTask(\'' + t.id + '\')" title="编辑">✏️</button>' +
'<button class="btn btn-del" onclick="delTask(\'' + t.id + '\')" title="删除">🗑️</button>' +
'</td></tr>';
}).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()
}
}