version 0.1
This commit is contained in:
+6
-5
@@ -13,14 +13,15 @@
|
|||||||
"setup": "npm install && npm run install:all"
|
"setup": "npm install && npm run install:all"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"better-sqlite3": "^11.7.0",
|
"better-sqlite3": "^11.10.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
"crypto-js": "^4.2.0",
|
||||||
|
"csv-parse": "^5.6.0",
|
||||||
|
"dayjs": "^1.11.13",
|
||||||
"express": "^4.21.0",
|
"express": "^4.21.0",
|
||||||
"multer": "^1.4.5-lts.1",
|
"multer": "^1.4.5-lts.1",
|
||||||
"csv-parse": "^5.6.0",
|
|
||||||
"uuid": "^10.0.0",
|
|
||||||
"openai": "^4.73.0",
|
"openai": "^4.73.0",
|
||||||
"dayjs": "^1.11.13",
|
"uuid": "^10.0.0",
|
||||||
"crypto-js": "^4.2.0"
|
"xlsx": "^0.18.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+139
-36
@@ -1,4 +1,4 @@
|
|||||||
const dayjs = require('dayjs');
|
const dayjs = require("dayjs");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 支付宝账单CSV解析器
|
* 支付宝账单CSV解析器
|
||||||
@@ -17,36 +17,73 @@ function parseAlipayCSV(records) {
|
|||||||
if (!record || Object.keys(record).length === 0) continue;
|
if (!record || Object.keys(record).length === 0) continue;
|
||||||
|
|
||||||
// 尝试多种可能的列名映射
|
// 尝试多种可能的列名映射
|
||||||
const dateStr = record['交易时间'] || record['交易日期'] || record['time'] || record['date'] || '';
|
const dateStr =
|
||||||
const counterparty = record['交易对方'] || record['交易对象'] || record['counterparty'] || record['merchant'] || '';
|
record["交易时间"] ||
|
||||||
const description = record['商品说明'] || record['商品'] || record['商品名称'] || record['description'] || record['product'] || '';
|
record["交易日期"] ||
|
||||||
const notes = record['备注'] || record['memo'] || '';
|
record["time"] ||
|
||||||
const directionStr = record['收/支'] || record['收支'] || record['direction'] || record['type'] || '';
|
record["date"] ||
|
||||||
const amountStr = record['金额'] || record['amount'] || record['money'] || '0';
|
"";
|
||||||
const status = record['交易状态'] || record['status'] || record['交易状态'] || '';
|
const counterparty =
|
||||||
const method = record['收/付款方式'] || record['支付方式'] || record['method'] || record['payment'] || '';
|
record["交易对方"] ||
|
||||||
const orderNo = record['交易订单号'] || record['订单号'] || record['order_no'] || record['trade_no'] || '';
|
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;
|
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;
|
if (isNaN(amount)) amount = 0;
|
||||||
|
|
||||||
// 确定收支方向
|
// 确定收支方向
|
||||||
let direction = 'expense';
|
let direction = "expense";
|
||||||
if (directionStr.includes('收入') || directionStr.includes('收') || directionStr === '收入') {
|
if (
|
||||||
direction = 'income';
|
directionStr.includes("收入") ||
|
||||||
} else if (directionStr.includes('支') || directionStr.includes('支出')) {
|
directionStr.includes("收") ||
|
||||||
direction = 'expense';
|
directionStr === "收入"
|
||||||
|
) {
|
||||||
|
direction = "income";
|
||||||
|
} else if (directionStr.includes("支") || directionStr.includes("支出")) {
|
||||||
|
direction = "expense";
|
||||||
} else if (amount > 0) {
|
} else if (amount > 0) {
|
||||||
direction = 'income';
|
direction = "income";
|
||||||
} else {
|
} else {
|
||||||
amount = -Math.abs(amount);
|
amount = -Math.abs(amount);
|
||||||
direction = 'expense';
|
direction = "expense";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 统一金额符号:支出为负
|
// 统一金额符号:支出为负
|
||||||
if (direction === 'expense') {
|
if (direction === "expense") {
|
||||||
amount = -Math.abs(amount);
|
amount = -Math.abs(amount);
|
||||||
} else {
|
} else {
|
||||||
amount = Math.abs(amount);
|
amount = Math.abs(amount);
|
||||||
@@ -65,7 +102,7 @@ function parseAlipayCSV(records) {
|
|||||||
status: status.trim(),
|
status: status.trim(),
|
||||||
method: method.trim(),
|
method: method.trim(),
|
||||||
order_no: orderNo.trim(),
|
order_no: orderNo.trim(),
|
||||||
raw_data: JSON.stringify(record)
|
raw_data: JSON.stringify(record),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,33 +110,99 @@ function parseAlipayCSV(records) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function parseDate(dateStr) {
|
function parseDate(dateStr) {
|
||||||
if (!dateStr) return dayjs().format('YYYY-MM-DD');
|
if (!dateStr) return dayjs().format("YYYY-MM-DD");
|
||||||
|
|
||||||
// 尝试多种日期格式
|
// 尝试多种日期格式
|
||||||
const formats = [
|
const formats = [
|
||||||
'YYYY-MM-DD HH:mm:ss',
|
"YYYY-MM-DD HH:mm:ss",
|
||||||
'YYYY-MM-DD HH:mm',
|
"YYYY-MM-DD HH:mm",
|
||||||
'YYYY-MM-DD',
|
"YYYY-MM-DD",
|
||||||
'YYYY/MM/DD HH:mm:ss',
|
"YYYY/MM/DD HH:mm:ss",
|
||||||
'YYYY/MM/DD HH:mm',
|
"YYYY/MM/DD HH:mm",
|
||||||
'YYYY/MM/DD',
|
"YYYY/MM/DD",
|
||||||
'MM/DD/YYYY HH:mm:ss',
|
"MM/DD/YYYY HH:mm:ss",
|
||||||
'MM/DD/YYYY',
|
"MM/DD/YYYY",
|
||||||
'YYYY年MM月DD日 HH:mm:ss',
|
"YYYY年MM月DD日 HH:mm:ss",
|
||||||
'YYYY年MM月DD日 HH:mm',
|
"YYYY年MM月DD日 HH:mm",
|
||||||
'YYYY年MM月DD日',
|
"YYYY年MM月DD日",
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const fmt of formats) {
|
for (const fmt of formats) {
|
||||||
const d = dayjs(dateStr, fmt);
|
const d = dayjs(dateStr, fmt);
|
||||||
if (d.isValid()) return d.format('YYYY-MM-DD');
|
if (d.isValid()) return d.format("YYYY-MM-DD");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 最后尝试直接解析
|
// 最后尝试直接解析
|
||||||
const d = dayjs(dateStr);
|
const d = dayjs(dateStr);
|
||||||
if (d.isValid()) return d.format('YYYY-MM-DD');
|
if (d.isValid()) return d.format("YYYY-MM-DD");
|
||||||
|
|
||||||
return dayjs().format('YYYY-MM-DD');
|
return dayjs().format("YYYY-MM-DD");
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { parseAlipayCSV, parseDate };
|
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 };
|
||||||
|
|||||||
+82
-23
@@ -1,4 +1,4 @@
|
|||||||
const { parseDate } = require('./alipay');
|
const { parseDate } = require("./alipay");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 银行卡流水CSV解析器
|
* 银行卡流水CSV解析器
|
||||||
@@ -16,33 +16,68 @@ function parseBankCSV(records) {
|
|||||||
for (const record of records) {
|
for (const record of records) {
|
||||||
if (!record || Object.keys(record).length === 0) continue;
|
if (!record || Object.keys(record).length === 0) continue;
|
||||||
|
|
||||||
const dateStr = record['交易日期'] || record['日期'] || record['交易时间'] || record['date'] || record['time'] || '';
|
const dateStr =
|
||||||
const description = record['摘要'] || record['说明'] || record['description'] || record['summary'] || record['用途'] || '';
|
record["交易日期"] ||
|
||||||
const counterparty = record['交易对方'] || record['对方'] || record['counterparty'] || record['对方户名'] || '';
|
record["日期"] ||
|
||||||
const notes = record['备注'] || record['memo'] || record['附言'] || '';
|
record["交易时间"] ||
|
||||||
const balanceStr = record['余额'] || record['balance'] || '';
|
record["date"] ||
|
||||||
const method = record['交易方式'] || record['渠道'] || record['method'] || '';
|
record["time"] ||
|
||||||
|
"";
|
||||||
|
const description =
|
||||||
|
record["摘要"] ||
|
||||||
|
record["说明"] ||
|
||||||
|
record["description"] ||
|
||||||
|
record["summary"] ||
|
||||||
|
record["用途"] ||
|
||||||
|
"";
|
||||||
|
const counterparty =
|
||||||
|
record["交易对方"] ||
|
||||||
|
record["对方"] ||
|
||||||
|
record["counterparty"] ||
|
||||||
|
record["对方户名"] ||
|
||||||
|
"";
|
||||||
|
const notes = record["备注"] || record["memo"] || record["附言"] || "";
|
||||||
|
const balanceStr = record["余额"] || record["balance"] || "";
|
||||||
|
const method =
|
||||||
|
record["交易方式"] || record["渠道"] || record["method"] || "";
|
||||||
|
|
||||||
// 银行通常有两种格式:单列金额(正负表示方向) 或 分列收入/支出
|
// 银行通常有两种格式:单列金额(正负表示方向) 或 分列收入/支出
|
||||||
let amount = 0;
|
let amount = 0;
|
||||||
let direction = 'expense';
|
let direction = "expense";
|
||||||
|
|
||||||
const amountStr = record['交易金额'] || record['金额'] || record['amount'] || '';
|
const amountStr =
|
||||||
const incomeStr = record['收入金额'] || record['收入'] || record['credit'] || record['贷方金额'] || '';
|
record["交易金额"] || record["金额"] || record["amount"] || "";
|
||||||
const expenseStr = record['支出金额'] || record['支出'] || record['debit'] || record['借方金额'] || '';
|
const incomeStr =
|
||||||
|
record["收入金额"] ||
|
||||||
|
record["收入"] ||
|
||||||
|
record["credit"] ||
|
||||||
|
record["贷方金额"] ||
|
||||||
|
"";
|
||||||
|
const expenseStr =
|
||||||
|
record["支出金额"] ||
|
||||||
|
record["支出"] ||
|
||||||
|
record["debit"] ||
|
||||||
|
record["借方金额"] ||
|
||||||
|
"";
|
||||||
|
|
||||||
if (incomeStr && parseFloat(String(incomeStr).replace(/[,,¥¥\s]/g, '')) !== 0) {
|
if (
|
||||||
amount = parseFloat(String(incomeStr).replace(/[,,¥¥\s]/g, ''));
|
incomeStr &&
|
||||||
direction = 'income';
|
parseFloat(String(incomeStr).replace(/[,,¥¥\s]/g, "")) !== 0
|
||||||
} else if (expenseStr && parseFloat(String(expenseStr).replace(/[,,¥¥\s]/g, '')) !== 0) {
|
) {
|
||||||
amount = -parseFloat(String(expenseStr).replace(/[,,¥¥\s]/g, ''));
|
amount = parseFloat(String(incomeStr).replace(/[,,¥¥\s]/g, ""));
|
||||||
direction = 'expense';
|
direction = "income";
|
||||||
|
} else if (
|
||||||
|
expenseStr &&
|
||||||
|
parseFloat(String(expenseStr).replace(/[,,¥¥\s]/g, "")) !== 0
|
||||||
|
) {
|
||||||
|
amount = -parseFloat(String(expenseStr).replace(/[,,¥¥\s]/g, ""));
|
||||||
|
direction = "expense";
|
||||||
} else if (amountStr) {
|
} else if (amountStr) {
|
||||||
amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ''));
|
amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ""));
|
||||||
if (amount < 0) {
|
if (amount < 0) {
|
||||||
direction = 'expense';
|
direction = "expense";
|
||||||
} else {
|
} else {
|
||||||
direction = 'income';
|
direction = "income";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +85,7 @@ function parseBankCSV(records) {
|
|||||||
|
|
||||||
let balance = null;
|
let balance = null;
|
||||||
if (balanceStr) {
|
if (balanceStr) {
|
||||||
balance = parseFloat(String(balanceStr).replace(/[,,¥¥\s]/g, ''));
|
balance = parseFloat(String(balanceStr).replace(/[,,¥¥\s]/g, ""));
|
||||||
if (isNaN(balance)) balance = null;
|
if (isNaN(balance)) balance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,11 +101,35 @@ function parseBankCSV(records) {
|
|||||||
direction,
|
direction,
|
||||||
balance,
|
balance,
|
||||||
method: method.trim(),
|
method: method.trim(),
|
||||||
raw_data: JSON.stringify(record)
|
raw_data: JSON.stringify(record),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return transactions;
|
return transactions;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { parseBankCSV };
|
const XLSX = require("xlsx");
|
||||||
|
|
||||||
|
function parseBankFromXLSX(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 [];
|
||||||
|
// Fallback: use CSV parser on the rows treated as records
|
||||||
|
const headerRow = rows[0];
|
||||||
|
const dataRows = rows.slice(1).map((row) => {
|
||||||
|
const obj = {};
|
||||||
|
headerRow.forEach((h, i) => {
|
||||||
|
obj[h] = row[i];
|
||||||
|
});
|
||||||
|
return obj;
|
||||||
|
});
|
||||||
|
return parseBankCSV(dataRows);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { parseBankCSV, parseBankFromXLSX };
|
||||||
|
|||||||
+57
-13
@@ -1,4 +1,4 @@
|
|||||||
const { parseDate } = require('./alipay');
|
const { parseDate } = require("./alipay");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 信用卡账单CSV解析器
|
* 信用卡账单CSV解析器
|
||||||
@@ -13,23 +13,44 @@ function parseCreditCardCSV(records) {
|
|||||||
for (const record of records) {
|
for (const record of records) {
|
||||||
if (!record || Object.keys(record).length === 0) continue;
|
if (!record || Object.keys(record).length === 0) continue;
|
||||||
|
|
||||||
const dateStr = record['交易日期'] || record['交易日'] || record['date'] || record['transaction_date'] || '';
|
const dateStr =
|
||||||
const postDateStr = record['入账日期'] || record['记账日'] || record['post_date'] || dateStr;
|
record["交易日期"] ||
|
||||||
const description = record['交易说明'] || record['交易描述'] || record['description'] || record['商品'] || '';
|
record["交易日"] ||
|
||||||
const counterparty = record['交易对方'] || record['商户名称'] || record['merchant'] || record['counterparty'] || '';
|
record["date"] ||
|
||||||
const notes = record['备注'] || record['memo'] || '';
|
record["transaction_date"] ||
|
||||||
|
"";
|
||||||
|
const postDateStr =
|
||||||
|
record["入账日期"] || record["记账日"] || record["post_date"] || dateStr;
|
||||||
|
const description =
|
||||||
|
record["交易说明"] ||
|
||||||
|
record["交易描述"] ||
|
||||||
|
record["description"] ||
|
||||||
|
record["商品"] ||
|
||||||
|
"";
|
||||||
|
const counterparty =
|
||||||
|
record["交易对方"] ||
|
||||||
|
record["商户名称"] ||
|
||||||
|
record["merchant"] ||
|
||||||
|
record["counterparty"] ||
|
||||||
|
"";
|
||||||
|
const notes = record["备注"] || record["memo"] || "";
|
||||||
|
|
||||||
// 信用卡金额通常是正数表示消费(支出),负数表示还款(收入)
|
// 信用卡金额通常是正数表示消费(支出),负数表示还款(收入)
|
||||||
const amountStr = record['交易金额'] || record['人民币金额'] || record['金额'] || record['amount'] || '0';
|
const amountStr =
|
||||||
const postAmountStr = record['入账金额'] || record['记账金额'] || '';
|
record["交易金额"] ||
|
||||||
|
record["人民币金额"] ||
|
||||||
|
record["金额"] ||
|
||||||
|
record["amount"] ||
|
||||||
|
"0";
|
||||||
|
const postAmountStr = record["入账金额"] || record["记账金额"] || "";
|
||||||
|
|
||||||
let amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ''));
|
let amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ""));
|
||||||
if (isNaN(amount)) amount = 0;
|
if (isNaN(amount)) amount = 0;
|
||||||
|
|
||||||
// 信用卡:正数=消费(欠款增加,对用户来说是支出)
|
// 信用卡:正数=消费(欠款增加,对用户来说是支出)
|
||||||
let direction = 'expense';
|
let direction = "expense";
|
||||||
if (amount < 0) {
|
if (amount < 0) {
|
||||||
direction = 'income'; // 负数=还款/退款
|
direction = "income"; // 负数=还款/退款
|
||||||
amount = Math.abs(amount); // 存储为正值收入
|
amount = Math.abs(amount); // 存储为正值收入
|
||||||
} else {
|
} else {
|
||||||
amount = -Math.abs(amount); // 存储为负值支出
|
amount = -Math.abs(amount); // 存储为负值支出
|
||||||
@@ -46,11 +67,34 @@ function parseCreditCardCSV(records) {
|
|||||||
amount,
|
amount,
|
||||||
direction,
|
direction,
|
||||||
post_date: parseDate(postDateStr),
|
post_date: parseDate(postDateStr),
|
||||||
raw_data: JSON.stringify(record)
|
raw_data: JSON.stringify(record),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return transactions;
|
return transactions;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { parseCreditCardCSV };
|
const XLSX = require("xlsx");
|
||||||
|
|
||||||
|
function parseCreditFromXLSX(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 headerRow = rows[0];
|
||||||
|
const dataRows = rows.slice(1).map((row) => {
|
||||||
|
const obj = {};
|
||||||
|
headerRow.forEach((h, i) => {
|
||||||
|
obj[h] = row[i];
|
||||||
|
});
|
||||||
|
return obj;
|
||||||
|
});
|
||||||
|
return parseCreditCardCSV(dataRows);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { parseCreditCardCSV, parseCreditFromXLSX };
|
||||||
|
|||||||
+145
-43
@@ -1,53 +1,120 @@
|
|||||||
const { parseDate } = require('./alipay');
|
const XLSX = require("xlsx");
|
||||||
|
const dayjs = require("dayjs");
|
||||||
|
|
||||||
/**
|
function parseWechatXLSX(filePath) {
|
||||||
* 微信账单CSV解析器
|
const workbook = XLSX.readFile(filePath);
|
||||||
*
|
const sheet = workbook.Sheets[workbook.SheetNames[0]];
|
||||||
* 微信导出的CSV格式:
|
const rows = XLSX.utils.sheet_to_json(sheet, {
|
||||||
* 交易时间,交易类型,交易对方,商品,收/支,金额(元),支付方式,当前状态,交易单号,商户单号,备注
|
header: 1,
|
||||||
*/
|
defval: "",
|
||||||
function parseWechatCSV(records) {
|
raw: true,
|
||||||
|
});
|
||||||
|
if (!rows || rows.length < 18) return [];
|
||||||
const transactions = [];
|
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) {
|
for (const record of records) {
|
||||||
if (!record || Object.keys(record).length === 0) continue;
|
if (!record || Object.keys(record).length === 0) continue;
|
||||||
|
const dateStr =
|
||||||
const dateStr = record['交易时间'] || record['时间'] || record['time'] || record['date'] || '';
|
record["交易时间"] ||
|
||||||
const type = record['交易类型'] || record['类型'] || record['transaction_type'] || '';
|
record["时间"] ||
|
||||||
const counterparty = record['交易对方'] || record['对方'] || record['counterparty'] || '';
|
record["time"] ||
|
||||||
const description = record['商品'] || record['商品名称'] || record['product'] || record['description'] || '';
|
record["date"] ||
|
||||||
const directionStr = record['收/支'] || record['收支'] || record['direction'] || '';
|
"";
|
||||||
const amountStr = record['金额(元)'] || record['金额'] || record['amount'] || '0';
|
const type =
|
||||||
const method = record['支付方式'] || record['method'] || record['payment'] || '';
|
record["交易类型"] || record["类型"] || record["transaction_type"] || "";
|
||||||
const status = record['当前状态'] || record['状态'] || record['status'] || '';
|
const counterparty =
|
||||||
const orderNo = record['交易单号'] || record['订单号'] || record['order_no'] || '';
|
record["交易对方"] || record["对方"] || record["counterparty"] || "";
|
||||||
const notes = record['备注'] || record['memo'] || '';
|
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;
|
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;
|
if (isNaN(amount)) amount = 0;
|
||||||
|
let direction = "expense";
|
||||||
let direction = 'expense';
|
if (directionStr.includes("收入") || directionStr.includes("收"))
|
||||||
if (directionStr.includes('收入') || directionStr.includes('收')) {
|
direction = "income";
|
||||||
direction = 'income';
|
else if (directionStr.includes("支出") || directionStr.includes("支"))
|
||||||
} else if (directionStr.includes('支出') || directionStr.includes('支')) {
|
direction = "expense";
|
||||||
direction = 'expense';
|
if (direction === "expense") amount = -Math.abs(amount);
|
||||||
} else if (amount > 0) {
|
else amount = Math.abs(amount);
|
||||||
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);
|
const parsedDate = parseDate(dateStr);
|
||||||
|
|
||||||
transactions.push({
|
transactions.push({
|
||||||
transaction_date: parsedDate,
|
transaction_date: parsedDate,
|
||||||
transaction_time: dateStr,
|
transaction_time: dateStr,
|
||||||
@@ -60,11 +127,46 @@ function parseWechatCSV(records) {
|
|||||||
method: method.trim(),
|
method: method.trim(),
|
||||||
order_no: orderNo.trim(),
|
order_no: orderNo.trim(),
|
||||||
transaction_type: type.trim(),
|
transaction_type: type.trim(),
|
||||||
raw_data: JSON.stringify(record)
|
raw_data: JSON.stringify(record),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return transactions;
|
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 };
|
||||||
|
|||||||
Reference in New Issue
Block a user