79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { request } from '../../utils/request'
|
|
import { PagedResult, NotificationItem } from '../../types/models'
|
|
import { formatRelativeTime } from '../../utils/format'
|
|
|
|
const FILTERS = [
|
|
{ name: '全部', value: 0 },
|
|
{ name: '点赞', value: 1 },
|
|
{ name: '订单', value: 2 },
|
|
{ name: '系统', value: 3 },
|
|
]
|
|
|
|
Page({
|
|
data: {
|
|
filters: FILTERS,
|
|
activeFilter: 0,
|
|
list: [] as NotificationItem[],
|
|
loading: true,
|
|
page: 1,
|
|
pageSize: 20,
|
|
hasMore: true,
|
|
},
|
|
|
|
onShow() {
|
|
this.refresh()
|
|
this.fetchUnread()
|
|
},
|
|
|
|
fetchUnread() {
|
|
request<{ unread_count: number }>({ url: '/notification/unread', showError: false })
|
|
.then((res) => {
|
|
wx.setStorageSync('zt_closet_unread', res.unread_count)
|
|
const tabBar = (this as any).getTabBar && (this as any).getTabBar()
|
|
if (tabBar && tabBar.refreshUnread) tabBar.refreshUnread()
|
|
})
|
|
.catch(() => {})
|
|
},
|
|
|
|
refresh() {
|
|
this.setData({ page: 1, hasMore: true, loading: this.data.list.length === 0 })
|
|
this.fetchList(1)
|
|
},
|
|
|
|
onReachBottom() {
|
|
if (!this.data.hasMore || this.data.loading) return
|
|
this.fetchList(this.data.page + 1)
|
|
},
|
|
|
|
fetchList(page: number) {
|
|
const params: Record<string, any> = { page, page_size: this.data.pageSize }
|
|
const filter = this.data.activeFilter
|
|
if (filter > 0) params.msg_type = filter
|
|
|
|
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)
|
|
this.setData({
|
|
list: merged,
|
|
hasMore: merged.length < res.total,
|
|
loading: false,
|
|
page,
|
|
})
|
|
})
|
|
.catch(() => this.setData({ loading: false }))
|
|
},
|
|
|
|
onFilterTap(e: WechatMiniprogram.TouchEvent) {
|
|
const index = Number(e.currentTarget.dataset.index)
|
|
if (index === this.data.activeFilter) return
|
|
this.setData({ activeFilter: index, list: [], loading: true })
|
|
this.refresh()
|
|
},
|
|
|
|
onItemTap(e: WechatMiniprogram.TouchEvent) {
|
|
const uuid = e.currentTarget.dataset.uuid as string
|
|
wx.navigateTo({ url: `/pages/notification-detail/notification-detail?uuid=${uuid}` })
|
|
},
|
|
})
|