// 添加账本 import express from "express"; const router = express.Router(); import { record_insert, record_update, isType, type_insert, getRecordInfoById, addFileByRecordId, getFileByRecordId, getRecordsInfoByTime, getRecordsInfoByMonth, getTypesById, delFileByRecordId, } from "#db"; import { shanghaiTime, shanghaiTimeFormat } from "#utils"; import dayjs from "dayjs"; // middleware that is specific to this router router.use(function timeLog(req, res, next) { console.log("Time: ", Date.now()); next(); }); async function getTypeInfo({ userInfo, book_id, type }) { if (!type) { return ""; } // type 是否存在重复项,有就返回id,没有就新增 再返回id const isAddType = await isType({ book_id, author_id: userInfo.user_id, type, }); let typeInfo = {}; if (!isAddType) { await type_insert({ book_id, author_id: userInfo.user_id, type, create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), }); typeInfo = await isType({ book_id, author_id: userInfo.user_id, type, }); } else { typeInfo = { ...isAddType }; } return Promise.resolve(typeInfo.id); } // 添加单个账单记录 router.post("/", async function (req, res) { const { book_id = "", total_fee = 0, type = "", time = "", remark = "", files = [], userInfo = {}, } = req.body; const type_id = await getTypeInfo({ userInfo, book_id, type }); const insertId = await record_insert({ book_id, type_id: type_id, author_id: userInfo.user_id, total_fee, remark, time: shanghaiTimeFormat(time), create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), }); const filesRes = await Promise.all( files.map((file_id) => addFileByRecordId({ file_id, record_id: insertId, book_id, author_id: userInfo.user_id, create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), }) ) ); res.json({ code: 200, data: { record_id: insertId, }, }); }); // define the home page route router.get("/:record_id", async function (req, res) { const record_id = req.params.record_id; // 获取 fileId 参数 const recordInfo = await getRecordInfoById(record_id); const files = await getFileByRecordId(record_id); const typesRes = await getTypesById({ typeId: recordInfo.type_id, book_id: recordInfo.book_id, author_id: recordInfo.author_id, }); if (typesRes) { recordInfo.type = typesRes.name; } else { recordInfo.type = ""; } // http://127.0.0.1:20040/?zs_interval=1732419124661/api/v1/files/6a3beffd24ee92a0b0846afaf2196b14 // http://localhost:3000/?zs_interval=1732420198559/api/v1/files/aace8ebb259f6e70ef5b0c1f8efde3ae function getFileUrl(elm) { // 获取请求的来源域名 const host = req.headers['host']; // 主机名 + 端口 const origin = req.headers["referer"]; // 请求的来源域(适用于跨域) if (`${origin}`.indexOf("3032") > -1 || `${host}`.indexOf('3000') > -1) { return "http://localhost:3000" + `/api_files_${elm.file_id}`; } if (`${origin}`.indexOf("zs_interval") > -1) { return `http://${origin.replace('/static/', '')}/api/v1/files/${elm.file_id}`; } return `${origin}api_files_${elm.file_id}`; } res.json({ code: 200, data: { headers: req.headers, ...recordInfo, time: shanghaiTimeFormat(recordInfo.time, "YYYY-MM-DD"), create_time: shanghaiTimeFormat(recordInfo.create_time), update_time: shanghaiTimeFormat(recordInfo.update_time), files: files.map((elm) => getFileUrl(elm)), }, }); }); // 更新账本数据 router.put("/:record_id", async function (req, res) { const record_id = req.params.record_id; // 获取 fileId 参数 const { book_id = "", total_fee = 0, type = "", time = "", remark = "", files = [], userInfo = {}, } = req.body; const getAllfiles = await getFileByRecordId(record_id); const getAllfilesIds = getAllfiles.map((elm) => elm.file_id); const dellFilesInRecordFiles = getAllfiles.filter( (elm) => files.indexOf(elm.file_id) < 0 ); const needAddFiles = files.filter( (file_id) => getAllfilesIds.indexOf(file_id) < 0 ); if (dellFilesInRecordFiles.length) { await Promise.all( dellFilesInRecordFiles.map((elm) => delFileByRecordId(record_id, elm.file_id) ) ); } if (needAddFiles.length) { // 更新附件信息 await Promise.all( needAddFiles.map((file_id) => addFileByRecordId({ file_id, record_id, book_id, author_id: userInfo.user_id, create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), }) ) ); } const typeId = await getTypeInfo({ userInfo, book_id, type, }); const recordInfo = await record_update({ id: record_id, // 要更新的记录的唯一标识符 type_id: typeId, author_id: userInfo.user_id, total_fee: total_fee, remark: remark, time: time, update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), }); res.json({ code: 200, data: recordInfo ? "" : "更新失败", }); }); // define the about route router.get("/about", function (req, res) { res.send("About record"); }); // 根据日期获取数据 router.get("/:book_id/:time", async function (req, res) { const time = req.params.time; // 获取 fileId 参数 const book_id = req.params.book_id; // 获取 fileId 参数 const recordsInfoRes = await getRecordsInfoByTime(time, book_id); res.json({ code: 200, data: recordsInfoRes.map((elm) => ({ ...elm, time: shanghaiTimeFormat(elm.time, "YYYY-MM-DD"), create_time: shanghaiTimeFormat(elm.create_time), update_time: shanghaiTimeFormat(elm.update_time), })), }); }); // 根据日期获取数据 router.get("/:book_id/m/:time", async function (req, res) { const time = req.params.time; // 获取 fileId 参数 const book_id = req.params.book_id; // 获取 fileId 参数 const recordsInfoRes = await getRecordsInfoByMonth(time, book_id); res.json({ code: 200, data: recordsInfoRes.map((elm) => ({ ...elm, time: shanghaiTimeFormat(elm.time, "YYYY-MM-DD"), create_time: shanghaiTimeFormat(elm.create_time), update_time: shanghaiTimeFormat(elm.update_time), })), }); }); export default router;