From 8df43ff8d4c44024ce20c179766b21afc37ce5ea Mon Sep 17 00:00:00 2001 From: WindowBird <13870814+windows-bird@user.noreply.gitee.com> Date: Tue, 26 Aug 2025 10:57:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=94=A8=E5=88=86=E7=B1=BB=E5=88=86?= =?UTF-8?q?=E9=A1=B5=E5=8A=A0=E8=BD=BD=E5=99=A8=E7=9A=84=E5=AE=8C=E5=96=84?= =?UTF-8?q?2.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composables/usePagination.js | 72 ++++--- pages/announcementList/announcementList.vue | 10 +- pages/myOrder/myOrder.vue | 2 +- pages/useList/useList.vue | 206 +++++++++----------- 4 files changed, 145 insertions(+), 145 deletions(-) diff --git a/composables/usePagination.js b/composables/usePagination.js index 22dd800..b5e2056 100644 --- a/composables/usePagination.js +++ b/composables/usePagination.js @@ -41,27 +41,51 @@ export function usePagination(options = {}) { const canLoadMore = computed(() => mode === 'loadMore' && !noMore.value && !loading.value) /** - * 获取数据列表 - * @param {boolean} isRefresh - 是否为刷新操作 + * 获取数据 + * @param {boolean} reset - 是否重置列表 */ - const getList = async (isRefresh = false) => { + const getList = async (reset = false) => { if (loading.value) return - try { - loading.value = true - error.value = null + if (reset) { + list.value = [] + queryParams.value.pageNum = 1 + noMore.value = false + } - // 如果是刷新,重置页码 - if (isRefresh) { - queryParams.value.pageNum = 1 - noMore.value = false + loading.value = true + error.value = null + + try { + const response = await fetchData(queryParams.value) + + if (!response || response.code !== 200) { + throw new Error(response?.message || '获取数据失败') } - const res = await fetchData(queryParams.value) + const { rows = [], total = 0 } = response + + // 数据转换:将API返回的用户数据转换为页面需要的格式 + const transformedData = rows.map(user => ({ + ...user, + isExpanded: false, // 默认收起 + // 将orders转换为devices格式 + devices: user.orders + ? user.orders.map(order => ({ + type: order.typeName || '未知设备', + amount: order.amount || 0, + rentDate: formatDate(order.leaseTime), + period: order.suitName || '未知周期', + expiryDate: formatDate(order.expirationTime), + })) + : [], + })) - // 处理响应数据 - const newData = res?.rows || [] - const total = res?.total || 0 + if (reset) { + list.value = transformedData + } else { + list.value.push(...transformedData) + } // 更新分页信息 pagination.value = { @@ -71,20 +95,13 @@ export function usePagination(options = {}) { totalPages: Math.ceil(total / pageSize), } - // 更新数据列表 - if (isRefresh || queryParams.value.pageNum === 1) { - list.value = newData - } else { - list.value = [...list.value, ...newData] - } - // 检查是否还有更多数据 if (mode === 'loadMore') { noMore.value = queryParams.value.pageNum * pageSize >= total console.log(`noMore状态: ${noMore.value}, 当前页: ${queryParams.value.pageNum}, 每页: ${pageSize}, 总数: ${total}`) } - console.log(`获取数据成功: 第${queryParams.value.pageNum}页,共${newData.length}条`) + console.log(`获取数据成功: 第${queryParams.value.pageNum}页,共${transformedData.length}条`) } catch (err) { console.error('获取数据失败:', err) error.value = err @@ -160,6 +177,17 @@ export function usePagination(options = {}) { getList() } + /** + * 格式化日期 + * @param {string} dateString - 日期字符串 + * @returns {string} 格式化后的日期 + */ + const formatDate = (dateString) => { + if (!dateString) return '未知' + const date = new Date(dateString) + return `${date.getFullYear()}.${String(date.getMonth() + 1).padStart(2, '0')}.${String(date.getDate()).padStart(2, '0')}` + } + return { // 状态 list, diff --git a/pages/announcementList/announcementList.vue b/pages/announcementList/announcementList.vue index d4912f5..0eec870 100644 --- a/pages/announcementList/announcementList.vue +++ b/pages/announcementList/announcementList.vue @@ -30,7 +30,7 @@ const { list, loading, noMore, pagination, getList, loadMore } = usePagination({ fetchData: getArticleList, defaultParams: {}, mode: 'loadMore', - pageSize: 9, + pageSize: 15, }) const detail = item => { @@ -46,11 +46,11 @@ onMounted(() => { // 上拉加载更多 onReachBottom(() => { - console.log('触发上拉加载更多,当前状态:', { - loading: loading.value, - noMore: noMore.value, + console.log('触发上拉加载更多,当前状态:', { + loading: loading.value, + noMore: noMore.value, listLength: list.value.length, - total: pagination.value.total + total: pagination.value.total, }) loadMore() }) diff --git a/pages/myOrder/myOrder.vue b/pages/myOrder/myOrder.vue index 4c51f2a..95b46d6 100644 --- a/pages/myOrder/myOrder.vue +++ b/pages/myOrder/myOrder.vue @@ -71,8 +71,8 @@