|
|
import http from '../../../utils/api'
Page({ data: { // 分类数据
categories: [], currentCategory: '全部', // 当前选中分类
currentType: '', // 当前选中的分类值
searchKeyword: '', // 搜索关键字
noticeList: [], // 渲染列表数据
pageIndex: 1, // 页码
pageSize: 8, // 每页条数
hasMore: true, // 是否有更多
loading: false, // 加载中
refreshing: false, // 下拉刷新状态
total: 0, // 总条数
searchTimer: null, // 搜索防抖定时器
isSearching: false, // 是否正在搜索
},
onLoad() { this.getwarningType() this.fetchNotices(true) },
onUnload() { // 页面卸载时清除定时器
if (this.data.searchTimer) { clearTimeout(this.data.searchTimer) } },
// 获取分类数据
getwarningType() { http.warningType({ data: { dictType: 'warning_type' }, success: res => { console.log('分类数据:', res) this.setData({ categories: res.rows || [] }) }, fail: err => { console.error('获取分类失败:', err) } }) },
// 获取通知公告列表
fetchNotices(isRefresh = false) { const { pageIndex, pageSize, searchKeyword, currentType, loading, hasMore } = this.data // 如果是刷新或者没有更多数据且不是刷新,则返回
if (!isRefresh && !hasMore) return if (loading) return
this.setData({ loading: true })
// 构建请求参数
const params = { pageNum: isRefresh ? 1 : pageIndex, pageSize: pageSize, }
// 添加搜索关键字(如果有关键字)
if (searchKeyword && searchKeyword.trim() !== '') { params.keyword = searchKeyword.trim() }
// 添加分类筛选(如果有分类且不是"全部")
if (currentType) { params.warningType = currentType }
console.log('请求参数:', params, '当前分类:', this.data.currentCategory, '当前类型:', currentType)
http.disaster({ data: params, success: res => { console.log('公告数据:', res) const newList = res.rows || [] const total = res.total || 0 this.setData({ noticeList: isRefresh ? newList : [...this.data.noticeList, ...newList], total: total, hasMore: newList.length >= pageSize, pageIndex: isRefresh ? 2 : pageIndex + 1, loading: false, refreshing: false, isSearching: false }) }, fail: err => { console.error('获取公告失败:', err) this.setData({ loading: false, refreshing: false, isSearching: false }) wx.showToast({ title: '加载失败', icon: 'none' }) } }) },
// 切换分类
switchCategory(e) { const category = e.currentTarget.dataset.category const type = e.currentTarget.dataset.type || '' if (category === this.data.currentCategory) return console.log('切换分类:', category, type) this.setData({ currentCategory: category, currentType: type, pageIndex: 1, hasMore: true, noticeList: [] }, () => { // 切换分类后立即加载数据(保留当前搜索关键词)
this.fetchNotices(true) }) },
// 搜索输入 - 实时搜索
onSearchInput(e) { const keyword = e.detail.value this.setData({ searchKeyword: keyword, isSearching: true }) // 清除之前的定时器
if (this.data.searchTimer) { clearTimeout(this.data.searchTimer) } // 设置新的定时器,300ms后执行搜索
const timer = setTimeout(() => { this.performSearch() }, 300) this.setData({ searchTimer: timer }) },
// 执行搜索
performSearch() { const { searchKeyword } = this.data console.log('执行搜索:', searchKeyword) // 重置列表并加载新数据
this.setData({ pageIndex: 1, hasMore: true, noticeList: [] }, () => { this.fetchNotices(true) }) },
// 手动搜索(点击确认时)
handleSearch() { // 清除防抖定时器
if (this.data.searchTimer) { clearTimeout(this.data.searchTimer) } // 立即执行搜索
this.setData({ pageIndex: 1, hasMore: true, noticeList: [] }, () => { this.fetchNotices(true) }) },
// 清空搜索框
clearSearch() { // 清除防抖定时器
if (this.data.searchTimer) { clearTimeout(this.data.searchTimer) } this.setData({ searchKeyword: '', pageIndex: 1, hasMore: true, noticeList: [] }, () => { // 清空搜索后重新加载数据(保持当前分类)
this.fetchNotices(true) }) },
// 上拉加载更多
loadMore() { const { hasMore, loading, refreshing, isSearching } = this.data if (!hasMore || loading || refreshing || isSearching) return this.fetchNotices(false) },
// 下拉刷新
onRefresh() { // 清除防抖定时器
if (this.data.searchTimer) { clearTimeout(this.data.searchTimer) } this.setData({ refreshing: true, pageIndex: 1, hasMore: true }, () => { this.fetchNotices(true) }) },
// 查看详情
viewDetail(e) { const id = e.currentTarget.dataset.id //跳转详情页
wx.navigateTo({ url: `/pagesA/pages/noticeListDetails/noticeListDetails?id=${id}`, }) },
// 根据重要性获取颜色
getImportanceColor(level) { const colorMap = { '紧急': '#f43f5e', '重要': '#f97316', '一般': '#3b82f6' } return colorMap[level] || '#3b82f6' },
// 获取标签背景色
getTagBg(level) { const bgMap = { '紧急': '#fee2e2', '重要': '#fff3e0', '一般': '#eef2ff' } return bgMap[level] || '#eef2ff' },
// 获取级别文字颜色
getLevelColor(level) { const colorMap = { '紧急': '#f43f5e', '重要': '#f97316', '一般': '#3b82f6' } return colorMap[level] || '#3b82f6' },
// 获取级别背景色
getLevelBg(level) { const bgMap = { '紧急': 'rgba(244, 63, 94, 0.1)', '重要': 'rgba(249, 115, 22, 0.1)', '一般': 'rgba(59, 130, 246, 0.1)' } return bgMap[level] || 'rgba(59, 130, 246, 0.1)' }})
|