feat(账单导入): 预览确认导入流程,去除账单名称输入框
- 上传先解析预览,弹框显示记录数+前5条样本,确认后入库 - 取消时删临时文件,不写入数据库 - 去掉账单名称手动输入,用来源类型自动命名 - 修复中文文件名 Latin-1 编码乱码 - 所有 alert/confirm 替换为 Toast 通知和 Modal 确认弹框 - 去除导入时自动分类逻辑
This commit is contained in:
+307
-136
@@ -6,36 +6,119 @@ import {
|
||||
AlertCircle,
|
||||
Trash2,
|
||||
RefreshCw,
|
||||
X,
|
||||
Info,
|
||||
} from "lucide-react";
|
||||
import { uploadFile, getSources, deleteSource } from "../hooks/useApi";
|
||||
import { getSources, deleteSource } from "../hooks/useApi";
|
||||
|
||||
const SOURCE_TYPES = [
|
||||
{ value: "alipay", label: "支付宝", icon: "🔵", desc: "支付宝账单 CSV 文件" },
|
||||
{ value: "wechat", label: "微信支付", icon: "🟢", desc: "微信支付账单 XLSX 文件" },
|
||||
{ value: "bank_debit", label: "浦发银行", icon: "🏦", desc: "浦发银行流水 XLS 文件" },
|
||||
{ value: "abc", label: "农业银行", icon: "🏦", desc: "农业银行流水 XLSX 文件" },
|
||||
{
|
||||
value: "alipay",
|
||||
label: "支付宝",
|
||||
icon: "\uD83D\uDD35",
|
||||
desc: "支付宝账单 CSV 文件",
|
||||
},
|
||||
{
|
||||
value: "wechat",
|
||||
label: "微信支付",
|
||||
icon: "\uD83D\uDFE2",
|
||||
desc: "微信支付账单 XLSX 文件",
|
||||
},
|
||||
{
|
||||
value: "bank_debit",
|
||||
label: "浦发银行",
|
||||
icon: "\uD83C\uDFE6",
|
||||
desc: "浦发银行流水 XLS 文件",
|
||||
},
|
||||
{
|
||||
value: "abc",
|
||||
label: "农业银行",
|
||||
icon: "\uD83C\uDFE6",
|
||||
desc: "农业银行流水 XLSX 文件",
|
||||
},
|
||||
];
|
||||
|
||||
const ALLOWED_EXTS = ["csv", "txt", "xlsx", "xls"];
|
||||
|
||||
// Toast 组件
|
||||
function Toast({ message, type, onClose }) {
|
||||
useEffect(() => {
|
||||
const t = setTimeout(onClose, 3000);
|
||||
return () => clearTimeout(t);
|
||||
}, [onClose]);
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
position: "fixed",
|
||||
top: 24,
|
||||
right: 24,
|
||||
zIndex: 3000,
|
||||
padding: "14px 20px",
|
||||
borderRadius: 10,
|
||||
background:
|
||||
type === "success"
|
||||
? "#10b981"
|
||||
: type === "error"
|
||||
? "#ef4444"
|
||||
: "#4f46e5",
|
||||
color: "#fff",
|
||||
fontSize: 14,
|
||||
fontWeight: 500,
|
||||
boxShadow: "0 4px 12px rgba(0,0,0,0.2)",
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 10,
|
||||
maxWidth: 400,
|
||||
animation: "slideIn 0.3s ease",
|
||||
}}
|
||||
>
|
||||
{type === "success" ? (
|
||||
<CheckCircle size={18} />
|
||||
) : type === "error" ? (
|
||||
<AlertCircle size={18} />
|
||||
) : (
|
||||
<Info size={18} />
|
||||
)}
|
||||
<span style={{ flex: 1 }}>{message}</span>
|
||||
<button
|
||||
onClick={onClose}
|
||||
style={{
|
||||
background: "none",
|
||||
border: "none",
|
||||
color: "#fff",
|
||||
cursor: "pointer",
|
||||
opacity: 0.7,
|
||||
}}
|
||||
>
|
||||
<X size={16} />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Import() {
|
||||
const [sourceType, setSourceType] = useState("alipay");
|
||||
const [sourceName, setSourceName] = useState("");
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [uploadResult, setUploadResult] = useState(null);
|
||||
const [sources, setSources] = useState([]);
|
||||
const [error, setError] = useState("");
|
||||
const [dragOver, setDragOver] = useState(false);
|
||||
const [preview, setPreview] = useState(null);
|
||||
const [confirming, setConfirming] = useState(false);
|
||||
const [toast, setToast] = useState(null);
|
||||
const [deleteTarget, setDeleteTarget] = useState(null);
|
||||
const fileInputRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
loadSources();
|
||||
}, []);
|
||||
|
||||
function showToast(message, type) {
|
||||
setToast({ message, type, key: Date.now() });
|
||||
}
|
||||
|
||||
async function loadSources() {
|
||||
try {
|
||||
const data = await getSources();
|
||||
setSources(data);
|
||||
setSources(await getSources());
|
||||
} catch (err) {}
|
||||
}
|
||||
|
||||
@@ -48,58 +131,105 @@ export default function Import() {
|
||||
}
|
||||
setError("");
|
||||
setUploading(true);
|
||||
setUploadResult(null);
|
||||
setPreview(null);
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
formData.append("source_type", sourceType);
|
||||
formData.append(
|
||||
"source_name",
|
||||
sourceName ||
|
||||
`${SOURCE_TYPES.find((s) => s.value === sourceType)?.label}_${new Date().toLocaleDateString()}`,
|
||||
);
|
||||
const result = await uploadFile(formData);
|
||||
setUploadResult(result);
|
||||
await loadSources();
|
||||
formData.append("source_name", sourceType);
|
||||
const res = await fetch("/api/upload", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || "解析失败");
|
||||
setPreview(data);
|
||||
} catch (err) {
|
||||
setError(err.response?.data?.error || err.message || "上传失败");
|
||||
setError(err.message || "上传失败");
|
||||
}
|
||||
setUploading(false);
|
||||
}
|
||||
|
||||
async function handleDeleteSource(id) {
|
||||
if (!window.confirm("确定要删除这个导入来源及其所有交易记录吗?")) return;
|
||||
async function handleConfirm() {
|
||||
if (!preview?.tempId) return;
|
||||
setConfirming(true);
|
||||
try {
|
||||
await deleteSource(id);
|
||||
await loadSources();
|
||||
} catch (err) {}
|
||||
const res = await fetch("/api/upload/confirm", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ tempId: preview.tempId }),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || "导入失败");
|
||||
setPreview(null);
|
||||
showToast(
|
||||
`导入成功!写入 ${data.inserted} 条${data.skipped > 0 ? ",跳过 " + data.skipped + " 条重复" : ""}`,
|
||||
"success",
|
||||
);
|
||||
loadSources();
|
||||
} catch (err) {
|
||||
showToast("导入失败: " + err.message, "error");
|
||||
}
|
||||
setConfirming(false);
|
||||
}
|
||||
|
||||
async function handleCancel() {
|
||||
if (preview?.tempId) {
|
||||
await fetch("/api/upload/cancel", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ tempId: preview.tempId }),
|
||||
});
|
||||
}
|
||||
setPreview(null);
|
||||
}
|
||||
|
||||
async function handleDeleteSource(id) {
|
||||
setDeleteTarget(id);
|
||||
}
|
||||
|
||||
async function confirmDelete() {
|
||||
if (!deleteTarget) return;
|
||||
try {
|
||||
await deleteSource(deleteTarget);
|
||||
showToast("已删除", "success");
|
||||
loadSources();
|
||||
} catch (err) {
|
||||
showToast("删除失败", "error");
|
||||
}
|
||||
setDeleteTarget(null);
|
||||
}
|
||||
|
||||
function handleDrop(e) {
|
||||
e.preventDefault();
|
||||
setDragOver(false);
|
||||
const file = e.dataTransfer.files[0];
|
||||
handleFile(file);
|
||||
handleFile(e.dataTransfer.files[0]);
|
||||
}
|
||||
|
||||
const SOURCE_LABEL_MAP = {
|
||||
alipay: "支付宝",
|
||||
wechat: "微信支付",
|
||||
bank_debit: "银行借记卡",
|
||||
bank_credit: "银行贷记卡",
|
||||
credit_card: "信用卡",
|
||||
bank_debit: "浦发银行",
|
||||
abc: "农业银行",
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
{/* Toast */}
|
||||
{toast && (
|
||||
<Toast
|
||||
key={toast.key}
|
||||
message={toast.message}
|
||||
type={toast.type}
|
||||
onClose={() => setToast(null)}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="page-header">
|
||||
<h2>📥 导入账单</h2>
|
||||
<p>
|
||||
上传支付宝、微信、银行卡等账单文件(CSV/TXT/XLSX/XLS),系统自动解析并分类
|
||||
</p>
|
||||
<p>上传账单文件,确认后导入数据库</p>
|
||||
</div>
|
||||
|
||||
{/* 类型选择 */}
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3>选择账单类型</h3>
|
||||
@@ -117,10 +247,10 @@ export default function Import() {
|
||||
key={st.value}
|
||||
onClick={() => setSourceType(st.value)}
|
||||
style={{
|
||||
padding: "16px",
|
||||
padding: 16,
|
||||
cursor: "pointer",
|
||||
textAlign: "center",
|
||||
borderRadius: "12px",
|
||||
borderRadius: 12,
|
||||
border: `2px solid ${sourceType === st.value ? "var(--primary)" : "var(--border)"}`,
|
||||
background:
|
||||
sourceType === st.value ? "var(--primary-bg)" : "white",
|
||||
@@ -142,18 +272,6 @@ export default function Import() {
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label>账单名称(可选)</label>
|
||||
<input
|
||||
type="text"
|
||||
className="form-input"
|
||||
placeholder="如:工商银行工资卡、招商银行信用卡..."
|
||||
value={sourceName}
|
||||
onChange={(e) => setSourceName(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 上传区域 */}
|
||||
<div
|
||||
className={`upload-zone ${dragOver ? "drag-over" : ""}`}
|
||||
onDragOver={(e) => {
|
||||
@@ -207,97 +325,8 @@ export default function Import() {
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{uploadResult && (
|
||||
<div
|
||||
style={{
|
||||
marginTop: 16,
|
||||
padding: "16px",
|
||||
background: "#f0fdf4",
|
||||
borderRadius: 8,
|
||||
border: "1px solid #bbf7d0",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 8,
|
||||
marginBottom: 8,
|
||||
}}
|
||||
>
|
||||
<CheckCircle size={20} color="var(--success)" />
|
||||
<span style={{ fontWeight: 600, color: "var(--success)" }}>
|
||||
导入成功!
|
||||
</span>
|
||||
</div>
|
||||
<div style={{ fontSize: 13, color: "var(--text-secondary)" }}>
|
||||
共导入 <strong>{uploadResult.record_count}</strong> 笔, 写入{" "}
|
||||
<strong>
|
||||
{uploadResult.inserted ?? uploadResult.record_count}
|
||||
</strong>{" "}
|
||||
笔
|
||||
{uploadResult.skipped > 0 && (
|
||||
<span>(跳过 {uploadResult.skipped} 笔重复)</span>
|
||||
)}
|
||||
, 分类 <strong>{uploadResult.classified || 0}</strong> 笔 (
|
||||
{uploadResult.classify_source === "llm"
|
||||
? "大模型"
|
||||
: uploadResult.classify_source === "rules"
|
||||
? "规则匹配"
|
||||
: "待分类"}
|
||||
)
|
||||
</div>
|
||||
{uploadResult.sample?.length > 0 && (
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<div style={{ fontSize: 12, fontWeight: 600, marginBottom: 6 }}>
|
||||
示例数据:
|
||||
</div>
|
||||
<div className="table-container">
|
||||
<table style={{ fontSize: 12 }}>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>日期</th>
|
||||
<th>对方</th>
|
||||
<th>描述</th>
|
||||
<th>金额</th>
|
||||
<th>方向</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{uploadResult.sample.map((s, i) => (
|
||||
<tr key={i}>
|
||||
<td>{s.transaction_date}</td>
|
||||
<td>{s.counterparty}</td>
|
||||
<td>{s.description}</td>
|
||||
<td
|
||||
className={
|
||||
s.direction === "income"
|
||||
? "stat-value income"
|
||||
: "stat-value expense"
|
||||
}
|
||||
>
|
||||
{s.amount.toFixed(2)}
|
||||
</td>
|
||||
<td>
|
||||
<span
|
||||
className={`tag ${s.direction === "income" ? "tag-income" : "tag-expense"}`}
|
||||
>
|
||||
{s.direction === "income" ? "收入" : "支出"}
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 已导入列表 */}
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3>📋 已导入的账单</h3>
|
||||
@@ -359,6 +388,148 @@ export default function Import() {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 确认导入弹框 */}
|
||||
{preview && (
|
||||
<div className="modal-overlay">
|
||||
<div className="modal" style={{ maxWidth: 480 }}>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
alignItems: "center",
|
||||
marginBottom: 16,
|
||||
}}
|
||||
>
|
||||
<h3>确认导入</h3>
|
||||
<button
|
||||
onClick={handleCancel}
|
||||
style={{
|
||||
background: "none",
|
||||
border: "none",
|
||||
cursor: "pointer",
|
||||
}}
|
||||
>
|
||||
<X size={20} />
|
||||
</button>
|
||||
</div>
|
||||
<div style={{ marginBottom: 16, lineHeight: 1.8 }}>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 24,
|
||||
fontWeight: 700,
|
||||
color: "var(--primary)",
|
||||
textAlign: "center",
|
||||
marginBottom: 8,
|
||||
}}
|
||||
>
|
||||
{preview.record_count} 条记录
|
||||
</div>
|
||||
<div
|
||||
style={{
|
||||
color: "var(--text-secondary)",
|
||||
textAlign: "center",
|
||||
fontSize: 13,
|
||||
}}
|
||||
>
|
||||
已成功解析,确认后导入数据库
|
||||
</div>
|
||||
</div>
|
||||
{preview.sample?.length > 0 && (
|
||||
<div style={{ marginBottom: 16 }}>
|
||||
<div style={{ fontSize: 12, fontWeight: 600, marginBottom: 6 }}>
|
||||
预览:
|
||||
</div>
|
||||
<div className="table-container">
|
||||
<table style={{ fontSize: 12 }}>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>日期</th>
|
||||
<th>对方</th>
|
||||
<th>描述</th>
|
||||
<th>金额</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{preview.sample.map((s, i) => (
|
||||
<tr key={i}>
|
||||
<td>{s.transaction_date}</td>
|
||||
<td>{s.counterparty}</td>
|
||||
<td
|
||||
style={{
|
||||
maxWidth: 120,
|
||||
overflow: "hidden",
|
||||
textOverflow: "ellipsis",
|
||||
whiteSpace: "nowrap",
|
||||
}}
|
||||
>
|
||||
{s.description}
|
||||
</td>
|
||||
<td
|
||||
className={
|
||||
s.direction === "income"
|
||||
? "stat-value income"
|
||||
: "stat-value expense"
|
||||
}
|
||||
>
|
||||
{s.amount.toFixed(2)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<div
|
||||
style={{ display: "flex", gap: 10, justifyContent: "flex-end" }}
|
||||
>
|
||||
<button className="btn btn-secondary" onClick={handleCancel}>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={handleConfirm}
|
||||
disabled={confirming}
|
||||
>
|
||||
<CheckCircle size={16} />{" "}
|
||||
{confirming ? "导入中..." : "确认导入"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 删除确认弹框 */}
|
||||
{deleteTarget && (
|
||||
<div className="modal-overlay">
|
||||
<div className="modal" style={{ maxWidth: 400 }}>
|
||||
<h3 style={{ marginBottom: 12 }}>确认删除</h3>
|
||||
<p
|
||||
style={{
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 20,
|
||||
fontSize: 14,
|
||||
}}
|
||||
>
|
||||
确定要删除此账单来源及其所有交易记录吗?此操作不可撤销。
|
||||
</p>
|
||||
<div
|
||||
style={{ display: "flex", gap: 10, justifyContent: "flex-end" }}
|
||||
>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
onClick={() => setDeleteTarget(null)}
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button className="btn btn-danger" onClick={confirmDelete}>
|
||||
确认删除
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user