feat: track duty email confirmations and add throttled account email tests
This commit is contained in:
Vendored
+39
@@ -526,6 +526,7 @@
|
||||
let dutySyncedAt = 0;
|
||||
let dutyRetryAt = 0;
|
||||
let dutyLoading = false;
|
||||
let dutyRemindersKey = "";
|
||||
const dutyDate = (ms, includeDate = false) => new Intl.DateTimeFormat("zh-CN", {
|
||||
timeZone: "Asia/Shanghai", ...(includeDate ? { month: "numeric", day: "numeric", weekday: "short" } : {}),
|
||||
hour: "2-digit", minute: "2-digit", hourCycle: "h23",
|
||||
@@ -540,6 +541,14 @@
|
||||
const shift = active ? current : next;
|
||||
const boundary = current ? current.end_at : next?.start_at;
|
||||
const expired = boundary && now >= boundary;
|
||||
const reminderKey = JSON.stringify(expired ? null : [shift?.start_at, shift?.reminders]);
|
||||
if (reminderKey !== dutyRemindersKey) {
|
||||
dutyRemindersKey = reminderKey;
|
||||
$("#duty-reminders").innerHTML = !shift || expired ? "" : (shift.reminders || []).map((r) => `
|
||||
<div class="duty-reminder-row"><div><b>${esc(r.name)}</b> <span class="tag ${r.confirmed_at ? 'attended' : 'full'}">${r.confirmed_at ? '已确认' : '未确认'}</span>
|
||||
<span class="muted">${r.last_sent_at ? `已提醒 ${dutyDate(r.last_sent_at, true)}` : '尚未成功发送'} · ${r.attempts}/3 次${r.confirmed_at ? ` · 确认于 ${dutyDate(r.confirmed_at, true)}` : ''}${r.matched ? '' : ' · 未匹配邮箱'}</span></div>
|
||||
${!r.confirmed_at ? `<button type="button" class="btn sm" data-duty-remind="${esc(r.name)}" data-start="${shift.start_at}" ${r.can_remind ? '' : 'disabled'}>${r.attempts >= 3 ? '已达3次上限' : '邮件提醒'}</button>` : ''}</div>`).join("");
|
||||
}
|
||||
$("#duty-banner").classList.toggle("on-duty", !!active);
|
||||
$("#duty-clock").classList.toggle("hidden", !shift || !!expired);
|
||||
if (expired) {
|
||||
@@ -574,6 +583,8 @@
|
||||
renderDutyStatus();
|
||||
} catch {
|
||||
dutySnapshot = null;
|
||||
dutyRemindersKey = "";
|
||||
$("#duty-reminders").innerHTML = "";
|
||||
$("#duty-banner").classList.remove("on-duty");
|
||||
$("#duty-label").textContent = "值班安排";
|
||||
$("#duty-names").textContent = "暂时无法加载";
|
||||
@@ -589,6 +600,34 @@
|
||||
else renderDutyStatus();
|
||||
}, 1000);
|
||||
document.addEventListener("visibilitychange", () => { if (!document.hidden) loadDutyStatus(); });
|
||||
$("#duty-reminders").addEventListener("click", async (event) => {
|
||||
const button = event.target.closest("[data-duty-remind]");
|
||||
if (!button || button.disabled) return;
|
||||
button.disabled = true;
|
||||
try {
|
||||
await api("/duty/remind", { method: "POST", body: { name: button.dataset.dutyRemind, start_at: Number(button.dataset.start) } });
|
||||
toast("值班提醒已发送");
|
||||
} catch (error) { toast(error.message, true); }
|
||||
finally { await loadDutyStatus(); }
|
||||
});
|
||||
|
||||
const testEmailCooldowns = new Map();
|
||||
function updateTestEmailButton() {
|
||||
const until = testEmailCooldowns.get(state.me?.email?.toLowerCase()) || 0;
|
||||
const left = Math.max(0, Math.ceil((until - Date.now()) / 1000));
|
||||
$("#pf-test-email").disabled = left > 0;
|
||||
$("#pf-test-email").textContent = left ? `${left} 秒后可再次测试` : "发送测试邮件";
|
||||
}
|
||||
$("#pf-test-email").addEventListener("click", async () => {
|
||||
testEmailCooldowns.set(state.me.email.toLowerCase(), Date.now() + 120000);
|
||||
updateTestEmailButton();
|
||||
$("#pf-test-email-status").textContent = "正在发送…";
|
||||
try {
|
||||
await api("/auth/test-email", { method: "POST" });
|
||||
$("#pf-test-email-status").textContent = "测试邮件已发送,请查看绑定邮箱及垃圾邮件文件夹。";
|
||||
} catch (error) { $("#pf-test-email-status").textContent = error.message; }
|
||||
});
|
||||
setInterval(updateTestEmailButton, 1000);
|
||||
|
||||
// ------------------------------------------------------------- 我的记录
|
||||
async function loadMine() {
|
||||
|
||||
Reference in New Issue
Block a user