From f564703e51aec7a031908ad9a3d9efc83e79b561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Mon, 11 Aug 2025 22:37:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor(user/order):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=88=97=E8=A1=A8=E7=AD=9B=E9=80=89=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=92=8C=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在设置状态栏高度时增加空值合并操作,避免潜在的 null错误 - 移除了未使用的导入语句,减少代码冗余 - 重构了订单列表的搜索和筛选逻辑: - 调整了搜索条件的合并方式,确保 tab 的 statusFilter 优先级最高 -优化了 useEffect 监听的搜索参数,只在关键词等其他条件变化时重新加载 -简化了 tab 切换逻辑,提高了代码可读性 --- src/user/order/components/OrderList.tsx | 56 +++++++++++-------------- src/user/order/order.tsx | 2 +- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/user/order/components/OrderList.tsx b/src/user/order/components/OrderList.tsx index 293c4c2..c2182ef 100644 --- a/src/user/order/components/OrderList.tsx +++ b/src/user/order/components/OrderList.tsx @@ -4,7 +4,7 @@ import {View} from '@tarojs/components' import Taro from '@tarojs/taro'; import {InfiniteLoading} from '@nutui/nutui-react-taro' import dayjs from "dayjs"; -import {pageShopOrder, removeShopOrder, updateShopOrder} from "@/api/shop/shopOrder"; +import {pageShopOrder, updateShopOrder} from "@/api/shop/shopOrder"; import {ShopOrder, ShopOrderParam} from "@/api/shop/shopOrder/model"; import {listShopOrderGoods} from "@/api/shop/shopOrderGoods"; import {ShopOrderGoods} from "@/api/shop/shopOrderGoods/model"; @@ -171,10 +171,12 @@ function OrderList(props: OrderListProps) { setError(null); // 清除之前的错误 const currentPage = resetPage ? 1 : (targetPage || page); const statusParams = getOrderStatusParams(tapIndex); + // 合并搜索条件,tab的statusFilter优先级更高 const searchConditions = { page: currentPage, - ...statusParams, - ...props.searchParams + userId: statusParams.userId, // 用户ID + ...props.searchParams, // 搜索关键词等其他条件 + statusFilter: statusParams.statusFilter // tab的statusFilter优先级最高 }; console.log('订单筛选条件:', { tapIndex, @@ -305,32 +307,24 @@ function OrderList(props: OrderListProps) { }, [tapIndex]); // 监听tapIndex变化 useEffect(() => { - // 当外部传入的statusFilter改变时,更新对应的tab索引 - if (props.searchParams?.statusFilter !== undefined) { - let targetTabIndex = 0; - - // 如果statusFilter为-1,表示全部,对应index为0 - if (props.searchParams.statusFilter === -1) { - targetTabIndex = 0; - } else { - const tab = tabs.find(t => t.statusFilter === props.searchParams?.statusFilter); - targetTabIndex = tab ? tab.index : 0; - } - - console.log('searchParams变化:', { - statusFilter: props.searchParams.statusFilter, - currentTapIndex: tapIndex, - targetTabIndex, - shouldUpdate: targetTabIndex !== tapIndex - }); + // 当外部传入的搜索参数变化时(不包括statusFilter,因为tab切换会处理) + // 只有当搜索关键词等其他条件变化时才重新加载 + const { statusFilter, ...otherParams } = props.searchParams || {}; + + // 检查是否有除statusFilter外的其他搜索条件变化 + const hasOtherSearchParams = Object.keys(otherParams).some(key => + otherParams[key] !== undefined && otherParams[key] !== '' + ); + + console.log('searchParams变化 (非statusFilter):', { + otherParams, + hasOtherSearchParams + }); - if (targetTabIndex !== tapIndex) { - setTapIndex(targetTabIndex); - return; // 避免重复调用reload - } + if (hasOtherSearchParams) { + reload(true).then(); // 只有其他搜索条件变化时才重新加载 } - reload(true).then(); // 搜索参数变化时重置页码 - }, [props.searchParams]); // 监听搜索参数变化 + }, [props.searchParams?.keywords, props.searchParams?.orderNo]); // 只监听具体的搜索字段 return ( <> @@ -347,12 +341,12 @@ function OrderList(props: OrderListProps) { }} value={tapIndex} onChange={(paneKey) => { - console.log('Tab切换到:', paneKey, '对应状态:', tabs[paneKey]?.title); + console.log('Tab切换到:', paneKey, '对应状态:', tabs[paneKey]?.title, 'statusFilter:', tabs[paneKey]?.statusFilter); setTapIndex(paneKey) }} > { - tabs?.map((item, index) => { + tabs?.map((item, _) => { return ( } loadMoreText={ - <> + 没有更多了 - + } > {list?.map((item, index) => { diff --git a/src/user/order/order.tsx b/src/user/order/order.tsx index e0fe796..182b9e8 100644 --- a/src/user/order/order.tsx +++ b/src/user/order/order.tsx @@ -55,7 +55,7 @@ function Order() { // 获取状态栏高度 Taro.getSystemInfo({ success: (res) => { - setStatusBarHeight(res.statusBarHeight) + setStatusBarHeight(res.statusBarHeight ?? 0) }, }) // 设置导航栏标题