|
@@ -1,26 +1,62 @@
|
|
|
-import type { CategoryOption, InheritItem, InheritLists } from '../../data/inherit-api'
|
|
|
|
|
|
|
+import type { CategoryOption, HeritageKey, InheritFilters, InheritItem, InheritLists } from '../../data/inherit-api'
|
|
|
import { getCultureListUrl } from '../../data/culture-content'
|
|
import { getCultureListUrl } from '../../data/culture-content'
|
|
|
-import { getInheritCategories, getInheritLists, HERITAGE_COLUMNS } from '../../data/inherit-api'
|
|
|
|
|
|
|
+import { getInheritCategories, getInheritLists, getInheritStats, HERITAGE_COLUMNS } from '../../data/inherit-api'
|
|
|
import { runRefresh } from '../../utils/refresh'
|
|
import { runRefresh } from '../../utils/refresh'
|
|
|
import { syncTabBar } from '../../utils/tab-bar'
|
|
import { syncTabBar } from '../../utils/tab-bar'
|
|
|
|
|
|
|
|
-interface TagOption extends CategoryOption {
|
|
|
|
|
- active: boolean
|
|
|
|
|
|
|
+const ALL_OPTION: CategoryOption = { id: '', name: '全部', pid: '' }
|
|
|
|
|
+
|
|
|
|
|
+const FILTERS = [
|
|
|
|
|
+ { key: 'category', label: '非遗门类' },
|
|
|
|
|
+ { key: 'level', label: '保护级别' },
|
|
|
|
|
+ { key: 'district', label: '漳州各区县' },
|
|
|
|
|
+] as const
|
|
|
|
|
+
|
|
|
|
|
+type FilterKey = typeof FILTERS[number]['key']
|
|
|
|
|
+type FilterOptions = Record<FilterKey, CategoryOption[]>
|
|
|
|
|
+type FilterValues = Record<FilterKey, string>
|
|
|
|
|
+
|
|
|
|
|
+function createFilterValues(): FilterValues {
|
|
|
|
|
+ return { category: '', level: '', district: '' }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const ALL_OPTION: CategoryOption = { id: '', name: '全部', pid: '' }
|
|
|
|
|
|
|
+function createFilterOptions(): FilterOptions {
|
|
|
|
|
+ return { category: [ALL_OPTION], level: [ALL_OPTION], district: [ALL_OPTION] }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 筛选条由选项和当前选中 id 推导,picker 需要名称数组和下标。
|
|
|
|
|
+function buildFilterBar(options: FilterOptions, values: FilterValues) {
|
|
|
|
|
+ return FILTERS.map((filter) => {
|
|
|
|
|
+ const list = options[filter.key]
|
|
|
|
|
+ const index = Math.max(0, list.findIndex(option => option.id === values[filter.key]))
|
|
|
|
|
+ return { key: filter.key, label: filter.label, names: list.map(option => option.name), index, value: index ? list[index].name : '' }
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function buildStats(stats?: Record<HeritageKey, number>) {
|
|
|
|
|
+ return HERITAGE_COLUMNS.map(column => ({
|
|
|
|
|
+ key: column.key,
|
|
|
|
|
+ label: column.label,
|
|
|
|
|
+ icon: column.icon,
|
|
|
|
|
+ unit: column.unit,
|
|
|
|
|
+ value: stats ? String(stats[column.key]) : '—',
|
|
|
|
|
+ }))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function toInheritFilters(keyword: string, values: FilterValues): InheritFilters {
|
|
|
|
|
+ return { keyword, categoryId: values.category, levelId: values.level, districtId: values.district }
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
Component({
|
|
Component({
|
|
|
data: {
|
|
data: {
|
|
|
refreshing: false,
|
|
refreshing: false,
|
|
|
keyword: '',
|
|
keyword: '',
|
|
|
- stats: HERITAGE_COLUMNS.map(column => ({ label: column.label, value: '—', unit: column.unit })),
|
|
|
|
|
- categories: [ALL_OPTION] as CategoryOption[],
|
|
|
|
|
- selectedCategory: '',
|
|
|
|
|
- levels: [{ ...ALL_OPTION, active: true }] as TagOption[],
|
|
|
|
|
- districts: [ALL_OPTION] as CategoryOption[],
|
|
|
|
|
|
|
+ stats: buildStats(),
|
|
|
|
|
+ filterOptions: createFilterOptions(),
|
|
|
|
|
+ filterValues: createFilterValues(),
|
|
|
|
|
+ filterBar: buildFilterBar(createFilterOptions(), createFilterValues()),
|
|
|
|
|
+ hasFilter: false,
|
|
|
optionsLoaded: false,
|
|
optionsLoaded: false,
|
|
|
- selectedDistrict: '',
|
|
|
|
|
projects: [] as InheritItem[],
|
|
projects: [] as InheritItem[],
|
|
|
inheritors: [] as InheritItem[],
|
|
inheritors: [] as InheritItem[],
|
|
|
units: [] as InheritItem[],
|
|
units: [] as InheritItem[],
|
|
@@ -34,6 +70,7 @@ Component({
|
|
|
lifetimes: {
|
|
lifetimes: {
|
|
|
attached() {
|
|
attached() {
|
|
|
this.loadData()
|
|
this.loadData()
|
|
|
|
|
+ this.loadStats().catch(() => {})
|
|
|
},
|
|
},
|
|
|
detached() {
|
|
detached() {
|
|
|
this.data.requestVersion++
|
|
this.data.requestVersion++
|
|
@@ -49,7 +86,7 @@ Component({
|
|
|
methods: {
|
|
methods: {
|
|
|
async onPullRefresh() {
|
|
async onPullRefresh() {
|
|
|
this.setData({ refreshing: true })
|
|
this.setData({ refreshing: true })
|
|
|
- await runRefresh([this.loadData(true)])
|
|
|
|
|
|
|
+ await runRefresh([this.loadData(true), this.loadStats()])
|
|
|
this.setData({ refreshing: false })
|
|
this.setData({ refreshing: false })
|
|
|
},
|
|
},
|
|
|
|
|
|
|
@@ -57,17 +94,17 @@ Component({
|
|
|
return this.loadData()
|
|
return this.loadData()
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
|
|
+ // 顶部统计不随筛选变化,只在进入页面和下拉刷新时请求;失败时保留原数字。
|
|
|
|
|
+ async loadStats() {
|
|
|
|
|
+ const stats = await getInheritStats()
|
|
|
|
|
+ this.setData({ stats: buildStats(stats) })
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
// refresh 为下拉刷新:同时重新拉取筛选项,不切换到加载态,已有内容时失败保留原内容。
|
|
// refresh 为下拉刷新:同时重新拉取筛选项,不切换到加载态,已有内容时失败保留原内容。
|
|
|
async loadData(refresh = false) {
|
|
async loadData(refresh = false) {
|
|
|
const requestVersion = ++this.data.requestVersion
|
|
const requestVersion = ++this.data.requestVersion
|
|
|
- const { keyword, selectedCategory, selectedDistrict, levels, optionsLoaded } = this.data
|
|
|
|
|
- const filters = {
|
|
|
|
|
- keyword,
|
|
|
|
|
- categoryId: selectedCategory,
|
|
|
|
|
- levelId: levels.find(level => level.active)?.id || '',
|
|
|
|
|
- districtId: selectedDistrict,
|
|
|
|
|
- }
|
|
|
|
|
- const unfiltered = !filters.keyword.trim() && !filters.categoryId && !filters.levelId && !filters.districtId
|
|
|
|
|
|
|
+ const { keyword, filterValues, optionsLoaded } = this.data
|
|
|
|
|
+ const filters = toInheritFilters(keyword, filterValues)
|
|
|
if (!refresh) {
|
|
if (!refresh) {
|
|
|
this.setData({ loading: true, loadError: '' })
|
|
this.setData({ loading: true, loadError: '' })
|
|
|
}
|
|
}
|
|
@@ -81,22 +118,17 @@ Component({
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
if (options) {
|
|
if (options) {
|
|
|
|
|
+ const filterOptions: FilterOptions = {
|
|
|
|
|
+ category: [ALL_OPTION, ...options.categories],
|
|
|
|
|
+ level: [ALL_OPTION, ...options.levels],
|
|
|
|
|
+ district: [ALL_OPTION, ...options.districts],
|
|
|
|
|
+ }
|
|
|
this.setData({
|
|
this.setData({
|
|
|
- categories: [ALL_OPTION, ...options.categories],
|
|
|
|
|
- districts: [ALL_OPTION, ...options.districts],
|
|
|
|
|
- levels: [
|
|
|
|
|
- { ...ALL_OPTION, active: !filters.levelId },
|
|
|
|
|
- ...options.levels.map(item => ({ ...item, active: filters.levelId === item.id })),
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ filterOptions,
|
|
|
|
|
+ filterBar: buildFilterBar(filterOptions, this.data.filterValues),
|
|
|
optionsLoaded: true,
|
|
optionsLoaded: true,
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
- // 顶部统计展示全部数据的总数,只用不带筛选条件的结果更新。
|
|
|
|
|
- if (unfiltered) {
|
|
|
|
|
- this.setData({
|
|
|
|
|
- stats: HERITAGE_COLUMNS.map(column => ({ label: column.label, value: String(result.totals[column.key]), unit: column.unit })),
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
this.setData({ loading: false, loadError: '' })
|
|
this.setData({ loading: false, loadError: '' })
|
|
|
this.refresh(result.lists)
|
|
this.refresh(result.lists)
|
|
|
}
|
|
}
|
|
@@ -124,39 +156,40 @@ Component({
|
|
|
return this.loadData()
|
|
return this.loadData()
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
- onSelectCategory(e: WechatMiniprogram.TouchEvent) {
|
|
|
|
|
- const id = e.currentTarget.dataset.id as string
|
|
|
|
|
- if (this.data.selectedCategory === id) {
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- this.setData({ selectedCategory: id })
|
|
|
|
|
- return this.loadData()
|
|
|
|
|
|
|
+ setFilterValues(filterValues: FilterValues) {
|
|
|
|
|
+ this.setData({
|
|
|
|
|
+ filterValues,
|
|
|
|
|
+ filterBar: buildFilterBar(this.data.filterOptions, filterValues),
|
|
|
|
|
+ hasFilter: FILTERS.some(filter => filterValues[filter.key]),
|
|
|
|
|
+ })
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
- onSelectLevel(e: WechatMiniprogram.TouchEvent) {
|
|
|
|
|
- const id = e.currentTarget.dataset.id as string
|
|
|
|
|
- const option = this.data.levels.find(level => level.id === id)
|
|
|
|
|
- if (!option || option.active) {
|
|
|
|
|
|
|
+ onFilterChange(e: WechatMiniprogram.PickerChange) {
|
|
|
|
|
+ const key = e.currentTarget.dataset.key as FilterKey
|
|
|
|
|
+ const option = this.data.filterOptions[key]?.[Number(e.detail.value)]
|
|
|
|
|
+ if (!option || this.data.filterValues[key] === option.id) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- const levels = this.data.levels.map(level => ({ ...level, active: level.id === id }))
|
|
|
|
|
- this.setData({ levels })
|
|
|
|
|
|
|
+ this.setFilterValues({ ...this.data.filterValues, [key]: option.id })
|
|
|
return this.loadData()
|
|
return this.loadData()
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
- onSelectDistrict(e: WechatMiniprogram.TouchEvent) {
|
|
|
|
|
- this.setData({ selectedDistrict: e.currentTarget.dataset.id as string })
|
|
|
|
|
|
|
+ onResetFilters() {
|
|
|
|
|
+ this.setFilterValues(createFilterValues())
|
|
|
return this.loadData()
|
|
return this.loadData()
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
onReset() {
|
|
onReset() {
|
|
|
- this.setData({
|
|
|
|
|
- keyword: '',
|
|
|
|
|
- selectedCategory: '',
|
|
|
|
|
- levels: this.data.levels.map(level => ({ ...level, active: !level.id })),
|
|
|
|
|
- selectedDistrict: '',
|
|
|
|
|
- })
|
|
|
|
|
- return this.loadData()
|
|
|
|
|
|
|
+ this.setData({ keyword: '' })
|
|
|
|
|
+ return this.onResetFilters()
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ // 统计数字是全部数据的总数,进入列表时不带筛选,保证数量一致。
|
|
|
|
|
+ onStatTap(e: WechatMiniprogram.TouchEvent) {
|
|
|
|
|
+ const column = HERITAGE_COLUMNS.find(column => column.key === e.currentTarget.dataset.category)
|
|
|
|
|
+ if (column) {
|
|
|
|
|
+ wx.navigateTo({ url: getCultureListUrl(column.key) })
|
|
|
|
|
+ }
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
onMore(e: WechatMiniprogram.TouchEvent) {
|
|
onMore(e: WechatMiniprogram.TouchEvent) {
|
|
@@ -164,14 +197,8 @@ Component({
|
|
|
if (!column) {
|
|
if (!column) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- const { keyword, selectedCategory, selectedDistrict, levels } = this.data
|
|
|
|
|
- const filters = { keyword, categoryId: selectedCategory, levelId: levels.find(level => level.active)?.id || '', districtId: selectedDistrict }
|
|
|
|
|
// 沿用当前筛选,让“更多”展示当前栏目对应的完整结果。
|
|
// 沿用当前筛选,让“更多”展示当前栏目对应的完整结果。
|
|
|
- wx.navigateTo({ url: getCultureListUrl(column.key, filters) })
|
|
|
|
|
- },
|
|
|
|
|
-
|
|
|
|
|
- onBack() {
|
|
|
|
|
- wx.switchTab({ url: '/pages/home/home' })
|
|
|
|
|
|
|
+ wx.navigateTo({ url: getCultureListUrl(column.key, toInheritFilters(this.data.keyword, this.data.filterValues)) })
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
})
|
|
})
|