|
@ -81,8 +81,25 @@ function OrderList(props: OrderListProps) { |
|
|
const [list, setList] = useState<OrderWithGoods[]>([]) |
|
|
const [list, setList] = useState<OrderWithGoods[]>([]) |
|
|
const [page, setPage] = useState(1) |
|
|
const [page, setPage] = useState(1) |
|
|
const [hasMore, setHasMore] = useState(true) |
|
|
const [hasMore, setHasMore] = useState(true) |
|
|
const [tapIndex, setTapIndex] = useState<string | number>(0) |
|
|
|
|
|
|
|
|
// 根据传入的statusFilter设置初始tab索引
|
|
|
|
|
|
const getInitialTabIndex = () => { |
|
|
|
|
|
if (props.searchParams?.statusFilter !== undefined) { |
|
|
|
|
|
// 如果statusFilter为-1,表示全部,对应index为0
|
|
|
|
|
|
if (props.searchParams.statusFilter === -1) { |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
const tab = tabs.find(t => t.statusFilter === props.searchParams?.statusFilter); |
|
|
|
|
|
return tab ? tab.index : 0; |
|
|
|
|
|
} |
|
|
|
|
|
return 0; |
|
|
|
|
|
}; |
|
|
|
|
|
const [tapIndex, setTapIndex] = useState<string | number>(() => { |
|
|
|
|
|
const initialIndex = getInitialTabIndex(); |
|
|
|
|
|
console.log('初始化tapIndex:', initialIndex, '对应statusFilter:', props.searchParams?.statusFilter); |
|
|
|
|
|
return initialIndex; |
|
|
|
|
|
}) |
|
|
const [loading, setLoading] = useState(false) |
|
|
const [loading, setLoading] = useState(false) |
|
|
|
|
|
const [error, setError] = useState<string | null>(null) |
|
|
|
|
|
|
|
|
// 获取订单状态文本
|
|
|
// 获取订单状态文本
|
|
|
const getOrderStatusText = (order: ShopOrder) => { |
|
|
const getOrderStatusText = (order: ShopOrder) => { |
|
@ -149,9 +166,10 @@ function OrderList(props: OrderListProps) { |
|
|
return params; |
|
|
return params; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const reload = async (resetPage = false) => { |
|
|
|
|
|
|
|
|
const reload = async (resetPage = false, targetPage?: number) => { |
|
|
setLoading(true); |
|
|
setLoading(true); |
|
|
const currentPage = resetPage ? 1 : page; |
|
|
|
|
|
|
|
|
setError(null); // 清除之前的错误
|
|
|
|
|
|
const currentPage = resetPage ? 1 : (targetPage || page); |
|
|
const statusParams = getOrderStatusParams(tapIndex); |
|
|
const statusParams = getOrderStatusParams(tapIndex); |
|
|
const searchConditions = { |
|
|
const searchConditions = { |
|
|
page: currentPage, |
|
|
page: currentPage, |
|
@ -169,30 +187,40 @@ function OrderList(props: OrderListProps) { |
|
|
let newList: OrderWithGoods[] = []; |
|
|
let newList: OrderWithGoods[] = []; |
|
|
|
|
|
|
|
|
if (res?.list && res?.list.length > 0) { |
|
|
if (res?.list && res?.list.length > 0) { |
|
|
// 为每个订单获取商品信息
|
|
|
|
|
|
const ordersWithGoods = await Promise.all( |
|
|
|
|
|
res.list.map(async (order) => { |
|
|
|
|
|
try { |
|
|
|
|
|
const orderGoods = await listShopOrderGoods({ orderId: order.orderId }); |
|
|
|
|
|
return { |
|
|
|
|
|
...order, |
|
|
|
|
|
orderGoods: orderGoods || [] |
|
|
|
|
|
}; |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error('获取订单商品失败:', error); |
|
|
|
|
|
return { |
|
|
|
|
|
...order, |
|
|
|
|
|
orderGoods: [] |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
// 批量获取订单商品信息,限制并发数量
|
|
|
|
|
|
const batchSize = 3; // 限制并发数量为3
|
|
|
|
|
|
const ordersWithGoods: OrderWithGoods[] = []; |
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < res.list.length; i += batchSize) { |
|
|
|
|
|
const batch = res.list.slice(i, i + batchSize); |
|
|
|
|
|
const batchResults = await Promise.all( |
|
|
|
|
|
batch.map(async (order) => { |
|
|
|
|
|
try { |
|
|
|
|
|
const orderGoods = await listShopOrderGoods({ orderId: order.orderId }); |
|
|
|
|
|
return { |
|
|
|
|
|
...order, |
|
|
|
|
|
orderGoods: orderGoods || [] |
|
|
|
|
|
}; |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error('获取订单商品失败:', error); |
|
|
|
|
|
return { |
|
|
|
|
|
...order, |
|
|
|
|
|
orderGoods: [] |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
); |
|
|
|
|
|
ordersWithGoods.push(...batchResults); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 合并数据
|
|
|
// 合并数据
|
|
|
newList = resetPage ? ordersWithGoods : list?.concat(ordersWithGoods); |
|
|
newList = resetPage ? ordersWithGoods : list?.concat(ordersWithGoods); |
|
|
setHasMore(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 正确判断是否还有更多数据
|
|
|
|
|
|
const hasMoreData = res.list.length >= 10; // 假设每页10条数据
|
|
|
|
|
|
setHasMore(hasMoreData); |
|
|
} else { |
|
|
} else { |
|
|
newList = []; |
|
|
|
|
|
|
|
|
newList = resetPage ? [] : list; |
|
|
setHasMore(false); |
|
|
setHasMore(false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -202,12 +230,20 @@ function OrderList(props: OrderListProps) { |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
console.error('加载订单失败:', error); |
|
|
console.error('加载订单失败:', error); |
|
|
setLoading(false); |
|
|
setLoading(false); |
|
|
|
|
|
setError('加载订单失败,请重试'); |
|
|
|
|
|
// 添加错误提示
|
|
|
|
|
|
Taro.showToast({ |
|
|
|
|
|
title: '加载失败,请重试', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const reloadMore = async () => { |
|
|
const reloadMore = async () => { |
|
|
setPage(page + 1); |
|
|
|
|
|
reload(); |
|
|
|
|
|
|
|
|
if (loading || !hasMore) return; // 防止重复加载
|
|
|
|
|
|
const nextPage = page + 1; |
|
|
|
|
|
setPage(nextPage); |
|
|
|
|
|
await reload(false, nextPage); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 确认收货
|
|
|
// 确认收货
|
|
@ -233,9 +269,25 @@ function OrderList(props: OrderListProps) { |
|
|
// 取消订单
|
|
|
// 取消订单
|
|
|
const cancelOrder = async (order: ShopOrder) => { |
|
|
const cancelOrder = async (order: ShopOrder) => { |
|
|
try { |
|
|
try { |
|
|
await removeShopOrder(order.orderId); |
|
|
|
|
|
|
|
|
// 显示确认对话框
|
|
|
|
|
|
const result = await Taro.showModal({ |
|
|
|
|
|
title: '确认取消', |
|
|
|
|
|
content: '确定要取消这个订单吗?', |
|
|
|
|
|
confirmText: '确认取消', |
|
|
|
|
|
cancelText: '我再想想' |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (!result.confirm) return; |
|
|
|
|
|
|
|
|
|
|
|
// 更新订单状态为已取消,而不是删除订单
|
|
|
|
|
|
await updateShopOrder({ |
|
|
|
|
|
...order, |
|
|
|
|
|
orderStatus: 2 // 已取消
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
Taro.showToast({ |
|
|
Taro.showToast({ |
|
|
title: '订单已删除', |
|
|
|
|
|
|
|
|
title: '订单已取消', |
|
|
|
|
|
icon: 'success' |
|
|
}); |
|
|
}); |
|
|
reload(true).then(); // 重新加载列表
|
|
|
reload(true).then(); // 重新加载列表
|
|
|
props.onReload?.(); // 通知父组件刷新
|
|
|
props.onReload?.(); // 通知父组件刷新
|
|
@ -243,6 +295,7 @@ function OrderList(props: OrderListProps) { |
|
|
console.error('取消订单失败:', error); |
|
|
console.error('取消订单失败:', error); |
|
|
Taro.showToast({ |
|
|
Taro.showToast({ |
|
|
title: '取消订单失败', |
|
|
title: '取消订单失败', |
|
|
|
|
|
icon: 'error' |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
@ -252,6 +305,30 @@ function OrderList(props: OrderListProps) { |
|
|
}, [tapIndex]); // 监听tapIndex变化
|
|
|
}, [tapIndex]); // 监听tapIndex变化
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
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 |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (targetTabIndex !== tapIndex) { |
|
|
|
|
|
setTapIndex(targetTabIndex); |
|
|
|
|
|
return; // 避免重复调用reload
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
reload(true).then(); // 搜索参数变化时重置页码
|
|
|
reload(true).then(); // 搜索参数变化时重置页码
|
|
|
}, [props.searchParams]); // 监听搜索参数变化
|
|
|
}, [props.searchParams]); // 监听搜索参数变化
|
|
|
|
|
|
|
|
@ -278,36 +355,48 @@ function OrderList(props: OrderListProps) { |
|
|
tabs?.map((item, index) => { |
|
|
tabs?.map((item, index) => { |
|
|
return ( |
|
|
return ( |
|
|
<TabPane |
|
|
<TabPane |
|
|
key={index} |
|
|
|
|
|
title={loading && tapIndex === index ? `${item.title}...` : item.title} |
|
|
|
|
|
|
|
|
key={item.index} |
|
|
|
|
|
title={loading && tapIndex === item.index ? `${item.title}...` : item.title} |
|
|
></TabPane> |
|
|
></TabPane> |
|
|
) |
|
|
) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
</Tabs> |
|
|
</Tabs> |
|
|
<div style={getInfiniteUlStyle(props.showSearch)} id="scroll"> |
|
|
<div style={getInfiniteUlStyle(props.showSearch)} id="scroll"> |
|
|
<InfiniteLoading |
|
|
|
|
|
target="scroll" |
|
|
|
|
|
hasMore={hasMore} |
|
|
|
|
|
onLoadMore={reloadMore} |
|
|
|
|
|
onScroll={() => { |
|
|
|
|
|
|
|
|
|
|
|
}} |
|
|
|
|
|
onScrollToUpper={() => { |
|
|
|
|
|
|
|
|
|
|
|
}} |
|
|
|
|
|
loadingText={ |
|
|
|
|
|
<> |
|
|
|
|
|
加载中 |
|
|
|
|
|
</> |
|
|
|
|
|
} |
|
|
|
|
|
loadMoreText={ |
|
|
|
|
|
<> |
|
|
|
|
|
没有更多了 |
|
|
|
|
|
</> |
|
|
|
|
|
} |
|
|
|
|
|
> |
|
|
|
|
|
{list?.map((item, index) => { |
|
|
|
|
|
|
|
|
{error ? ( |
|
|
|
|
|
<div className="flex flex-col items-center justify-center h-64"> |
|
|
|
|
|
<div className="text-gray-500 mb-4">{error}</div> |
|
|
|
|
|
<Button |
|
|
|
|
|
size="small" |
|
|
|
|
|
type="primary" |
|
|
|
|
|
onClick={() => reload(true)} |
|
|
|
|
|
> |
|
|
|
|
|
重新加载 |
|
|
|
|
|
</Button> |
|
|
|
|
|
</div> |
|
|
|
|
|
) : ( |
|
|
|
|
|
<InfiniteLoading |
|
|
|
|
|
target="scroll" |
|
|
|
|
|
hasMore={hasMore} |
|
|
|
|
|
onLoadMore={reloadMore} |
|
|
|
|
|
onScroll={() => { |
|
|
|
|
|
|
|
|
|
|
|
}} |
|
|
|
|
|
onScrollToUpper={() => { |
|
|
|
|
|
|
|
|
|
|
|
}} |
|
|
|
|
|
loadingText={ |
|
|
|
|
|
<> |
|
|
|
|
|
加载中 |
|
|
|
|
|
</> |
|
|
|
|
|
} |
|
|
|
|
|
loadMoreText={ |
|
|
|
|
|
<> |
|
|
|
|
|
没有更多了 |
|
|
|
|
|
</> |
|
|
|
|
|
} |
|
|
|
|
|
> |
|
|
|
|
|
{list?.map((item, index) => { |
|
|
return ( |
|
|
return ( |
|
|
<Cell key={index} style={{padding: '16px'}} onClick={() => Taro.navigateTo({url: `/shop/orderDetail/index?orderId=${item.orderId}`})}> |
|
|
<Cell key={index} style={{padding: '16px'}} onClick={() => Taro.navigateTo({url: `/shop/orderDetail/index?orderId=${item.orderId}`})}> |
|
|
<Space direction={'vertical'} className={'w-full flex flex-col'}> |
|
|
<Space direction={'vertical'} className={'w-full flex flex-col'}> |
|
@ -378,8 +467,9 @@ function OrderList(props: OrderListProps) { |
|
|
</Space> |
|
|
</Space> |
|
|
</Cell> |
|
|
</Cell> |
|
|
) |
|
|
) |
|
|
})} |
|
|
|
|
|
</InfiniteLoading> |
|
|
|
|
|
|
|
|
})} |
|
|
|
|
|
</InfiniteLoading> |
|
|
|
|
|
)} |
|
|
</div> |
|
|
</div> |
|
|
</> |
|
|
</> |
|
|
) |
|
|
) |
|
|