index.js 2.1 KB

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