version 0.1

This commit is contained in:
2026-05-05 14:46:23 +08:00
parent 554efc9364
commit 64ffbf0851
5 changed files with 429 additions and 120 deletions
+145 -43
View File
@@ -1,53 +1,120 @@
const { parseDate } = require('./alipay');
const XLSX = require("xlsx");
const dayjs = require("dayjs");
/**
* 微信账单CSV解析器
*
* 微信导出的CSV格式:
* 交易时间,交易类型,交易对方,商品,收/支,金额(元),支付方式,当前状态,交易单号,商户单号,备注
*/
function parseWechatCSV(records) {
function parseWechatXLSX(filePath) {
const workbook = XLSX.readFile(filePath);
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const rows = XLSX.utils.sheet_to_json(sheet, {
header: 1,
defval: "",
raw: true,
});
if (!rows || rows.length < 18) return [];
const transactions = [];
for (let i = 18; i < rows.length; i++) {
const row = rows[i];
if (!row || row.length === 0) continue;
if (String(row[0] || "").includes("---") || String(row[0] || "") === "")
continue;
const { date: txDate, time: txTime } = excelSerialToDateTime(row[0]);
const type = String(row[1] || "").trim();
const counterparty = String(row[2] || "").trim();
const description = String(row[3] || "").trim();
const directionStr = String(row[4] || "").trim();
const amountRaw = row[5];
const method = String(row[6] || "").trim();
const status = String(row[7] || "").trim();
const orderNo = String(row[8] || "").trim();
const merchantNo = String(row[9] || "").trim();
const notes = String(row[10] || "").trim();
if (!txDate && !amountRaw) continue;
let amount = parseFloat(amountRaw);
if (isNaN(amount)) amount = 0;
let direction = "expense";
if (directionStr.includes("收入")) direction = "income";
else if (directionStr.includes("支出")) direction = "expense";
if (direction === "expense") amount = -Math.abs(amount);
else amount = Math.abs(amount);
transactions.push({
transaction_date: txDate,
transaction_time: txTime,
counterparty,
description: description || type,
notes,
amount,
direction,
status,
method,
order_no: orderNo,
transaction_type: type,
raw_data: JSON.stringify({
type,
counterparty,
description,
directionStr,
amount: amountRaw,
method,
status,
orderNo,
merchantNo,
notes,
}),
});
}
return transactions;
}
function parseWechatCSV(records) {
const { parseDate } = require("./alipay");
const transactions = [];
for (const record of records) {
if (!record || Object.keys(record).length === 0) continue;
const dateStr = record['交易时间'] || record['时间'] || record['time'] || record['date'] || '';
const type = record['交易类型'] || record['类型'] || record['transaction_type'] || '';
const counterparty = record['交易对方'] || record['对方'] || record['counterparty'] || '';
const description = record['商品'] || record['商品名称'] || record['product'] || record['description'] || '';
const directionStr = record['收/支'] || record['收支'] || record['direction'] || '';
const amountStr = record['金额(元)'] || record['金额'] || record['amount'] || '0';
const method = record['支付方式'] || record['method'] || record['payment'] || '';
const status = record['当前状态'] || record['状态'] || record['status'] || '';
const orderNo = record['交易单号'] || record['订单号'] || record['order_no'] || '';
const notes = record['备注'] || record['memo'] || '';
const dateStr =
record["交易时间"] ||
record["时间"] ||
record["time"] ||
record["date"] ||
"";
const type =
record["交易类型"] || record["类型"] || record["transaction_type"] || "";
const counterparty =
record["交易对方"] || record["对方"] || record["counterparty"] || "";
const description =
record["商品"] ||
record["商品名称"] ||
record["product"] ||
record["description"] ||
"";
const directionStr =
record["收/支"] || record["收支"] || record["direction"] || "";
const amountStr =
record["金额(元)"] || record["金额"] || record["amount"] || "0";
const method =
record["支付方式"] || record["method"] || record["payment"] || "";
const status =
record["当前状态"] || record["状态"] || record["status"] || "";
const orderNo =
record["交易单号"] || record["订单号"] || record["order_no"] || "";
const notes = record["备注"] || record["memo"] || "";
if (!dateStr && !amountStr) continue;
let amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ''));
let amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ""));
if (isNaN(amount)) amount = 0;
let direction = 'expense';
if (directionStr.includes('收入') || directionStr.includes('收')) {
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);
}
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,
@@ -60,11 +127,46 @@ function parseWechatCSV(records) {
method: method.trim(),
order_no: orderNo.trim(),
transaction_type: type.trim(),
raw_data: JSON.stringify(record)
raw_data: JSON.stringify(record),
});
}
return transactions;
}
module.exports = { parseWechatCSV };
// Excel 序列号 → {date: 'YYYY-MM-DD', time: 'YYYY-MM-DD HH:MM:SS'}
function excelSerialToDateTime(serial) {
if (!serial) {
const d = dayjs();
return {
date: d.format("YYYY-MM-DD"),
time: d.format("YYYY-MM-DD HH:mm:ss"),
};
}
if (
typeof serial === "string" &&
serial.match(/^\d{4}[-/]\d{1,2}[-/]\d{1,2}/)
) {
const d = dayjs(serial);
return {
date: d.format("YYYY-MM-DD"),
time: d.format("YYYY-MM-DD HH:mm:ss"),
};
}
const num = parseFloat(serial);
if (isNaN(num) || num <= 0) {
const d = dayjs();
return {
date: d.format("YYYY-MM-DD"),
time: d.format("YYYY-MM-DD HH:mm:ss"),
};
}
// Excel serial → JS date (days since 1899-12-30)
const jsDate = new Date((num - 25569) * 86400000);
const d = dayjs(jsDate);
return {
date: d.format("YYYY-MM-DD"),
time: d.format("YYYY-MM-DD HH:mm:ss"),
};
}
module.exports = { parseWechatXLSX, parseWechatCSV };