This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
<view class="container">
|
||||
<view class="toolbar flex-row-sb">
|
||||
<view class="summary">共 {{ total }} 条通知</view>
|
||||
<button class="btn-apple btn-secondary mark-all" :disabled="unreadCount === 0" @tap="markAllRead">
|
||||
<button class="btn-apple btn-secondary mark-all" :disabled="unreadCount === 0 || markingAll" @tap="markAllRead">
|
||||
全部已读
|
||||
</button>
|
||||
</view>
|
||||
@@ -40,9 +40,10 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, reactive, onMounted } from 'vue';
|
||||
import { ref, reactive } from 'vue';
|
||||
import { onShow } from '@dcloudio/uni-app';
|
||||
import { notificationApi } from '@/api/index.js';
|
||||
import { getSessionContext, isCurrentSession } from '@/utils/session-context.js';
|
||||
|
||||
const pageSize = 20;
|
||||
const notifications = reactive([]);
|
||||
@@ -52,6 +53,10 @@
|
||||
const loading = ref(false);
|
||||
const notificationsLoaded = ref(false);
|
||||
const noMore = ref(false);
|
||||
const markingAll = ref(false);
|
||||
let listRequestId = 0;
|
||||
let countRequestId = 0;
|
||||
const readPending = new Set();
|
||||
|
||||
const formatDate = (value) => {
|
||||
if (!value) return '-';
|
||||
@@ -73,8 +78,11 @@
|
||||
}[severity] || '提示');
|
||||
|
||||
const loadUnreadCount = async () => {
|
||||
const context = getSessionContext();
|
||||
const requestId = ++countRequestId;
|
||||
try {
|
||||
const data = await notificationApi.getUnreadCount();
|
||||
if (!isCurrentSession(context) || requestId !== countRequestId) return;
|
||||
unreadCount.value = Number(data?.count || 0);
|
||||
} catch (error) {
|
||||
console.error('加载通知未读数失败', error);
|
||||
@@ -82,10 +90,14 @@
|
||||
};
|
||||
|
||||
const loadNotifications = async (append = false) => {
|
||||
if (loading.value || (append && noMore.value)) return;
|
||||
if (append && (loading.value || noMore.value)) return;
|
||||
const context = getSessionContext();
|
||||
const requestId = ++listRequestId;
|
||||
const requestedPage = append ? page.value : 1;
|
||||
loading.value = true;
|
||||
try {
|
||||
const data = await notificationApi.getList(page.value, pageSize);
|
||||
const data = await notificationApi.getList(requestedPage, pageSize);
|
||||
if (!isCurrentSession(context) || requestId !== listRequestId) return;
|
||||
const items = (data?.items || []);
|
||||
if (append) {
|
||||
notifications.push(...items);
|
||||
@@ -94,39 +106,48 @@
|
||||
}
|
||||
total.value = Number(data?.total || 0);
|
||||
noMore.value = notifications.length >= total.value || items.length < pageSize;
|
||||
if (!noMore.value) page.value += 1;
|
||||
page.value = requestedPage + 1;
|
||||
} catch (error) {
|
||||
console.error('加载通知列表失败', error);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
notificationsLoaded.value = true;
|
||||
if (isCurrentSession(context) && requestId === listRequestId) {
|
||||
loading.value = false;
|
||||
notificationsLoaded.value = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const markItemRead = async (item) => {
|
||||
if (item.is_read) return;
|
||||
if (item.is_read || readPending.has(item.id) || markingAll.value) return;
|
||||
const context = getSessionContext();
|
||||
readPending.add(item.id);
|
||||
try {
|
||||
await notificationApi.markRead(item.id);
|
||||
if (!isCurrentSession(context)) return;
|
||||
item.is_read = true;
|
||||
item.read_at = new Date().toISOString();
|
||||
unreadCount.value = Math.max(unreadCount.value - 1, 0);
|
||||
await loadUnreadCount();
|
||||
} catch (error) {
|
||||
console.error('标记通知已读失败', error);
|
||||
} finally {
|
||||
readPending.delete(item.id);
|
||||
}
|
||||
};
|
||||
|
||||
const markAllRead = async () => {
|
||||
if (!unreadCount.value) return;
|
||||
if (!unreadCount.value || markingAll.value) return;
|
||||
const context = getSessionContext();
|
||||
markingAll.value = true;
|
||||
try {
|
||||
await notificationApi.markAllRead();
|
||||
notifications.forEach(item => {
|
||||
item.is_read = true;
|
||||
item.read_at = item.read_at || new Date().toISOString();
|
||||
});
|
||||
unreadCount.value = 0;
|
||||
if (!isCurrentSession(context)) return;
|
||||
await Promise.all([loadNotifications(), loadUnreadCount()]);
|
||||
if (!isCurrentSession(context)) return;
|
||||
uni.showToast({ title: '已全部标记为已读', icon: 'success' });
|
||||
} catch (error) {
|
||||
console.error('全部标记通知已读失败', error);
|
||||
} finally {
|
||||
markingAll.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -134,13 +155,14 @@
|
||||
if (!noMore.value) loadNotifications(true);
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
loadNotifications();
|
||||
loadUnreadCount();
|
||||
});
|
||||
|
||||
onShow(() => {
|
||||
loadUnreadCount();
|
||||
onShow(async () => {
|
||||
notifications.splice(0);
|
||||
total.value = 0;
|
||||
unreadCount.value = 0;
|
||||
page.value = 1;
|
||||
noMore.value = false;
|
||||
notificationsLoaded.value = false;
|
||||
await Promise.all([loadNotifications(), loadUnreadCount()]);
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user