import React, { useState, useEffect } from 'react' import { View, Text, ScrollView, Image } from '@tarojs/components' import Taro from '@tarojs/taro' import { listShopPointsOrder, cancelShopPointsOrder } from '@/api/shop/shopPointsOrder' import type { ShopPointsOrder, PointsOrderStatus } from '@/api/shop/shopPointsOrder' import EmptyState from '@/components/common/EmptyState' import LoadMore from '@/components/common/LoadMore' import { getCompressedImageUrl } from '@/utils/image' definePageConfig({ navigationBarTitleText: '积分订单', }) // Tab 配置 const TAB_LIST = [ { title: '全部', status: undefined }, { title: '待发货', status: 'paid' as PointsOrderStatus }, { title: '已完成', status: 'completed' as PointsOrderStatus }, { title: '已取消', status: 'cancelled' as PointsOrderStatus }, ] const PointsOrderListPage: React.FC = () => { const [tabIndex, setTabIndex] = useState(0) const [orders, setOrders] = useState([]) const [loading, setLoading] = useState(false) const [finished, setFinished] = useState(false) const [page, setPage] = useState(1) useEffect(() => { setOrders([]) setPage(1) setFinished(false) loadList(1) }, [tabIndex]) const loadList = async (p: number) => { if (loading) return setLoading(true) try { const status = TAB_LIST[tabIndex]?.status const params: any = { page: p, limit: 10, } if (status) { params.status = status } const res = await listShopPointsOrder(params) if (res?.list) { if (p === 1) { setOrders(res.list) } else { setOrders(prev => [...prev, ...res.list]) } setFinished(res.list.length < 10) setPage(p) } } catch (err) { console.error('加载积分订单失败', err) Taro.showToast({ title: '加载失败', icon: 'none' }) } finally { setLoading(false) } } const handleLoadMore = () => { if (!finished && !loading) { loadList(page + 1) } } const handleDetail = (id: string) => { Taro.navigateTo({ url: `/pages/points/order-list/index?id=${id}` }) } // 取消订单 const handleCancel = async (id: string) => { Taro.showModal({ title: '确认取消', content: '确定取消该订单吗?', confirmColor: '#0e932e', success: async (res) => { if (res.confirm) { try { await cancelShopPointsOrder(id) Taro.showToast({ title: '已取消', icon: 'success' }) setOrders([]) setPage(1) setFinished(false) loadList(1) } catch (err) { console.error('取消订单失败', err) Taro.showToast({ title: '取消失败', icon: 'none' }) } } } }) } // 获取状态文本和颜色 const getStatusInfo = (status: PointsOrderStatus) => { const map: Record = { 'pending': { label: '待支付', color: 'text-orange-500' }, 'paid': { label: '待发货', color: 'text-blue-500' }, 'shipped': { label: '已发货', color: 'text-blue-500' }, 'completed': { label: '已完成', color: 'text-green-500' }, 'cancelled': { label: '已取消', color: 'text-gray-400' }, } return map[status] || { label: '未知', color: 'text-gray-400' } } return ( {/* Tab 栏 */} {TAB_LIST.map((tab, index) => ( setTabIndex(index)} > {tab.title} {tabIndex === index && ( )} ))} {/* 订单列表 */} {orders.length > 0 ? ( orders.map(order => { const statusInfo = getStatusInfo(order.status) return ( handleDetail(order.id)} > {/* 顶部状态 */} {order.orderNo} {statusInfo.label} {/* 商品列表 */} {order.items.map((item, idx) => ( {item.goodsName} {item.points}积分 x {item.quantity} ))} {/* 底部操作 */} 共 {order.totalPoints} 积分 {order.status === 'paid' && ( { e.stopPropagation() handleCancel(order.id) }} > 取消订单 )} { e.stopPropagation() handleDetail(order.id) }} > 查看详情 ) }) ) : !loading ? ( ) : null} ) } export default PointsOrderListPage