index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // 添加账本
  2. import express from "express";
  3. const router = express.Router();
  4. import {
  5. record_insert,
  6. record_and_files,
  7. recordRelation,
  8. isType,
  9. type_insert,
  10. getRecordInfoById
  11. } from "#db";
  12. import dayjs from "dayjs";
  13. // middleware that is specific to this router
  14. router.use(function timeLog(req, res, next) {
  15. console.log("Time: ", Date.now());
  16. next();
  17. });
  18. // 添加单个账单记录
  19. router.post("/", async function (req, res) {
  20. const {
  21. book_id = "",
  22. total_fee = 0,
  23. type = "",
  24. remark = "",
  25. files = [],
  26. userInfo = {},
  27. } = req.body;
  28. // type 是否存在重复项,有就返回id,没有就新增 再返回id
  29. const isAddType = await isType({
  30. book_id,
  31. user_id: userInfo.user_id,
  32. type,
  33. });
  34. let typeInfo = {};
  35. if (!isAddType) {
  36. await type_insert({
  37. book_id,
  38. user_id: userInfo.user_id,
  39. type,
  40. create_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  41. update_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  42. });
  43. typeInfo = await isType({
  44. book_id,
  45. user_id: userInfo.user_id,
  46. type,
  47. });
  48. } else {
  49. typeInfo = { ...isAddType };
  50. }
  51. console.log(51, {
  52. book_id,
  53. type_id: typeInfo.id,
  54. author_id: userInfo.user_id,
  55. total_fee,
  56. remark,
  57. create_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  58. update_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  59. })
  60. const insertId = await record_insert({
  61. book_id,
  62. type_id: typeInfo.id,
  63. author_id: userInfo.user_id,
  64. total_fee,
  65. remark,
  66. create_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  67. update_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  68. });
  69. res.json({
  70. code: 200,
  71. data: {
  72. record_id: insertId
  73. }
  74. });
  75. });
  76. // define the home page route
  77. router.get("/:record_id", async function (req, res) {
  78. const record_id = req.params.record_id; // 获取 fileId 参数
  79. const recordInfo = await getRecordInfoById(record_id)
  80. res.json({
  81. code: 200,
  82. data: recordInfo
  83. });
  84. });
  85. // define the about route
  86. router.get("/about", function (req, res) {
  87. res.send("About record");
  88. });
  89. export default router;