This commit is contained in:
2026-09-13 01:51:09 +08:00
parent b1a664a768
commit afd0973ce5
32 changed files with 935 additions and 171 deletions
+21 -6
View File
@@ -3,6 +3,8 @@ import * as auth from '../../utils/auth'
import { PagedResult, NotificationItem } from '../../types/models'
import { STORAGE_KEYS } from '../../utils/config'
import { formatRelativeTime } from '../../utils/format'
import { normalizePaged, mergePaged, hasMorePaged } from '../../utils/paged'
import { getTabBarInstance } from '../../utils/tabbar'
const FILTERS = [
{ name: '全部', value: 0 },
@@ -17,6 +19,7 @@ Page({
activeFilter: 0,
list: [] as NotificationItem[],
loading: true,
loadingMore: false,
page: 1,
pageSize: 20,
hasMore: true,
@@ -41,7 +44,8 @@ Page({
.then((res) => {
const unread = res.unread_count || 0
wx.setStorageSync(STORAGE_KEYS.UNREAD, unread)
const tabBar = (this as any).getTabBar && (this as any).getTabBar()
// 本页不是 tab 页,getTabBar() 恒为 undefined,需从页面栈里取 tab 页的 tabBar
const tabBar = getTabBarInstance()
// 直接更新红点,避免 tabBar 内部再发一次请求
if (tabBar && tabBar.setUnread) tabBar.setUnread(unread)
})
@@ -54,7 +58,7 @@ Page({
},
onReachBottom() {
if (!this.data.hasMore || this.data.loading) return
if (!this.data.hasMore || this.data.loading || this.data.loadingMore) return
this.fetchList(this.data.page + 1)
},
@@ -63,18 +67,29 @@ Page({
const filter = this.data.activeFilter
if (filter > 0) params.msg_type = filter
// 请求序号:切换筛选时旧响应可能后到,序号不匹配直接丢弃
const seq = ((this as any).reqSeq || 0) + 1
;(this as any).reqSeq = seq
if (page > 1) this.setData({ loadingMore: true })
request<PagedResult<NotificationItem>>({ url: '/notification', data: params })
.then((res) => {
const list = res.list.map((it) => ({ ...it, _time: formatRelativeTime(it.created_at) }))
const merged = page === 1 ? list : this.data.list.concat(list)
if ((this as any).reqSeq !== seq) return
const { list: raw, total } = normalizePaged<NotificationItem>(res)
const list = raw.map((it) => ({ ...it, _time: formatRelativeTime(it.created_at) }))
const merged = mergePaged(this.data.list, list, page === 1)
this.setData({
list: merged,
hasMore: merged.length < res.total,
hasMore: hasMorePaged(merged, total, this.data.pageSize),
loading: false,
loadingMore: false,
page,
})
})
.catch(() => this.setData({ loading: false }))
.catch(() => {
if ((this as any).reqSeq !== seq) return
this.setData({ loading: false, loadingMore: false })
})
},
onFilterTap(e: WechatMiniprogram.TouchEvent) {