Fix :更新通知逻辑
This commit is contained in:
31
main.go
31
main.go
@@ -25,6 +25,7 @@ type Task struct {
|
|||||||
InStock bool `json:"in_stock"`
|
InStock bool `json:"in_stock"`
|
||||||
LastCheck time.Time `json:"last_check"`
|
LastCheck time.Time `json:"last_check"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
|
Notified bool `json:"notified"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -32,11 +33,12 @@ type Config struct {
|
|||||||
GotifyToken string `json:"gotify_token"`
|
GotifyToken string `json:"gotify_token"`
|
||||||
Interval int `json:"interval"`
|
Interval int `json:"interval"`
|
||||||
Timeout int `json:"timeout"`
|
Timeout int `json:"timeout"`
|
||||||
|
NotifyEnabled bool `json:"notify_enabled"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tasks = make(map[string]*Task)
|
tasks = make(map[string]*Task)
|
||||||
config = &Config{Interval: 60, Timeout: 20}
|
config = &Config{Interval: 60, Timeout: 20, NotifyEnabled: true}
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
configMu sync.RWMutex
|
configMu sync.RWMutex
|
||||||
authToken = os.Getenv("AUTH_TOKEN")
|
authToken = os.Getenv("AUTH_TOKEN")
|
||||||
@@ -211,6 +213,9 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
|||||||
<h2>系统设置</h2>
|
<h2>系统设置</h2>
|
||||||
<form id="configForm">
|
<form id="configForm">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" id="notifyEnabled"> 启用通知推送
|
||||||
|
</label>
|
||||||
<label>Gotify URL</label>
|
<label>Gotify URL</label>
|
||||||
<input id="gotifyUrl" placeholder="https://gotify.example.com">
|
<input id="gotifyUrl" placeholder="https://gotify.example.com">
|
||||||
</div>
|
</div>
|
||||||
@@ -286,8 +291,8 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
|||||||
'<td>' + stockText + '</td>' +
|
'<td>' + stockText + '</td>' +
|
||||||
'<td>' + lastCheck + '</td>' +
|
'<td>' + lastCheck + '</td>' +
|
||||||
'<td>' +
|
'<td>' +
|
||||||
'<button class="btn" onclick="editTask(\'' + t.id + '\')">编辑</button>' +
|
'<button class="btn" onclick="editTask(\'' + t.id + '\')" title="编辑">✏️</button>' +
|
||||||
'<button class="btn btn-del" onclick="delTask(\'' + t.id + '\')">删除</button>' +
|
'<button class="btn btn-del" onclick="delTask(\'' + t.id + '\')" title="删除">🗑️</button>' +
|
||||||
'</td></tr>';
|
'</td></tr>';
|
||||||
}).join('');
|
}).join('');
|
||||||
});
|
});
|
||||||
@@ -315,6 +320,7 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
|||||||
document.getElementById('gotifyToken').value = cfg.gotify_token || '';
|
document.getElementById('gotifyToken').value = cfg.gotify_token || '';
|
||||||
document.getElementById('interval').value = cfg.interval || 60;
|
document.getElementById('interval').value = cfg.interval || 60;
|
||||||
document.getElementById('timeout').value = cfg.timeout || 20;
|
document.getElementById('timeout').value = cfg.timeout || 20;
|
||||||
|
document.getElementById('notifyEnabled').checked = cfg.notify_enabled !== false;
|
||||||
document.getElementById('configModal').classList.add('show');
|
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_url: document.getElementById('gotifyUrl').value,
|
||||||
gotify_token: document.getElementById('gotifyToken').value,
|
gotify_token: document.getElementById('gotifyToken').value,
|
||||||
interval: parseInt(document.getElementById('interval').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', {
|
fetch('/api/config', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -495,15 +502,27 @@ func checkTask(task *Task) {
|
|||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
wasInStock := task.InStock
|
wasInStock := task.InStock
|
||||||
|
wasNotified := task.Notified
|
||||||
task.Status = status
|
task.Status = status
|
||||||
task.InStock = inStock
|
task.InStock = inStock
|
||||||
task.LastCheck = now
|
task.LastCheck = now
|
||||||
|
|
||||||
|
if !inStock {
|
||||||
|
task.Notified = false
|
||||||
|
}
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
saveTasks()
|
saveTasks()
|
||||||
|
|
||||||
if inStock && !wasInStock {
|
configMu.RLock()
|
||||||
|
notifyEnabled := config.NotifyEnabled
|
||||||
|
configMu.RUnlock()
|
||||||
|
|
||||||
|
if notifyEnabled && inStock && !wasInStock && !wasNotified {
|
||||||
notify(task.Name + " 有货了!", task.URL)
|
notify(task.Name + " 有货了!", task.URL)
|
||||||
|
mu.Lock()
|
||||||
|
task.Notified = true
|
||||||
|
mu.Unlock()
|
||||||
|
saveTasks()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user