133 lines
3.7 KiB
JavaScript
133 lines
3.7 KiB
JavaScript
(function (root, factory) {
|
|
if (typeof module === 'object' && module.exports) {
|
|
module.exports = factory(root);
|
|
return;
|
|
}
|
|
|
|
root.NotificationPages = factory(root);
|
|
if (root.document) {
|
|
root.document.addEventListener('DOMContentLoaded', function () {
|
|
root.NotificationPages.init();
|
|
});
|
|
}
|
|
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
|
|
'use strict';
|
|
|
|
var documentRef = root.document;
|
|
|
|
function getApi() {
|
|
return root.GenealogyApi && root.GenealogyApi.defaultClient;
|
|
}
|
|
|
|
function normalizeNotifications(data) {
|
|
return Array.isArray(data) ? data : [];
|
|
}
|
|
|
|
function formatNotification(data) {
|
|
return JSON.stringify(data || {}, null, 2);
|
|
}
|
|
|
|
function getUnreadCount(data) {
|
|
var count = Number(data);
|
|
|
|
return Number.isFinite(count) && count >= 0 ? count : 0;
|
|
}
|
|
|
|
function query(selector, rootNode) {
|
|
return (rootNode || documentRef).querySelector(selector);
|
|
}
|
|
|
|
function showMessage(message) {
|
|
if (root.layui && root.layui.layer) {
|
|
root.layui.layer.msg(message);
|
|
return;
|
|
}
|
|
|
|
if (root.alert) root.alert(message);
|
|
}
|
|
|
|
function renderNotifications(container, notifications) {
|
|
if (!container) return;
|
|
container.innerHTML = '';
|
|
|
|
if (!notifications.length) {
|
|
container.textContent = '暂无通知';
|
|
return;
|
|
}
|
|
|
|
notifications.forEach(function (notification) {
|
|
var row = documentRef.createElement('pre');
|
|
|
|
row.className = 'notification-raw';
|
|
row.textContent = formatNotification(notification);
|
|
container.appendChild(row);
|
|
});
|
|
}
|
|
|
|
function updateStatus(message) {
|
|
var status = query('[data-notification-status]');
|
|
|
|
if (status) status.textContent = message;
|
|
}
|
|
|
|
async function loadNotifications() {
|
|
var api = getApi();
|
|
var container = query('[data-notification-list]');
|
|
var countTarget = query('[data-notification-unread-count]');
|
|
var result;
|
|
|
|
if (!api || !api.notifications || !api.unreadNotificationCount) {
|
|
updateStatus('消息接口初始化失败,请刷新后重试');
|
|
return;
|
|
}
|
|
|
|
updateStatus('正在同步消息…');
|
|
try {
|
|
result = await Promise.all([api.notifications(), api.unreadNotificationCount()]);
|
|
renderNotifications(container, normalizeNotifications(result[0]));
|
|
if (countTarget) countTarget.textContent = String(getUnreadCount(result[1]));
|
|
updateStatus('已同步消息。列表元素 DTO 尚未在 PC 契约展开,以下保留服务端原始记录。');
|
|
} catch (error) {
|
|
updateStatus(error.message || '消息同步失败');
|
|
}
|
|
}
|
|
|
|
async function markAllRead(button) {
|
|
var api = getApi();
|
|
|
|
if (!api || !api.markAllNotificationsRead) {
|
|
updateStatus('消息接口初始化失败,请刷新后重试');
|
|
return;
|
|
}
|
|
|
|
button.disabled = true;
|
|
try {
|
|
await api.markAllNotificationsRead();
|
|
showMessage('已全部标记为已读');
|
|
await loadNotifications();
|
|
} catch (error) {
|
|
updateStatus(error.message || '全部标记已读失败');
|
|
} finally {
|
|
button.disabled = false;
|
|
}
|
|
}
|
|
|
|
function init() {
|
|
var readAll = query('[data-notification-read-all]');
|
|
var refresh = query('[data-notification-refresh]');
|
|
|
|
if (!documentRef || !query('[data-notification-list]')) return;
|
|
if (readAll) readAll.addEventListener('click', function () { markAllRead(readAll); });
|
|
if (refresh) refresh.addEventListener('click', loadNotifications);
|
|
loadNotifications();
|
|
}
|
|
|
|
return {
|
|
normalizeNotifications: normalizeNotifications,
|
|
formatNotification: formatNotification,
|
|
getUnreadCount: getUnreadCount,
|
|
loadNotifications: loadNotifications,
|
|
init: init
|
|
};
|
|
});
|