import http from '../../../utils/api' const baseUrl = require('../../../utils/baseUrl') Page({ data: { // 搜索文本 searchText: '', baseUrl: baseUrl, // 当前选中的分类 activeCategory: '', // 分类数据 categories: [], // 经验分享列表 experienceList: [], // 分页参数 pageNum: 1, pageSize: 10, total: 0, // 加载状态 loading: false, hasMore: true, // 搜索防抖定时器 searchTimer: null }, onLoad() { // 获取分类数据 this.getCategories() // 获取经验分享列表 this.getExperienceList(true) }, // 获取经验分享列表 getExperienceList(isRefresh = false) { if (this.data.loading) return const params = { pageNum: isRefresh ? 1 : this.data.pageNum, pageSize: this.data.pageSize } // 添加搜索关键词 if (this.data.searchText) { params.searchKey = this.data.searchText.trim() } // 添加分类筛选 if (this.data.activeCategory) { params.categoryName = this.data.activeCategory } this.setData({ loading: true }) http.experience({ data: params, success: res => { console.log('经验列表数据:', res) if (res.code === 200) { const newList = isRefresh ? res.rows : [...this.data.experienceList, ...res.rows] const total = res.total || 0 const hasMore = newList.length < total this.setData({ experienceList: newList, total: total, hasMore: hasMore, pageNum: isRefresh ? 2 : this.data.pageNum + 1, loading: false }) } else { wx.showToast({ title: res.msg || '加载失败', icon: 'none' }) this.setData({ loading: false }) } }, fail: err => { console.error('请求失败:', err) wx.showToast({ title: '网络错误,请重试', icon: 'none' }) this.setData({ loading: false }) } }) }, // 获取分类数据 getCategories() { http.experiencezd({ data: { categoryName: 'category_id' }, success: res => { console.log('分类数据:', res) if (res.code === 200) { this.setData({ categories: res.data || [] }) } }, fail: err => { console.error('获取分类失败:', err) } }) }, // 处理搜索输入(带防抖) onSearchInput(e) { const searchText = e.detail.value this.setData({ searchText: searchText }) // 清除之前的定时器 if (this.data.searchTimer) { clearTimeout(this.data.searchTimer) } // 设置新的定时器,500ms后执行搜索 const timer = setTimeout(() => { this.handleSearch() }, 500) this.setData({ searchTimer: timer }) }, // 搜索确认 onSearchConfirm() { if (this.data.searchTimer) { clearTimeout(this.data.searchTimer) } this.handleSearch() }, // 执行搜索 handleSearch() { this.setData({ pageNum: 1, hasMore: true }) this.getExperienceList(true) }, // 清空搜索 clearSearch() { this.setData({ searchText: '', pageNum: 1, hasMore: true }) this.getExperienceList(true) }, // 分类点击事件 onCategoryTap(e) { const categoryId = e.currentTarget.dataset.id if (this.data.activeCategory === categoryId) { return } this.setData({ activeCategory: categoryId, pageNum: 1, hasMore: true }) this.getExperienceList(true) }, // 滚动到底部加载更多 onScrollToLower() { if (!this.data.loading && this.data.hasMore) { this.getExperienceList() } }, // 点击加载更多 loadMoreData() { if (!this.data.loading && this.data.hasMore) { this.getExperienceList() } }, // 经验分享点击事件 onExperienceTap(e) { const id = e.currentTarget.dataset.id wx.navigateTo({ url: `/pagesB/pages/experienceDetails/experienceDetails?id=${id}`, }) }, onUnload() { // 清理定时器 if (this.data.searchTimer) { clearTimeout(this.data.searchTimer) } } })