|
|
import http from '../../../utils/api'const baseUr = require('../../../utils/baseUrl')
Page({ data: { // 药品列表数据
filteredList: [], // 搜索相关
searchKeyword: '', // 筛选相关
currentCategory: '全部', baseUr: baseUr, // 分页相关
pageNum: 1, pageSize: 10, hasMore: true, loading: false, isRefreshing: false, // 滚动相关
showBackToTop: false, scrollTop: 0 }, onLoad: function() { // 初始化加载数据
this.resetListAndLoad(); }, onShow: function() { // 页面显示时刷新数据
this.refreshList(); }, onPullDownRefresh: function() { // 下拉刷新
this.refreshList(); }, onReachBottom: function() { // 上拉加载更多
if (this.data.hasMore && !this.data.loading) { this.loadMore(); } }, onPageScroll: function(e) { // 显示/隐藏返回顶部按钮
if (e.scrollTop > 300 && !this.data.showBackToTop) { this.setData({ showBackToTop: true }); } else if (e.scrollTop <= 300 && this.data.showBackToTop) { this.setData({ showBackToTop: false }); } this.setData({ scrollTop: e.scrollTop }); },
/** * 获取药品列表数据 * @param {number} pageNum - 页码 * @param {boolean} isLoadMore - 是否为加载更多 * @param {string} searchKeywords - 搜索关键词 * @param {string} medicineType - 药品类型筛选 */ getRecommendationList: function(pageNum = 1, isLoadMore = false, searchKeywords = '', medicineType = '') { // 如果正在加载,防止重复请求
if (this.data.loading) return; this.setData({ loading: true, pageNum: pageNum }); // 构建请求参数
const params = { pageNum: pageNum, pageSize: this.data.pageSize, searchKeywords: searchKeywords || this.data.searchKeyword }; // 添加药品类型筛选(如果不是"全部")
if (medicineType && medicineType !== '全部') { params.medicineType = medicineType; } else if (this.data.currentCategory && this.data.currentCategory !== '全部') { params.medicineType = this.data.currentCategory; } http.recommendationList({ data: params, success: res => { const newData = res.rows || []; const total = res.total || 0; // 计算是否还有更多数据
const hasMore = newData.length >= this.data.pageSize && (this.data.filteredList.length + newData.length) < total; if (isLoadMore) { // 加载更多:追加数据
this.setData({ filteredList: [...this.data.filteredList, ...newData], hasMore: hasMore, loading: false }); } else { // 刷新或首次加载:替换数据
this.setData({ filteredList: newData, hasMore: hasMore, loading: false }); } // 停止下拉刷新
if (this.data.isRefreshing) { wx.stopPullDownRefresh(); this.setData({ isRefreshing: false }); } console.log('加载成功,当前数据量:', this.data.filteredList.length, '是否有更多:', hasMore); }, fail: err => { console.error('加载数据失败:', err); wx.showToast({ title: '加载失败', icon: 'none' }); this.setData({ loading: false, isRefreshing: false }); // 停止下拉刷新
wx.stopPullDownRefresh(); } }); },
/** * 重置列表并加载第一页 */ resetListAndLoad: function() { this.setData({ pageNum: 1, hasMore: true, filteredList: [] }, () => { this.getRecommendationList(1, false); }); },
/** * 刷新列表(第一页) */ refreshList: function() { this.setData({ pageNum: 1, hasMore: true, isRefreshing: true }); this.getRecommendationList(1, false); },
/** * 加载更多数据 */ loadMore: function() { if (!this.data.hasMore || this.data.loading) return; const nextPage = this.data.pageNum + 1; this.getRecommendationList(nextPage, true); },
/** * 搜索输入处理 */ onSearchInput: function(e) { const keyword = e.detail.value.trim(); this.setData({ searchKeyword: keyword }, () => { // 防抖处理,避免频繁请求
clearTimeout(this.searchTimer); this.searchTimer = setTimeout(() => { this.resetListAndLoad(); }, 500); }); },
/** * 清除搜索 */ clearSearch: function() { this.setData({ searchKeyword: '' }, () => { this.resetListAndLoad(); }); },
/** * 分类筛选切换 */ onCategoryChange: function(e) { const category = e.currentTarget.dataset.category; this.setData({ currentCategory: category }, () => { this.resetListAndLoad(); }); },
/** * 显示药品详情 */ showMedicineDetail: function(e) { const medId = e.currentTarget.dataset.id; wx.navigateTo({ url: `/pagesA/pages/medicineDetails/medicineDetails?id=${medId}`, }); },
/** * 返回顶部 */ scrollToTop: function() { wx.pageScrollTo({ scrollTop: 0, duration: 300 }); }});
|