等待后端更新接口
This commit is contained in:
@@ -178,10 +178,23 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
function updateStatus(message) {
|
||||
function isForbidden(error) {
|
||||
return Number(error && (error.status || error.code)) === 403;
|
||||
}
|
||||
|
||||
function setNotificationState(target, type, message) {
|
||||
if (!target) return;
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState && type) {
|
||||
root.ProfileUI.setApiState(target, type, message);
|
||||
return;
|
||||
}
|
||||
target.textContent = message || '';
|
||||
}
|
||||
|
||||
function updateStatus(type, message) {
|
||||
var status = query('[data-notification-status]');
|
||||
|
||||
if (status) status.textContent = message || '';
|
||||
setNotificationState(status, type, message);
|
||||
}
|
||||
|
||||
function setWritePending(value) {
|
||||
@@ -203,21 +216,27 @@
|
||||
});
|
||||
if (!container) return notifications;
|
||||
if (!Array.isArray(data) || invalidCount) {
|
||||
container.innerHTML = '<div class="api-empty">通知列表响应缺少稳定 NotificationView 字段,请联系后端核对。</div>';
|
||||
setNotificationState(container, 'error', '通知列表响应无效,请稍后重试。');
|
||||
return [];
|
||||
}
|
||||
if (!notifications.length) {
|
||||
container.innerHTML = '<div class="api-empty">暂无通知</div>';
|
||||
setNotificationState(container, 'empty', '当前暂无通知。');
|
||||
return [];
|
||||
}
|
||||
container.innerHTML = notifications.map(renderNotificationRow).join('');
|
||||
if (writePending) setWritePending(true);
|
||||
return notifications;
|
||||
}
|
||||
|
||||
function renderDetail(notification) {
|
||||
var container = query('[data-notification-detail]');
|
||||
|
||||
if (container) container.innerHTML = renderNotificationDetail(notification);
|
||||
if (!container) return;
|
||||
if (!notification) {
|
||||
setNotificationState(container, 'empty', '请选择一条通知查看详情。');
|
||||
return;
|
||||
}
|
||||
container.innerHTML = renderNotificationDetail(notification);
|
||||
}
|
||||
|
||||
async function loadNotificationDetail(notificationId) {
|
||||
@@ -225,6 +244,7 @@
|
||||
var notification;
|
||||
|
||||
if (!normalizeId(notificationId) || redirectUnauthorized(api)) return null;
|
||||
setNotificationState(query('[data-notification-detail]'), 'loading', '正在读取通知详情…');
|
||||
try {
|
||||
notification = normalizeNotification(await api.notificationDetail(notificationId));
|
||||
if (!notification || !matchesNotification(notification, notificationId)) {
|
||||
@@ -236,8 +256,8 @@
|
||||
return notification;
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return null;
|
||||
renderDetail(null);
|
||||
updateStatus(error.message || '通知详情加载失败');
|
||||
setNotificationState(query('[data-notification-detail]'), isForbidden(error) ? 'forbidden' : 'error', isForbidden(error) ? '暂无权限查看该通知详情。' : '通知详情加载失败,请稍后重试。');
|
||||
updateStatus(isForbidden(error) ? 'forbidden' : 'error', isForbidden(error) ? '暂无权限查看该通知详情。' : (error.message || '通知详情加载失败,请稍后重试。'));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -257,10 +277,12 @@
|
||||
|
||||
if (redirectUnauthorized(api)) return;
|
||||
if (!api.notifications || !api.notificationDetail || !api.unreadNotificationCount) {
|
||||
updateStatus('消息接口初始化失败,请刷新后重试');
|
||||
setNotificationState(query('[data-notification-list]'), 'error', '消息服务初始化失败,请刷新后重试。');
|
||||
updateStatus('error', '消息服务初始化失败,请刷新后重试。');
|
||||
return;
|
||||
}
|
||||
updateStatus('正在同步消息…');
|
||||
setNotificationState(query('[data-notification-list]'), 'loading', '正在同步消息…');
|
||||
updateStatus('loading', '正在同步消息…');
|
||||
try {
|
||||
result = await Promise.all([
|
||||
api.notifications(buildNotificationQuery(currentFilter())),
|
||||
@@ -273,12 +295,12 @@
|
||||
: (notifications[0] && notifications[0].notificationId);
|
||||
if (nextSelectedId) await loadNotificationDetail(nextSelectedId);
|
||||
else renderDetail(null);
|
||||
updateStatus('消息已同步');
|
||||
updateStatus('', '消息已同步');
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return;
|
||||
renderNotifications(null);
|
||||
renderDetail(null);
|
||||
updateStatus(error.message || '消息同步失败');
|
||||
setNotificationState(query('[data-notification-detail]'), isForbidden(error) ? 'forbidden' : 'error', isForbidden(error) ? '暂无权限查看通知详情。' : '消息同步失败,请稍后重试。');
|
||||
updateStatus(isForbidden(error) ? 'forbidden' : 'error', isForbidden(error) ? '暂无权限查看通知列表。' : (error.message || '消息同步失败,请稍后重试。'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,6 +310,7 @@
|
||||
|
||||
if (writePending || !normalizeId(notificationId) || !canMarkRead(notification) || redirectUnauthorized(api)) return;
|
||||
setWritePending(true);
|
||||
updateStatus('loading', '正在标记通知为已读…');
|
||||
try {
|
||||
await api.markNotificationRead(notificationId);
|
||||
selectedNotificationId = notificationId;
|
||||
@@ -295,7 +318,7 @@
|
||||
await loadNotifications();
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return;
|
||||
updateStatus(error.message || '标记通知已读失败');
|
||||
updateStatus(isForbidden(error) ? 'forbidden' : 'error', isForbidden(error) ? '暂无权限标记该通知为已读。' : (error.message || '标记通知已读失败,请稍后重试。'));
|
||||
} finally {
|
||||
setWritePending(false);
|
||||
}
|
||||
@@ -306,17 +329,18 @@
|
||||
|
||||
if (writePending || redirectUnauthorized(api)) return;
|
||||
if (!api.markAllNotificationsRead) {
|
||||
updateStatus('消息接口初始化失败,请刷新后重试');
|
||||
updateStatus('error', '消息服务初始化失败,请刷新后重试。');
|
||||
return;
|
||||
}
|
||||
setWritePending(true);
|
||||
updateStatus('loading', '正在将全部通知标记为已读…');
|
||||
try {
|
||||
await api.markAllNotificationsRead();
|
||||
showMessage('已全部标记为已读');
|
||||
await loadNotifications();
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return;
|
||||
updateStatus(error.message || '全部标记已读失败');
|
||||
updateStatus(isForbidden(error) ? 'forbidden' : 'error', isForbidden(error) ? '暂无权限标记全部通知为已读。' : (error.message || '全部标记已读失败,请稍后重试。'));
|
||||
} finally {
|
||||
setWritePending(false);
|
||||
}
|
||||
@@ -368,6 +392,7 @@
|
||||
renderNotificationRow: renderNotificationRow,
|
||||
renderNotificationDetail: renderNotificationDetail,
|
||||
shouldRedirectToLogin: shouldRedirectToLogin,
|
||||
isForbidden: isForbidden,
|
||||
loadNotifications: loadNotifications,
|
||||
init: init
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user