import http from '../../../utils/api' Page({ data: { currentDate: '2023年11月15日', searchKeyword: '', activeFilter: '全部', activeFilterValue: '', showPolicyDetail: false, currentPolicy: {}, filteredPolicies: [], dictType: [], // 分页相关 pageNum: 1, pageSize: 10, total: 0, hasMore: true, isLoading: false, isLoadingMore: false, // 滚动区域高度 scrollHeight: 0, // 搜索定时器 searchTimer: null }, onLoad: function(options) { this.getpolicyeZd() this.getpolicyelucidation() // 计算滚动区域高度 this.calculateScrollHeight() }, onReady: function() { // 确保获取正确的高度 this.calculateScrollHeight() }, // 计算滚动区域高度 calculateScrollHeight: function() { const systemInfo = wx.getSystemInfoSync() const query = wx.createSelectorQuery() query.select('.search-section').boundingClientRect() query.select('.bottom-tip').boundingClientRect() query.exec((res) => { if (res[0] && res[1]) { const searchHeight = res[0].height const bottomHeight = res[1].height const windowHeight = systemInfo.windowHeight const scrollHeight = windowHeight - searchHeight - bottomHeight - 40 this.setData({ scrollHeight: Math.max(scrollHeight, 400) // 设置最小高度 }) } }) }, // 政策解读列表(支持分页和筛选) getpolicyelucidation: function(isLoadMore = false) { if (isLoadMore && (!this.data.hasMore || this.data.isLoadingMore)) { return } if (!isLoadMore) { this.setData({ isLoading: true }) } else { this.setData({ isLoadingMore: true }) } const params = { pageNum: isLoadMore ? this.data.pageNum : 1, pageSize: this.data.pageSize, searchKey: this.data.searchKeyword || undefined } // 如果选择了分类(不是"全部"),则添加分类参数 if (this.data.activeFilterValue && this.data.activeFilter !== '全部') { params.policyCategory = this.data.activeFilterValue } http.policyelucidation({ data: params, success: res => { console.log('政策列表:', res) const newList = isLoadMore ? [...this.data.filteredPolicies, ...res.rows] : res.rows const total = res.total || 0 const currentTotal = newList.length const hasMore = currentTotal < total this.setData({ filteredPolicies: newList, total: total, hasMore: hasMore, pageNum: isLoadMore ? this.data.pageNum + 1 : 2, isLoading: false, isLoadingMore: false }) }, fail: (err) => { console.error('获取政策列表失败:', err) this.setData({ isLoading: false, isLoadingMore: false }) if (!isLoadMore) { wx.showToast({ title: '加载失败', icon: 'none' }) } } }) }, // 类型 getpolicyeZd: function() { http.policyeZd({ data: { dictType: 'policy_category' }, success: res => { console.log('分类数据:', res) this.setData({ dictType: res.rows }) }, fail: (err) => { console.error('获取分类失败:', err) wx.showToast({ title: '加载分类失败', icon: 'none' }) } }) }, // 搜索输入处理(防抖) onSearchInput: function(e) { const searchKeyword = e.detail.value.trim() this.setData({ searchKeyword: searchKeyword }) // 清除之前的定时器 if (this.data.searchTimer) { clearTimeout(this.data.searchTimer) } // 设置新的定时器(500ms防抖) this.data.searchTimer = setTimeout(() => { // 重置分页 this.setData({ pageNum: 1, hasMore: true }, () => { this.getpolicyelucidation() }) }, 500) }, // 清空搜索 onClearSearch: function() { this.setData({ searchKeyword: '', pageNum: 1, hasMore: true }, () => { this.getpolicyelucidation() }) }, // 搜索按钮点击(键盘搜索键) onSearch: function() { this.setData({ pageNum: 1, hasMore: true }, () => { this.getpolicyelucidation() }) }, // 筛选标签点击 onFilterTap: function(e) { const filter = e.currentTarget.dataset.filter const value = e.currentTarget.dataset.value this.setData({ activeFilter: filter, activeFilterValue: value, pageNum: 1, hasMore: true }, () => { this.getpolicyelucidation() }) }, // 政策卡片点击 onPolicyTap: function(e) { const policyId = e.currentTarget.dataset.id http.policyeDetails({ data: { id: policyId }, success: res => { console.log('政策详情:', res) this.setData({ showPolicyDetail: true, currentPolicy: res.data }) }, fail: (err) => { console.error('获取政策详情失败:', err) wx.showToast({ title: '加载详情失败', icon: 'none' }) } }) }, // 上拉加载更多 onReachBottom: function() { if (this.data.hasMore && !this.data.isLoadingMore) { this.getpolicyelucidation(true) } }, // 隐藏政策详情 hidePolicyDetail: function() { this.setData({ showPolicyDetail: false }) }, // 阻止事件冒泡 stopPropagation: function() { // 空函数,仅用于阻止事件冒泡 } })