对齐官方 WebSocket 批量握手、注册、心跳、ACK、同步恢复及运行时设备标识生命周期。 保留 Cookie 完整属性,接入静默续期和保存登录信息设置,并在凭证更新后重建账号连接。 完善订单与商品同步、卡券和通知渠道交互,更新前端品牌、时间展示及嵌入资源。 补充 Go 与前端回归测试,确保完整检查和生产构建通过。
981 lines
43 KiB
TypeScript
981 lines
43 KiB
TypeScript
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
import { createPortal } from 'react-dom';
|
|
import { AccountDetail, Order, OrderStatus, Item } from '../types';
|
|
import { getOrders, syncOrders, syncSingleOrder, manualShipOrder, updateOrder, deleteOrder, importOrders, getItems, getAccountDetails } from '../services/api';
|
|
import { Search, MoreHorizontal, Truck, RefreshCw, Copy, ChevronLeft, ChevronRight, PackageCheck, Edit, Eye, Plus, Save, X, User as UserIcon, Phone, MapPin, Upload, ExternalLink, Trash2 } from 'lucide-react';
|
|
import { failedOrderImportRows, normalizeOrderImportResult, OrderImportResult } from './orderImportState';
|
|
import { formatLocalDateTime } from '../dateTime';
|
|
|
|
const StatusBadge: React.FC<{ status: OrderStatus }> = ({ status }) => {
|
|
const styles = {
|
|
processing: 'bg-blue-100 text-blue-800',
|
|
pending_ship: 'bg-[#0094f7] text-white',
|
|
shipped: 'bg-blue-100 text-blue-700',
|
|
completed: 'bg-green-100 text-green-700',
|
|
cancelled: 'bg-gray-100 text-gray-500',
|
|
refunding: 'bg-red-100 text-red-600',
|
|
unknown: 'bg-gray-100 text-gray-500',
|
|
};
|
|
|
|
const labels = {
|
|
processing: '处理中',
|
|
pending_ship: '待发货',
|
|
shipped: '已发货',
|
|
completed: '已完成',
|
|
cancelled: '已取消',
|
|
refunding: '退款中',
|
|
unknown: '未知',
|
|
};
|
|
|
|
return (
|
|
<span className={`px-3 py-1.5 rounded-lg text-xs font-bold ${styles[status] || styles.cancelled}`}>
|
|
{labels[status] || status}
|
|
</span>
|
|
);
|
|
};
|
|
|
|
const OrderList: React.FC = () => {
|
|
const [orders, setOrders] = useState<Order[]>([]);
|
|
const [items, setItems] = useState<Item[]>([]);
|
|
const [accounts, setAccounts] = useState<AccountDetail[]>([]);
|
|
const [itemNames, setItemNames] = useState<Record<string, string>>({});
|
|
const [filter, setFilter] = useState('all');
|
|
const [accountFilter, setAccountFilter] = useState('');
|
|
const [searchText, setSearchText] = useState(''); // 搜索文本
|
|
const [debouncedSearch, setDebouncedSearch] = useState('');
|
|
const requestSequence = useRef(0);
|
|
const [page, setPage] = useState(1);
|
|
const [totalPages, setTotalPages] = useState(1);
|
|
const [loading, setLoading] = useState(false);
|
|
const [showDetailModal, setShowDetailModal] = useState(false);
|
|
const [showEditModal, setShowEditModal] = useState(false);
|
|
const [showImportModal, setShowImportModal] = useState(false);
|
|
const [selectedOrder, setSelectedOrder] = useState<Order | null>(null);
|
|
const [editingOrder, setEditingOrder] = useState<Partial<Order> | null>(null);
|
|
const [editForm, setEditForm] = useState<Partial<Order>>({});
|
|
const [showShipModal, setShowShipModal] = useState(false);
|
|
const [shipOrderId, setShipOrderId] = useState<string>('');
|
|
const [shipLoading, setShipLoading] = useState(false);
|
|
const [shipResult, setShipResult] = useState<{success: boolean; message: string} | null>(null);
|
|
const [importFile, setImportFile] = useState<File | null>(null);
|
|
const [importResult, setImportResult] = useState<OrderImportResult | null>(null);
|
|
const [importing, setImporting] = useState(false);
|
|
const [syncingOrderId, setSyncingOrderId] = useState<string | null>(null);
|
|
const [deletingOrderId, setDeletingOrderId] = useState<string | null>(null);
|
|
|
|
const loadOrders = async () => {
|
|
const sequence = ++requestSequence.current;
|
|
setLoading(true);
|
|
|
|
try {
|
|
const res = await getOrders(accountFilter || undefined, filter, page, 20, debouncedSearch);
|
|
if (sequence !== requestSequence.current) return;
|
|
setOrders(res.data);
|
|
setTotalPages(res.total_pages);
|
|
} catch (e) {
|
|
console.error('加载订单失败:', e);
|
|
} finally {
|
|
if (sequence === requestSequence.current) setLoading(false);
|
|
}
|
|
};
|
|
|
|
// 从订单的 item_id 查找对应的商品名称(通过标题匹配)
|
|
const getItemNameById = (cookieId: string, orderId: string, orderItemTitle?: string): string => {
|
|
// 如果订单有 item_title,优先使用
|
|
if (orderItemTitle && orderItemTitle.trim()) {
|
|
return orderItemTitle;
|
|
}
|
|
|
|
// 尝试通过 item_id 直接匹配
|
|
if (itemNames[`${cookieId}:${orderId}`]) {
|
|
return itemNames[`${cookieId}:${orderId}`];
|
|
}
|
|
|
|
// 尝试在商品列表中查找相似标题的商品
|
|
const matchingItem = items.find(item => {
|
|
// 如果订单有标题,尝试匹配商品标题
|
|
if (orderItemTitle && item.item_title) {
|
|
// 检查是否包含关键词
|
|
const orderTitleLower = orderItemTitle.toLowerCase();
|
|
const itemTitleLower = item.item_title.toLowerCase();
|
|
return itemTitleLower.includes(orderTitleLower) || orderTitleLower.includes(itemTitleLower);
|
|
}
|
|
return false;
|
|
});
|
|
|
|
if (matchingItem?.item_title) {
|
|
return matchingItem.item_title;
|
|
}
|
|
|
|
return '未知商品';
|
|
};
|
|
|
|
// 从商品列表构建商品ID到商品名的映射
|
|
const buildItemNamesMap = () => {
|
|
const namesMap: Record<string, string> = {};
|
|
items.forEach(item => {
|
|
// 使用 item_id 作为键,商品标题作为值
|
|
if (item.item_id) {
|
|
namesMap[`${item.cookie_id}:${item.item_id}`] = item.item_title || item.item_id;
|
|
}
|
|
});
|
|
setItemNames(namesMap);
|
|
};
|
|
|
|
const buildItemNamesMapFrom = (itemsList: Item[]) => {
|
|
const namesMap: Record<string, string> = {};
|
|
itemsList.forEach(item => {
|
|
if (item.item_id) {
|
|
namesMap[`${item.cookie_id}:${item.item_id}`] = item.item_title || item.item_id;
|
|
}
|
|
});
|
|
setItemNames(namesMap);
|
|
};
|
|
|
|
useEffect(() => {
|
|
const timer = window.setTimeout(() => {
|
|
setPage(1);
|
|
setDebouncedSearch(searchText.trim());
|
|
}, 300);
|
|
return () => window.clearTimeout(timer);
|
|
}, [searchText]);
|
|
|
|
useEffect(() => {
|
|
loadOrders();
|
|
}, [accountFilter, filter, page, debouncedSearch]);
|
|
|
|
useEffect(() => {
|
|
Promise.all([getAccountDetails(), getItems()]).then(([accountList, itemsList]) => {
|
|
setAccounts(accountList);
|
|
setItems(itemsList);
|
|
buildItemNamesMapFrom(itemsList);
|
|
}).catch((e) => {
|
|
console.error('加载商品列表失败:', e);
|
|
});
|
|
}, []);
|
|
|
|
const accountMap = useMemo(
|
|
() => new Map(accounts.map(account => [account.id, account])),
|
|
[accounts],
|
|
);
|
|
|
|
const accountName = (cookieId: string) => {
|
|
const account = accountMap.get(cookieId);
|
|
const name = account?.remark || account?.nickname;
|
|
return name ? `${name} · ${cookieId.slice(0, 6)}` : `账号 ${cookieId.slice(0, 8)}`;
|
|
};
|
|
const accountNickname = (cookieId: string) => {
|
|
const account = accountMap.get(cookieId);
|
|
return account?.remark || account?.nickname || '未命名账号';
|
|
};
|
|
|
|
const handleSync = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const result = await syncOrders(accountFilter || undefined, filter);
|
|
await loadOrders();
|
|
if (result?.message) {
|
|
alert(result.message);
|
|
}
|
|
} catch (error: any) {
|
|
console.error('同步订单失败:', error);
|
|
alert(error?.message || '同步失败,请重试');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleShip = (id: string) => {
|
|
setShipOrderId(id);
|
|
setShipResult(null);
|
|
setShowShipModal(true);
|
|
};
|
|
|
|
const executeShip = async (mode: 'status_only' | 'full_delivery') => {
|
|
setShipLoading(true);
|
|
setShipResult(null);
|
|
try {
|
|
const res = await manualShipOrder([shipOrderId], mode);
|
|
const result = res?.results?.[0];
|
|
if (result?.success) {
|
|
setShipResult({ success: true, message: result.message });
|
|
loadOrders();
|
|
} else {
|
|
setShipResult({ success: false, message: result?.message || '发货失败' });
|
|
}
|
|
} catch (e: any) {
|
|
setShipResult({ success: false, message: e?.message || '请求失败' });
|
|
} finally {
|
|
setShipLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleViewDetail = (order: Order) => {
|
|
setSelectedOrder(order);
|
|
setShowDetailModal(true);
|
|
};
|
|
|
|
const handleEdit = (order: Order) => {
|
|
setEditingOrder({ ...order });
|
|
setShowEditModal(true);
|
|
};
|
|
|
|
const handleSaveEdit = async () => {
|
|
if (!editingOrder || !editingOrder.order_id) return;
|
|
try {
|
|
// 映射前端字段到后端期望的字段名
|
|
const updateData: Record<string, any> = {};
|
|
|
|
if (editingOrder.status !== undefined) {
|
|
updateData.order_status = editingOrder.status;
|
|
}
|
|
if (editingOrder.buyer_id !== undefined) {
|
|
updateData.buyer_id = editingOrder.buyer_id;
|
|
}
|
|
if (editingOrder.amount !== undefined) {
|
|
updateData.amount = editingOrder.amount;
|
|
}
|
|
if (editingOrder.receiver_name !== undefined) {
|
|
updateData.receiver_name = editingOrder.receiver_name;
|
|
}
|
|
if (editingOrder.receiver_phone !== undefined) {
|
|
updateData.receiver_phone = editingOrder.receiver_phone;
|
|
}
|
|
if (editingOrder.receiver_address !== undefined) {
|
|
updateData.receiver_address = editingOrder.receiver_address;
|
|
}
|
|
if (editingOrder.item_id !== undefined) {
|
|
updateData.item_id = editingOrder.item_id;
|
|
}
|
|
if (editingOrder.quantity !== undefined) {
|
|
updateData.quantity = editingOrder.quantity;
|
|
}
|
|
if (editingOrder.item_title !== undefined) {
|
|
updateData.item_title = editingOrder.item_title;
|
|
}
|
|
|
|
await updateOrder(editingOrder.order_id, updateData);
|
|
setShowEditModal(false);
|
|
setEditingOrder(null);
|
|
loadOrders();
|
|
} catch (error) {
|
|
console.error('更新订单失败:', error);
|
|
alert('更新失败,请重试');
|
|
}
|
|
};
|
|
|
|
const handleImportOrders = async () => {
|
|
if (!importFile) return;
|
|
setImporting(true);
|
|
try {
|
|
const formData = new FormData();
|
|
formData.append('file', importFile);
|
|
const result = normalizeOrderImportResult(await importOrders(formData));
|
|
setImportResult(result);
|
|
setImportFile(null);
|
|
await loadOrders();
|
|
if (result.failed_count === 0) {
|
|
setShowImportModal(false);
|
|
setImportResult(null);
|
|
alert(`订单导入成功,共 ${result.success_count} 条`);
|
|
}
|
|
} catch (error: any) {
|
|
alert(error?.message || '导入失败,请检查文件格式');
|
|
} finally {
|
|
setImporting(false);
|
|
}
|
|
};
|
|
|
|
const closeImportModal = () => {
|
|
if (importing) return;
|
|
setShowImportModal(false);
|
|
setImportFile(null);
|
|
setImportResult(null);
|
|
};
|
|
|
|
const handleSyncSingle = async (orderId: string) => {
|
|
setSyncingOrderId(orderId);
|
|
try {
|
|
const result = await syncSingleOrder(orderId);
|
|
if (result.success) {
|
|
await loadOrders();
|
|
} else {
|
|
alert(result.message || '同步失败');
|
|
}
|
|
} catch (error: any) {
|
|
console.error('同步订单失败:', error);
|
|
alert(error?.message || '同步失败,请重试');
|
|
} finally {
|
|
setSyncingOrderId(null);
|
|
}
|
|
};
|
|
|
|
const handleDelete = async (orderId: string) => {
|
|
if (!confirm('确认删除该订单吗?删除后无法恢复。')) return;
|
|
setDeletingOrderId(orderId);
|
|
try {
|
|
await deleteOrder(orderId);
|
|
if (orders.length === 1 && page > 1) {
|
|
setPage(current => current - 1);
|
|
} else {
|
|
await loadOrders();
|
|
}
|
|
} catch (error: any) {
|
|
console.error('删除订单失败:', error);
|
|
alert(error?.message || '删除失败,请重试');
|
|
await loadOrders();
|
|
} finally {
|
|
setDeletingOrderId(null);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-6 animate-fade-in">
|
|
<div className="flex flex-col md:flex-row justify-between md:items-end gap-4">
|
|
<div>
|
|
<h2 className="text-4xl font-extrabold text-gray-900 tracking-tight">订单中心</h2>
|
|
<p className="text-gray-500 mt-2 font-medium">查看所有闲鱼交易记录与状态。</p>
|
|
</div>
|
|
<div className="flex items-center gap-3">
|
|
<button onClick={loadOrders} className="p-3 rounded-2xl bg-white border border-gray-100 text-gray-600 hover:bg-gray-50 hover:text-black transition-colors shadow-sm">
|
|
<RefreshCw className={`w-5 h-5 ${loading ? 'animate-spin' : ''}`} />
|
|
</button>
|
|
<button
|
|
onClick={() => { setImportResult(null); setShowImportModal(true); }}
|
|
className="px-5 py-3 rounded-2xl font-bold bg-gray-900 text-white hover:bg-gray-800 transition-colors text-sm flex items-center gap-2 shadow-lg"
|
|
>
|
|
<Plus className="w-4 h-4" />
|
|
插入订单
|
|
</button>
|
|
<button onClick={handleSync} className="ios-btn-primary px-6 py-3 rounded-2xl font-bold shadow-lg shadow-blue-200 text-sm flex items-center gap-2">
|
|
<Truck className="w-5 h-5" />
|
|
一键同步订单
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="ios-card rounded-xl overflow-hidden shadow-lg border-0 bg-white">
|
|
{/* Toolbar */}
|
|
<div className="p-4 border-b border-gray-50 flex flex-col md:flex-row gap-4 justify-between items-center bg-[#FAFAFA]">
|
|
<div className="flex gap-1 p-1 bg-gray-200/50 rounded-xl overflow-x-auto max-w-full">
|
|
{[
|
|
{k:'all', v:'全部'},
|
|
{k:'processing', v:'处理中'},
|
|
{k:'shipped', v:'已发货'},
|
|
{k:'pending_ship', v:'待发货'},
|
|
{k:'completed', v:'已完成'},
|
|
{k:'cancelled', v:'已取消'},
|
|
{k:'refunding', v:'退款中'}
|
|
].map(opt => (
|
|
<button
|
|
key={opt.k}
|
|
onClick={() => { setFilter(opt.k); setPage(1); setSearchText(''); }}
|
|
className={`px-5 py-2 rounded-lg text-sm font-bold transition-all whitespace-nowrap ${filter === opt.k ? 'bg-white text-black shadow-sm' : 'text-gray-500 hover:text-gray-700'}`}
|
|
>
|
|
{opt.v}
|
|
</button>
|
|
))}
|
|
</div>
|
|
<div className="flex w-full md:w-auto flex-col sm:flex-row gap-3">
|
|
<div className="relative">
|
|
<UserIcon className="w-4 h-4 absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 pointer-events-none" />
|
|
<select
|
|
aria-label="按账号筛选订单"
|
|
value={accountFilter}
|
|
onChange={(event) => { setAccountFilter(event.target.value); setPage(1); }}
|
|
className="ios-input pl-10 pr-9 py-2.5 rounded-xl w-full sm:w-56 bg-white border-none shadow-sm"
|
|
>
|
|
<option value="">全部账号</option>
|
|
{accounts.map(account => (
|
|
<option key={account.id} value={account.id}>{accountName(account.id)}</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
<div className="relative group">
|
|
<Search className="w-4 h-4 absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 group-focus-within:text-[#0094f7] transition-colors" />
|
|
<input
|
|
type="text"
|
|
placeholder="搜索订单号/商品/买家..."
|
|
value={searchText}
|
|
onChange={(e) => { setSearchText(e.target.value); setPage(1); }}
|
|
className="ios-input pl-10 pr-4 py-2.5 rounded-xl w-64 bg-white border-none shadow-sm focus:ring-0"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Table */}
|
|
<div className="overflow-x-auto min-h-[400px]">
|
|
<table className="w-full text-left border-collapse table-fixed">
|
|
<thead>
|
|
<tr className="bg-white text-gray-400 text-xs font-bold uppercase tracking-wider border-b border-gray-50">
|
|
<th className="px-6 py-5" style={{width: '28%'}}>订单信息</th>
|
|
<th className="px-6 py-5" style={{width: '26%'}}>买家信息</th>
|
|
<th className="px-6 py-5" style={{width: '11%'}}>实付金额</th>
|
|
<th className="px-6 py-5" style={{width: '13%'}}>当前状态</th>
|
|
<th className="px-6 py-5 text-right" style={{width: '22%'}}>操作</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-50">
|
|
{orders.map((order) => (
|
|
<tr key={order.id} className="hover:bg-[#FFFDE7]/50 transition-colors group">
|
|
<td className="px-6 py-5">
|
|
<div className="flex items-center gap-5">
|
|
<div className="w-14 h-14 rounded-xl bg-gray-100 overflow-hidden shadow-sm border border-gray-100 flex-shrink-0">
|
|
{order.item_image ? (
|
|
<img src={order.item_image} alt="" className="w-full h-full object-cover" />
|
|
) : (
|
|
<div className="w-full h-full flex items-center justify-center text-gray-300"><PackageCheck /></div>
|
|
)}
|
|
</div>
|
|
<div className="min-w-0">
|
|
<div className="font-bold text-gray-900 line-clamp-1 text-sm">
|
|
{getItemNameById(order.cookie_id, order.item_id, order.item_title)}
|
|
</div>
|
|
<div className="mt-1 flex w-fit min-w-0 max-w-full items-center gap-1 rounded-md bg-blue-50 px-2 py-1 text-[10px] font-extrabold text-blue-700" title={accountNickname(order.cookie_id)}>
|
|
<UserIcon className="h-3 w-3 shrink-0" />
|
|
<span className="min-w-0 truncate whitespace-nowrap">{accountNickname(order.cookie_id)}</span>
|
|
</div>
|
|
<div className="text-xs text-gray-500 mt-1 font-medium">订单ID: {order.order_id}</div>
|
|
<div className="text-xs text-gray-400 mt-0.5">数量: {order.quantity} • {formatLocalDateTime(order.created_at)}</div>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
<td className="px-6 py-5">
|
|
<div className="flex flex-col gap-1">
|
|
<div className="text-xs text-gray-500">买家ID</div>
|
|
<div className="text-sm font-bold text-gray-800">{order.buyer_id}</div>
|
|
{order.receiver_name && (
|
|
<>
|
|
<div className="text-xs text-gray-500">收货人</div>
|
|
<div className="text-xs text-gray-600">{order.receiver_name}</div>
|
|
</>
|
|
)}
|
|
{order.receiver_phone && (
|
|
<>
|
|
<div className="text-xs text-gray-500">联系电话</div>
|
|
<div className="text-xs text-gray-600 font-mono">{order.receiver_phone}</div>
|
|
</>
|
|
)}
|
|
{order.receiver_address && (
|
|
<>
|
|
<div className="text-xs text-gray-500">收货地址</div>
|
|
<div className="text-xs text-gray-600 line-clamp-1">{order.receiver_address}</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
</td>
|
|
<td className="px-6 py-5 text-base font-extrabold text-gray-900 font-feature-settings-tnum">
|
|
{order.amount ? `¥${order.amount}` : <span className="text-xs text-amber-600 font-bold">待获取</span>}
|
|
</td>
|
|
<td className="px-6 py-5">
|
|
<StatusBadge status={order.status} />
|
|
</td>
|
|
<td className="px-6 py-5 text-right">
|
|
{order.status === 'pending_ship' && (
|
|
<button
|
|
onClick={() => handleShip(order.order_id)}
|
|
className="mr-2 text-white bg-black hover:bg-gray-800 shadow-lg shadow-gray-200 text-xs font-bold px-3 py-2 rounded-xl transition-all active:scale-95"
|
|
>
|
|
立即发货
|
|
</button>
|
|
)}
|
|
<a
|
|
href={`https://www.goofish.com/order-detail?orderId=${order.order_id}&role=seller`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="mr-2 inline-flex text-gray-400 hover:text-blue-600 p-2 rounded-xl hover:bg-blue-50 transition-colors"
|
|
title="查看闲鱼详情"
|
|
>
|
|
<ExternalLink className="w-4 h-4" />
|
|
</a>
|
|
<button
|
|
onClick={() => handleViewDetail(order)}
|
|
className="mr-2 text-gray-400 hover:text-blue-600 p-2 rounded-xl hover:bg-blue-50 transition-colors"
|
|
title="查看详情"
|
|
>
|
|
<Eye className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
onClick={() => handleEdit(order)}
|
|
className="mr-2 text-gray-400 hover:text-black p-2 rounded-xl hover:bg-gray-100 transition-colors"
|
|
title="编辑订单"
|
|
>
|
|
<Edit className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
onClick={() => handleSyncSingle(order.order_id)}
|
|
disabled={syncingOrderId === order.order_id}
|
|
className="mr-2 text-gray-400 hover:text-green-600 p-2 rounded-xl hover:bg-green-50 transition-colors disabled:opacity-50"
|
|
title="同步订单"
|
|
>
|
|
<RefreshCw className={`w-4 h-4 ${syncingOrderId === order.order_id ? 'animate-spin' : ''}`} />
|
|
</button>
|
|
<button
|
|
onClick={() => handleDelete(order.order_id)}
|
|
disabled={deletingOrderId === order.order_id}
|
|
className="text-gray-400 hover:text-red-500 p-2 rounded-xl hover:bg-red-50 transition-colors disabled:opacity-50"
|
|
title="删除订单"
|
|
>
|
|
<Trash2 className="w-4 h-4" />
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
{/* Pagination */}
|
|
<div className="p-4 border-t border-gray-50 flex items-center justify-between bg-white">
|
|
<div className="text-sm text-gray-500 font-medium pl-2">
|
|
第 {page} 页 / 共 {totalPages} 页
|
|
</div>
|
|
<div className="flex gap-2">
|
|
<button
|
|
disabled={page <= 1}
|
|
onClick={() => setPage(p => p - 1)}
|
|
className="p-2.5 rounded-xl bg-gray-50 hover:bg-gray-100 disabled:opacity-50 disabled:cursor-not-allowed text-gray-600 transition-colors"
|
|
>
|
|
<ChevronLeft className="w-5 h-5" />
|
|
</button>
|
|
<button
|
|
disabled={page >= totalPages}
|
|
onClick={() => setPage(p => p + 1)}
|
|
className="p-2.5 rounded-xl bg-gray-50 hover:bg-gray-100 disabled:opacity-50 disabled:cursor-not-allowed text-gray-600 transition-colors"
|
|
>
|
|
<ChevronRight className="w-5 h-5" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 订单详情弹窗 - 使用 Portal */}
|
|
{showDetailModal && selectedOrder && createPortal(
|
|
<div className="modal-overlay-centered">
|
|
<div className="modal-container">
|
|
<div className="modal-header">
|
|
<div className="flex items-center justify-between w-full">
|
|
<h3 className="text-2xl font-extrabold text-gray-900">订单详情</h3>
|
|
<button
|
|
onClick={() => setShowDetailModal(false)}
|
|
className="p-2 bg-gray-100 rounded-full hover:bg-gray-200 transition-colors"
|
|
>
|
|
<X className="w-5 h-5 text-gray-600" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="modal-body space-y-6">
|
|
{/* Order Info */}
|
|
<div className="space-y-4">
|
|
<h4 className="text-lg font-bold text-gray-800">订单信息</h4>
|
|
<div className="grid grid-cols-2 gap-4 p-4 bg-gray-50 rounded-xl">
|
|
<div>
|
|
<div className="text-xs text-gray-500 mb-1">订单号</div>
|
|
<div className="font-mono text-sm font-bold text-gray-900">{selectedOrder.order_id}</div>
|
|
</div>
|
|
<div>
|
|
<div className="text-xs text-gray-500 mb-1">所属账号</div>
|
|
<div className="truncate whitespace-nowrap text-sm font-bold text-blue-700" title={accountNickname(selectedOrder.cookie_id)}>{accountNickname(selectedOrder.cookie_id)}</div>
|
|
</div>
|
|
<div>
|
|
<div className="text-xs text-gray-500 mb-1">状态</div>
|
|
<StatusBadge status={selectedOrder.status} />
|
|
</div>
|
|
<div>
|
|
<div className="text-xs text-gray-500 mb-1">实付金额</div>
|
|
<div className="text-lg font-extrabold text-gray-900">{selectedOrder.amount ? `¥${selectedOrder.amount}` : '待获取'}</div>
|
|
</div>
|
|
<div>
|
|
<div className="text-xs text-gray-500 mb-1">数量</div>
|
|
<div className="font-bold text-gray-900">{selectedOrder.quantity}</div>
|
|
</div>
|
|
<div className="col-span-2">
|
|
<div className="text-xs text-gray-500 mb-1">创建时间</div>
|
|
<div className="text-sm font-medium text-gray-700">{formatLocalDateTime(selectedOrder.created_at)}</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Item Info */}
|
|
<div className="space-y-4">
|
|
<h4 className="text-lg font-bold text-gray-800">商品信息</h4>
|
|
<div className="p-4 bg-gray-50 rounded-xl flex items-center gap-4">
|
|
{selectedOrder.item_image && (
|
|
<img src={selectedOrder.item_image} alt="" className="w-20 h-20 rounded-xl object-cover border border-gray-200" />
|
|
)}
|
|
<div className="flex-1">
|
|
<div className="font-bold text-gray-900 mb-1">
|
|
{getItemNameById(selectedOrder.cookie_id, selectedOrder.item_id, selectedOrder.item_title)}
|
|
</div>
|
|
<div className="text-sm text-gray-500">商品ID: {selectedOrder.item_id}</div>
|
|
{selectedOrder.item_price && (
|
|
<div className="text-sm text-gray-500 mt-1">标价: ¥{selectedOrder.item_price}</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Buyer Info */}
|
|
<div className="space-y-4">
|
|
<h4 className="text-lg font-bold text-gray-800">买家信息</h4>
|
|
<div className="p-4 bg-gray-50 rounded-xl space-y-3">
|
|
<div>
|
|
<div className="text-xs text-gray-500 mb-1">买家ID</div>
|
|
<div className="font-bold text-gray-900">{selectedOrder.buyer_id}</div>
|
|
</div>
|
|
{selectedOrder.receiver_name && (
|
|
<div>
|
|
<div className="text-xs text-gray-500 mb-1">收货人</div>
|
|
<div className="font-medium text-gray-700">{selectedOrder.receiver_name}</div>
|
|
</div>
|
|
)}
|
|
{selectedOrder.receiver_phone && (
|
|
<div>
|
|
<div className="text-xs text-gray-500 mb-1">联系电话</div>
|
|
<div className="font-mono text-sm text-gray-700">{selectedOrder.receiver_phone}</div>
|
|
</div>
|
|
)}
|
|
{selectedOrder.receiver_address && (
|
|
<div>
|
|
<div className="text-xs text-gray-500 mb-1">收货地址</div>
|
|
<div className="text-sm text-gray-700">{selectedOrder.receiver_address}</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="modal-footer">
|
|
<div className="flex gap-3 w-full">
|
|
<button
|
|
onClick={() => setShowDetailModal(false)}
|
|
className="flex-1 px-6 py-3 rounded-xl bg-gray-100 hover:bg-gray-200 text-gray-800 font-bold transition-colors"
|
|
>
|
|
关闭
|
|
</button>
|
|
{selectedOrder.status === 'pending_ship' && (
|
|
<button
|
|
onClick={() => {
|
|
setShowDetailModal(false);
|
|
handleShip(selectedOrder.order_id);
|
|
}}
|
|
className="flex-1 px-6 py-3 rounded-xl ios-btn-primary font-bold shadow-lg shadow-blue-200"
|
|
>
|
|
立即发货
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>,
|
|
document.body
|
|
)}
|
|
|
|
{/* Import Modal - 使用 Portal */}
|
|
{showImportModal && createPortal(
|
|
<div className="modal-overlay-centered">
|
|
<div className="modal-container">
|
|
<div className="modal-header">
|
|
<div className="flex items-center justify-between w-full">
|
|
<h3 className="text-2xl font-extrabold text-gray-900">插入订单</h3>
|
|
<button
|
|
onClick={closeImportModal}
|
|
className="p-2 bg-gray-100 rounded-full hover:bg-gray-200 transition-colors"
|
|
>
|
|
<X className="w-5 h-5 text-gray-600" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="modal-body space-y-5">
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-2">选择Excel文件</label>
|
|
<input
|
|
type="file"
|
|
accept=".xlsx,.csv,.tsv,.json"
|
|
onChange={(e) => setImportFile(e.target.files?.[0] || null)}
|
|
className="w-full ios-input px-4 py-3 rounded-xl text-sm"
|
|
/>
|
|
<p className="text-xs text-gray-500 mt-2">支持 .xlsx、.csv、.tsv、.json 格式</p>
|
|
</div>
|
|
|
|
{importFile && (
|
|
<div className="p-3 bg-blue-50 rounded-xl">
|
|
<div className="flex items-center gap-2">
|
|
<Upload className="w-4 h-4 text-blue-600" />
|
|
<span className="text-sm font-medium text-blue-900">{importFile.name}</span>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{importResult && importResult.failed_count > 0 && (
|
|
<div className="space-y-3">
|
|
<div className="rounded-xl bg-amber-50 border border-amber-100 p-3 text-sm font-bold text-amber-800">
|
|
导入完成:成功 {importResult.success_count} 条,失败 {importResult.failed_count} 条
|
|
</div>
|
|
<div className="max-h-64 overflow-y-auto rounded-xl border border-gray-100">
|
|
<table className="w-full text-left text-xs">
|
|
<thead className="sticky top-0 bg-gray-50 text-gray-500">
|
|
<tr><th className="px-3 py-2">订单ID</th><th className="px-3 py-2">失败原因</th></tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-100">
|
|
{failedOrderImportRows(importResult).map((row, index) => (
|
|
<tr key={`${row.order_id}-${index}`}>
|
|
<td className="px-3 py-2 font-mono">{row.order_id}</td>
|
|
<td className="px-3 py-2 text-red-600">{row.message || '导入失败'}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="modal-footer">
|
|
<div className="flex gap-3 w-full">
|
|
<button
|
|
onClick={closeImportModal}
|
|
disabled={importing}
|
|
className="flex-1 px-6 py-3 rounded-xl bg-gray-100 hover:bg-gray-200 text-gray-800 font-bold transition-colors"
|
|
>
|
|
取消
|
|
</button>
|
|
<button
|
|
onClick={handleImportOrders}
|
|
disabled={!importFile || importing}
|
|
className="flex-1 px-6 py-3 rounded-xl ios-btn-primary font-bold shadow-lg shadow-blue-200 disabled:opacity-50"
|
|
>
|
|
{importing ? '正在导入…' : '导入订单'}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>,
|
|
document.body
|
|
)}
|
|
|
|
{/* Ship Modal - 发货方式选择 */}
|
|
{showShipModal && createPortal(
|
|
<div className="modal-overlay-centered">
|
|
<div className="modal-container" style={{ maxWidth: '480px' }}>
|
|
<div className="modal-header">
|
|
<div className="flex items-center justify-between w-full">
|
|
<h3 className="text-2xl font-extrabold text-gray-900">立即发货</h3>
|
|
<button
|
|
onClick={() => { setShowShipModal(false); setShipResult(null); }}
|
|
className="p-2 bg-gray-100 rounded-full hover:bg-gray-200 transition-colors"
|
|
>
|
|
<X className="w-5 h-5 text-gray-600" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="modal-body space-y-4">
|
|
<p className="text-sm text-gray-600">请选择发货方式:</p>
|
|
|
|
{/* 选项A: 仅修改发货状态 */}
|
|
<button
|
|
onClick={() => executeShip('status_only')}
|
|
disabled={shipLoading}
|
|
className="w-full text-left p-4 rounded-xl border-2 border-gray-200 hover:border-gray-400 hover:bg-gray-50 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
|
>
|
|
<div className="flex items-start gap-3">
|
|
<div className="w-10 h-10 rounded-xl bg-blue-100 flex items-center justify-center flex-shrink-0 mt-0.5">
|
|
<Truck className="w-5 h-5 text-blue-600" />
|
|
</div>
|
|
<div>
|
|
<div className="font-bold text-gray-900 text-sm">仅修改闲鱼发货状态</div>
|
|
<div className="text-xs text-gray-500 mt-1 leading-relaxed">
|
|
不实际扣除或发送卡券,仅在闲鱼平台将订单标记为"已发货"。
|
|
适用于已经给客户发过货、只是忘记在闲鱼修改状态的情况。
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</button>
|
|
|
|
{/* 选项B: 完整发货流程 */}
|
|
<button
|
|
onClick={() => executeShip('full_delivery')}
|
|
disabled={shipLoading}
|
|
className="w-full text-left p-4 rounded-xl border-2 border-gray-200 hover:border-[#0094f7] hover:bg-blue-50 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
|
>
|
|
<div className="flex items-start gap-3">
|
|
<div className="w-10 h-10 rounded-xl bg-blue-100 flex items-center justify-center flex-shrink-0 mt-0.5">
|
|
<PackageCheck className="w-5 h-5 text-blue-700" />
|
|
</div>
|
|
<div>
|
|
<div className="font-bold text-gray-900 text-sm">完整发货(匹配卡券并发送)</div>
|
|
<div className="text-xs text-gray-500 mt-1 leading-relaxed">
|
|
自动匹配发货规则、获取卡券、发送卡券信息给买家,并修改发货状态。
|
|
适用于订单既没有发送卡券给买家、也没有修改发货状态的情况。
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</button>
|
|
|
|
{/* 加载状态 */}
|
|
{shipLoading && (
|
|
<div className="flex items-center justify-center gap-2 py-3">
|
|
<RefreshCw className="w-4 h-4 animate-spin text-gray-500" />
|
|
<span className="text-sm text-gray-500">正在处理中...</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* 结果显示 */}
|
|
{shipResult && (
|
|
<div className={`p-3 rounded-xl text-sm ${shipResult.success ? 'bg-green-50 text-green-800' : 'bg-red-50 text-red-800'}`}>
|
|
{shipResult.success ? '✓ ' : '✗ '}{shipResult.message}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="modal-footer">
|
|
<button
|
|
onClick={() => { setShowShipModal(false); setShipResult(null); }}
|
|
className="w-full px-6 py-3 rounded-xl bg-gray-100 hover:bg-gray-200 text-gray-800 font-bold transition-colors"
|
|
>
|
|
{shipResult?.success ? '完成' : '取消'}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>,
|
|
document.body
|
|
)}
|
|
|
|
{/* Edit Modal - 使用 Portal */}
|
|
{showEditModal && editingOrder && createPortal(
|
|
<div className="modal-overlay-centered">
|
|
<div className="modal-container">
|
|
<div className="modal-header">
|
|
<div className="flex items-center justify-between w-full">
|
|
<h3 className="text-2xl font-extrabold text-gray-900">编辑订单</h3>
|
|
<button
|
|
onClick={() => setShowEditModal(false)}
|
|
className="p-2 bg-gray-100 rounded-full hover:bg-gray-200 transition-colors"
|
|
>
|
|
<X className="w-5 h-5 text-gray-600" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="modal-body space-y-5">
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-2">订单号</label>
|
|
<input
|
|
type="text"
|
|
value={editingOrder.order_id}
|
|
disabled
|
|
className="w-full ios-input px-4 py-3 rounded-xl bg-gray-50 text-gray-500"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-2">订单状态</label>
|
|
<select
|
|
value={editingOrder.status}
|
|
onChange={(e) => setEditingOrder({ ...editingOrder, status: e.target.value as OrderStatus })}
|
|
className="w-full ios-input px-4 py-3 rounded-xl"
|
|
>
|
|
<option value="processing">处理中</option>
|
|
<option value="pending_ship">待发货</option>
|
|
<option value="shipped">已发货</option>
|
|
<option value="completed">已完成</option>
|
|
<option value="cancelled">已取消</option>
|
|
<option value="refunding">退款中</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-2">买家ID</label>
|
|
<input
|
|
type="text"
|
|
value={editingOrder.buyer_id}
|
|
onChange={(e) => setEditingOrder({ ...editingOrder, buyer_id: e.target.value })}
|
|
className="w-full ios-input px-4 py-3 rounded-xl"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-2">实付金额</label>
|
|
<input
|
|
type="number"
|
|
value={editingOrder.amount}
|
|
onChange={(e) => setEditingOrder({ ...editingOrder, amount: e.target.value })}
|
|
className="w-full ios-input px-4 py-3 rounded-xl"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-2">收货人</label>
|
|
<input
|
|
type="text"
|
|
value={editingOrder.receiver_name || ''}
|
|
onChange={(e) => setEditingOrder({ ...editingOrder, receiver_name: e.target.value })}
|
|
className="w-full ios-input px-4 py-3 rounded-xl"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-2">联系电话</label>
|
|
<input
|
|
type="text"
|
|
value={editingOrder.receiver_phone || ''}
|
|
onChange={(e) => setEditingOrder({ ...editingOrder, receiver_phone: e.target.value })}
|
|
className="w-full ios-input px-4 py-3 rounded-xl"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-2">收货地址</label>
|
|
<textarea
|
|
value={editingOrder.receiver_address || ''}
|
|
onChange={(e) => setEditingOrder({ ...editingOrder, receiver_address: e.target.value })}
|
|
rows={2}
|
|
className="w-full ios-input px-4 py-3 rounded-xl resize-none"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-2">商品标题</label>
|
|
<input
|
|
type="text"
|
|
value={editingOrder.item_title || ''}
|
|
onChange={(e) => setEditingOrder({ ...editingOrder, item_title: e.target.value })}
|
|
className="w-full ios-input px-4 py-3 rounded-xl"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="modal-footer">
|
|
<div className="flex gap-3 w-full">
|
|
<button
|
|
onClick={() => setShowEditModal(false)}
|
|
className="flex-1 px-6 py-3 rounded-xl font-bold bg-gray-100 text-gray-700 hover:bg-gray-200 transition-colors"
|
|
>
|
|
取消
|
|
</button>
|
|
<button
|
|
onClick={handleSaveEdit}
|
|
className="flex-1 ios-btn-primary px-6 py-3 rounded-xl font-bold flex items-center justify-center gap-2"
|
|
>
|
|
<Save className="w-4 h-4" />
|
|
保存更改
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>,
|
|
document.body
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default OrderList;
|