Files
Letters/js/notice-index.js
T
biss b3a2199f28
Vercel Deploy / deploy (push) Successful in 2m8s
优化结构
2026-05-01 08:29:46 +08:00

71 lines
2.5 KiB
JavaScript

(function () {
fetch("../notices.json?v=" + Math.floor(Date.now() / 300000), { cache: "no-store" })
.then(function (response) {
if (!response.ok) {
throw new Error("notice source unavailable");
}
return response.json();
})
.then(function (payload) {
var notices = Array.isArray(payload) ? payload : payload.notices;
var notice = Array.isArray(notices) && notices.find(function (item) {
return item && item.enabled !== false;
});
if (notice) {
renderNotice(notice);
}
})
.catch(function () {});
function renderNotice(notice) {
setText("noticeTitle", notice.title || "重要通知");
setText("noticeSummary", notice.message || "");
setText("noticeBadge", notice.badge || "通知");
setText("noticeLevel", String(notice.level || "info").toUpperCase());
setText("articleTitle", notice.title || "重要通知");
setText("articleMessage", notice.message || "");
setText("noticeDate", formatDate(notice.startsAt) || "未设置开始时间");
setText("noticeWindow", formatWindow(notice.startsAt, notice.endsAt));
}
function setText(id, text) {
var node = document.getElementById(id);
if (node) {
node.textContent = text;
}
}
function formatDate(value) {
if (!value) {
return "";
}
var date = new Date(value);
if (Number.isNaN(date.getTime())) {
return "";
}
return date.toLocaleDateString("zh-CN", {
year: "numeric",
month: "2-digit",
day: "2-digit"
});
}
function formatWindow(start, end) {
var startText = formatDate(start);
var endText = formatDate(end);
if (startText && endText) {
return startText + " 至 " + endText;
}
if (startText) {
return startText + " 起生效";
}
return "长期有效";
}
})();