chore: establish V1.1 recovery baseline
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* 通知中心页 U25
|
||||
*/
|
||||
const api = require('../../utils/api');
|
||||
|
||||
Page({
|
||||
data: {
|
||||
notifications: [],
|
||||
unreadCount: 0,
|
||||
loading: true,
|
||||
page: 1,
|
||||
hasMore: true
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.loadNotifications();
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.loadUnreadCount();
|
||||
},
|
||||
|
||||
async loadNotifications() {
|
||||
this.setData({ loading: true });
|
||||
try {
|
||||
const token = wx.getStorageSync('accessToken');
|
||||
const res = await api.get(
|
||||
`/notifications?page=${this.data.page}&pageSize=20`,
|
||||
{},
|
||||
{ Authorization: `Bearer ${token}` }
|
||||
);
|
||||
if (res) {
|
||||
const isNewPage = this.data.page === 1;
|
||||
this.setData({
|
||||
notifications: isNewPage ? res.notifications : [...this.data.notifications, ...res.notifications],
|
||||
unreadCount: res.unreadCount,
|
||||
hasMore: res.notifications.length >= 20,
|
||||
loading: false
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
this.setData({ loading: false });
|
||||
}
|
||||
},
|
||||
|
||||
async loadUnreadCount() {
|
||||
try {
|
||||
const token = wx.getStorageSync('accessToken');
|
||||
const res = await api.get('/notifications/unread-count', {}, { Authorization: `Bearer ${token}` });
|
||||
this.setData({ unreadCount: res?.unreadCount || 0 });
|
||||
} catch (e) { /* ignore */ }
|
||||
},
|
||||
|
||||
async onMarkRead(e) {
|
||||
const notificationId = e.currentTarget.dataset.id;
|
||||
try {
|
||||
const token = wx.getStorageSync('accessToken');
|
||||
await api.put(`/notifications/${notificationId}/read`, {}, { Authorization: `Bearer ${token}` });
|
||||
this.setData({
|
||||
notifications: this.data.notifications.map(n =>
|
||||
n.notificationId === notificationId ? { ...n, read: true } : n
|
||||
),
|
||||
unreadCount: Math.max(0, this.data.unreadCount - 1)
|
||||
});
|
||||
} catch (err) { /* ignore */ }
|
||||
},
|
||||
|
||||
async onMarkAllRead() {
|
||||
try {
|
||||
const token = wx.getStorageSync('accessToken');
|
||||
await api.put('/notifications/read-all', {}, { Authorization: `Bearer ${token}` });
|
||||
this.setData({ unreadCount: 0 });
|
||||
} catch (err) { /* ignore */ }
|
||||
},
|
||||
|
||||
async onReachBottom() {
|
||||
if (this.data.hasMore && !this.data.loading) {
|
||||
this.setData({ page: this.data.page + 1 });
|
||||
this.loadNotifications();
|
||||
}
|
||||
},
|
||||
|
||||
onPullDownRefresh() {
|
||||
this.setData({ page: 1 });
|
||||
this.loadNotifications().then(() => wx.stopPullDownRefresh());
|
||||
},
|
||||
|
||||
formatTime(ts) {
|
||||
if (!ts) return '';
|
||||
const d = new Date(ts);
|
||||
const now = new Date();
|
||||
if (d.toDateString() === now.toDateString()) {
|
||||
return d.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
|
||||
}
|
||||
return `${d.getMonth() + 1}/${d.getDate()} ${d.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })}`;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user