209 lines
5.5 KiB
JavaScript
209 lines
5.5 KiB
JavaScript
const dayjs = require("dayjs");
|
|
|
|
/**
|
|
* 支付宝账单CSV解析器
|
|
*
|
|
* 支付宝导出的CSV通常有两种格式:
|
|
* 1. 标准版:交易时间,交易对方,商品说明,收/支,金额,交易状态,交易订单号,收/付款方式...
|
|
* 2. 新版:可能列名略有不同
|
|
*
|
|
* 此解析器尝试自动识别列名
|
|
*/
|
|
function parseAlipayCSV(records) {
|
|
const transactions = [];
|
|
|
|
for (const record of records) {
|
|
// 跳过空行和标题行
|
|
if (!record || Object.keys(record).length === 0) continue;
|
|
|
|
// 尝试多种可能的列名映射
|
|
const dateStr =
|
|
record["交易时间"] ||
|
|
record["交易日期"] ||
|
|
record["time"] ||
|
|
record["date"] ||
|
|
"";
|
|
const counterparty =
|
|
record["交易对方"] ||
|
|
record["交易对象"] ||
|
|
record["counterparty"] ||
|
|
record["merchant"] ||
|
|
"";
|
|
const description =
|
|
record["商品说明"] ||
|
|
record["商品"] ||
|
|
record["商品名称"] ||
|
|
record["description"] ||
|
|
record["product"] ||
|
|
"";
|
|
const notes = record["备注"] || record["memo"] || "";
|
|
const directionStr =
|
|
record["收/支"] ||
|
|
record["收支"] ||
|
|
record["direction"] ||
|
|
record["type"] ||
|
|
"";
|
|
const amountStr =
|
|
record["金额"] || record["amount"] || record["money"] || "0";
|
|
const status =
|
|
record["交易状态"] || record["status"] || record["交易状态"] || "";
|
|
const method =
|
|
record["收/付款方式"] ||
|
|
record["支付方式"] ||
|
|
record["method"] ||
|
|
record["payment"] ||
|
|
"";
|
|
const orderNo =
|
|
record["交易订单号"] ||
|
|
record["订单号"] ||
|
|
record["order_no"] ||
|
|
record["trade_no"] ||
|
|
"";
|
|
|
|
if (!dateStr && !amountStr) continue;
|
|
|
|
let amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ""));
|
|
if (isNaN(amount)) amount = 0;
|
|
|
|
// 确定收支方向
|
|
let direction = "expense";
|
|
if (
|
|
directionStr.includes("收入") ||
|
|
directionStr.includes("收") ||
|
|
directionStr === "收入"
|
|
) {
|
|
direction = "income";
|
|
} else if (directionStr.includes("支") || directionStr.includes("支出")) {
|
|
direction = "expense";
|
|
} else if (amount > 0) {
|
|
direction = "income";
|
|
} else {
|
|
amount = -Math.abs(amount);
|
|
direction = "expense";
|
|
}
|
|
|
|
// 统一金额符号:支出为负
|
|
if (direction === "expense") {
|
|
amount = -Math.abs(amount);
|
|
} else {
|
|
amount = Math.abs(amount);
|
|
}
|
|
|
|
const parsedDate = parseDate(dateStr);
|
|
|
|
transactions.push({
|
|
transaction_date: parsedDate,
|
|
transaction_time: dateStr,
|
|
counterparty: counterparty.trim(),
|
|
description: description.trim(),
|
|
notes: notes.trim(),
|
|
amount,
|
|
direction,
|
|
status: status.trim(),
|
|
method: method.trim(),
|
|
order_no: orderNo.trim(),
|
|
raw_data: JSON.stringify(record),
|
|
});
|
|
}
|
|
|
|
return transactions;
|
|
}
|
|
|
|
function parseDate(dateStr) {
|
|
if (!dateStr) return dayjs().format("YYYY-MM-DD");
|
|
|
|
// 尝试多种日期格式
|
|
const formats = [
|
|
"YYYY-MM-DD HH:mm:ss",
|
|
"YYYY-MM-DD HH:mm",
|
|
"YYYY-MM-DD",
|
|
"YYYY/MM/DD HH:mm:ss",
|
|
"YYYY/MM/DD HH:mm",
|
|
"YYYY/MM/DD",
|
|
"MM/DD/YYYY HH:mm:ss",
|
|
"MM/DD/YYYY",
|
|
"YYYY年MM月DD日 HH:mm:ss",
|
|
"YYYY年MM月DD日 HH:mm",
|
|
"YYYY年MM月DD日",
|
|
];
|
|
|
|
for (const fmt of formats) {
|
|
const d = dayjs(dateStr, fmt);
|
|
if (d.isValid()) return d.format("YYYY-MM-DD");
|
|
}
|
|
|
|
// 最后尝试直接解析
|
|
const d = dayjs(dateStr);
|
|
if (d.isValid()) return d.format("YYYY-MM-DD");
|
|
|
|
return dayjs().format("YYYY-MM-DD");
|
|
}
|
|
|
|
const XLSX = require("xlsx");
|
|
|
|
function parseAlipayFromXLSX(filePath) {
|
|
const workbook = XLSX.readFile(filePath);
|
|
const sheetName = workbook.SheetNames[0];
|
|
const sheet = workbook.Sheets[sheetName];
|
|
const rows = XLSX.utils.sheet_to_json(sheet, {
|
|
header: 1,
|
|
defval: "",
|
|
raw: true,
|
|
});
|
|
if (!rows || rows.length < 2) return [];
|
|
|
|
// 假设第一行是表头
|
|
const headers = rows[0];
|
|
const headerMap = {};
|
|
headers.forEach((h, i) => {
|
|
headerMap[h] = i;
|
|
});
|
|
|
|
const transactions = [];
|
|
for (let i = 1; i < rows.length; i++) {
|
|
const row = rows[i];
|
|
if (!row || row.length === 0) continue;
|
|
|
|
const dateStr = String(
|
|
row[headerMap["交易时间"] || headerMap["交易日期"]] || row[0] || "",
|
|
);
|
|
const counterparty = String(
|
|
row[headerMap["交易对方"]] || row[2] || "",
|
|
).trim();
|
|
const description = String(
|
|
row[headerMap["商品说明"] || headerMap["商品"]] || row[3] || "",
|
|
).trim();
|
|
const directionStr = String(row[headerMap["收/支"]] || row[4] || "").trim();
|
|
const amountRaw = row[headerMap["金额"]] || row[5] || 0;
|
|
|
|
let amount = parseFloat(amountRaw);
|
|
if (isNaN(amount)) amount = 0;
|
|
let direction = "expense";
|
|
if (directionStr.includes("收入") || directionStr.includes("收"))
|
|
direction = "income";
|
|
else if (directionStr.includes("支出") || directionStr.includes("支"))
|
|
direction = "expense";
|
|
|
|
if (direction === "expense") amount = -Math.abs(amount);
|
|
else amount = Math.abs(amount);
|
|
|
|
const parsedDate = parseDate(dateStr);
|
|
transactions.push({
|
|
transaction_date: parsedDate,
|
|
transaction_time: dateStr,
|
|
counterparty,
|
|
description,
|
|
notes: "",
|
|
amount,
|
|
direction,
|
|
status: "",
|
|
method: "",
|
|
order_no: "",
|
|
raw_data: JSON.stringify(row),
|
|
});
|
|
}
|
|
return transactions;
|
|
}
|
|
|
|
module.exports = { parseAlipayCSV, parseAlipayFromXLSX, parseDate };
|