const app = getApp() // 公告列表项 interface IAnnouncementItem { id: number title: string content: string displayPosition: number priority: number redDot: boolean jumpType: number jumpTarget: string buttonText: string startAt: string endAt: string createdAt: string isRead: boolean } // 通知列表项 interface INoticeItem { id: number taskId: number title: string subtitle: string subType: string subTypeName: string coverImage: string jumpType: number jumpTarget: string buttonText: string isRead: boolean scheduleAt: string } interface IPagination { page: number pageSize: number pages: number count: number } Page({ data: { active: 0, imageUrl: app.globalData.imageUrl, Timestamp: app.globalData.Timestamp, // 公告 announcementList: [] as IAnnouncementItem[], announcementPagination: { page: 1, pageSize: 20, pages: 0, count: 0 } as IPagination, announcementLoading: false, // 重要通知 importantList: [] as INoticeItem[], importantPagination: { page: 1, pageSize: 20, pages: 0, count: 0 } as IPagination, importantLoading: false, // 活动通知 activityList: [] as INoticeItem[], activityPagination: { page: 1, pageSize: 20, pages: 0, count: 0 } as IPagination, activityLoading: false, }, onLoad() { app.waitLogin({ type: 1 }).then(() => { this.clearNotice('announcement') this.fetchAnnouncementList() }) }, onShow() { // 刷新 tabbar 红点 const tabBar = this.getTabBar() if (tabBar && typeof tabBar.fetchUnreadCount === 'function') { tabBar.fetchUnreadCount() } }, // 自动清空当前tab类型的通知(进入列表时即清空) async clearNotice(type: string) { try { await wx.ajax({ url: '/notification/mark-read-by-type', method: 'POST', data: { type }, }) // 刷新 tabbar 红点 const tabBar = this.getTabBar() if (tabBar && typeof tabBar.fetchUnreadCount === 'function') { tabBar.fetchUnreadCount() } } catch (err) { console.error('清空通知失败:', err) } }, // 切换 tab onChange(e: WechatMiniprogram.CustomEvent) { const active = e.detail.index ?? 0 this.setData({ active }) if (active === 0 && this.data.announcementList.length === 0) { this.clearNotice('announcement') this.fetchAnnouncementList() } else if (active === 1 && this.data.importantList.length === 0) { this.clearNotice('important') this.fetchNoticeList('important') } else if (active === 2 && this.data.activityList.length === 0) { this.clearNotice('activity') this.fetchNoticeList('activity') } }, // 获取公告列表(专用接口) async fetchAnnouncementList(isRefresh = false) { if (this.data.announcementLoading) return const { announcementPagination } = this.data const page = isRefresh ? 1 : announcementPagination.page this.setData({ announcementLoading: true }) try { const res = await wx.ajax({ url: '/notification/announcement-list', method: 'GET', data: { page, pageSize: announcementPagination.pageSize }, }) if (res) { const newList = isRefresh ? res.list : [...this.data.announcementList, ...res.list] this.setData({ announcementList: newList, announcementPagination: { page: res.pagination?.page || page, pageSize: res.pagination?.pageSize || announcementPagination.pageSize, pages: res.pagination?.totalPages || 0, count: res.pagination?.total || 0, }, }) } } catch (err) { console.error('获取公告列表失败:', err) } finally { this.setData({ announcementLoading: false }) } }, // 获取通知列表(重要通知/活动通知) async fetchNoticeList(type: 'important' | 'activity', isRefresh = false) { const loadingKey = `${type}Loading` as const const listKey = `${type}List` as const const paginationKey = `${type}Pagination` as const if (this.data[loadingKey]) return const pagination = this.data[paginationKey] const page = isRefresh ? 1 : pagination.page this.setData({ [loadingKey]: true } as any) try { const res = await wx.ajax({ url: '/notification/list', method: 'GET', data: { type, page, pageSize: pagination.pageSize }, }) if (res) { const newList = isRefresh ? res.list : [...this.data[listKey], ...res.list] this.setData({ [listKey]: newList, [paginationKey]: { page: res.pagination?.page || page, pageSize: res.pagination?.pageSize || pagination.pageSize, pages: res.pagination?.totalPages || 0, count: res.pagination?.total || 0, }, } as any) } } catch (err) { console.error('获取通知列表失败:', err) } finally { this.setData({ [loadingKey]: false } as any) } }, // 查看公告详情 handleAnnouncementDetail(e: WechatMiniprogram.TouchEvent) { const { id } = e.currentTarget.dataset wx.navigateTo({ url: `/pages/noticeDetail/index?id=${id}&type=announcement`, }) }, // 查看通知详情 handleNoticeDetail(e: WechatMiniprogram.TouchEvent) { const { taskId } = e.currentTarget.dataset wx.navigateTo({ url: `/pages/noticeDetail/index?taskId=${taskId}&type=notice`, }) }, handleActNotice(e) { const { index } = e.currentTarget.dataset const { activityList } = this.data const { jumpTarget } = activityList[index] wx.navigateTo({ url: `/pages/actDetail/index?id=${jumpTarget}`, }) }, // 下拉刷新 onPullDownRefresh() { const { active } = this.data if (active === 0) { this.fetchAnnouncementList(true) } else if (active === 1) { this.fetchNoticeList('important', true) } else if (active === 2) { this.fetchNoticeList('activity', true) } wx.stopPullDownRefresh() }, // 上拉加载更多 onReachBottom() { const { active, announcementPagination, importantPagination, activityPagination } = this.data if (active === 0) { if (announcementPagination.page < announcementPagination.pages) { this.setData({ announcementPagination: { ...announcementPagination, page: announcementPagination.page + 1 }, }) this.fetchAnnouncementList() } } else if (active === 1) { if (importantPagination.page < importantPagination.pages) { this.setData({ importantPagination: { ...importantPagination, page: importantPagination.page + 1 }, }) this.fetchNoticeList('important') } } else if (active === 2) { if (activityPagination.page < activityPagination.pages) { this.setData({ activityPagination: { ...activityPagination, page: activityPagination.page + 1 }, }) this.fetchNoticeList('activity') } } }, }) export {}