index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // 添加账本
  2. import express from "express";
  3. const router = express.Router();
  4. import {
  5. record_insert,
  6. record_update,
  7. isType,
  8. type_insert,
  9. getRecordInfoById,
  10. addFileByRecordId,
  11. getFileByRecordId,
  12. getRecordsInfoByTime,
  13. getRecordsInfoByMonth,
  14. getTypesById,
  15. delFileByRecordId,
  16. } from "#db";
  17. import { shanghaiTime, shanghaiTimeFormat } from "#utils";
  18. import dayjs from "dayjs";
  19. // middleware that is specific to this router
  20. router.use(function timeLog(req, res, next) {
  21. console.log("Time: ", Date.now());
  22. next();
  23. });
  24. async function getTypeInfo({ userInfo, book_id, type }) {
  25. if (!type) {
  26. return "";
  27. }
  28. // type 是否存在重复项,有就返回id,没有就新增 再返回id
  29. const isAddType = await isType({
  30. book_id,
  31. author_id: userInfo.user_id,
  32. type,
  33. });
  34. let typeInfo = {};
  35. if (!isAddType) {
  36. await type_insert({
  37. book_id,
  38. author_id: userInfo.user_id,
  39. type,
  40. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  41. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  42. });
  43. typeInfo = await isType({
  44. book_id,
  45. author_id: userInfo.user_id,
  46. type,
  47. });
  48. } else {
  49. typeInfo = { ...isAddType };
  50. }
  51. return Promise.resolve(typeInfo.id);
  52. }
  53. // 添加单个账单记录
  54. router.post("/", async function (req, res) {
  55. const {
  56. book_id = "",
  57. total_fee = 0,
  58. type = "",
  59. time = "",
  60. remark = "",
  61. files = [],
  62. userInfo = {},
  63. } = req.body;
  64. const type_id = await getTypeInfo({ userInfo, book_id, type });
  65. const insertId = await record_insert({
  66. book_id,
  67. type_id: type_id,
  68. author_id: userInfo.user_id,
  69. total_fee,
  70. remark,
  71. time: shanghaiTimeFormat(time),
  72. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  73. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  74. });
  75. const filesRes = await Promise.all(
  76. files.map((file_id) =>
  77. addFileByRecordId({
  78. file_id,
  79. record_id: insertId,
  80. book_id,
  81. author_id: userInfo.user_id,
  82. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  83. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  84. })
  85. )
  86. );
  87. res.json({
  88. code: 200,
  89. data: {
  90. record_id: insertId,
  91. },
  92. });
  93. });
  94. // define the home page route
  95. router.get("/:record_id", async function (req, res) {
  96. const record_id = req.params.record_id; // 获取 fileId 参数
  97. const recordInfo = await getRecordInfoById(record_id);
  98. const files = await getFileByRecordId(record_id);
  99. const typesRes = await getTypesById({
  100. typeId: recordInfo.type_id,
  101. book_id: recordInfo.book_id,
  102. author_id: recordInfo.author_id,
  103. });
  104. if (typesRes) {
  105. recordInfo.type = typesRes.name;
  106. } else {
  107. recordInfo.type = "";
  108. }
  109. // http://127.0.0.1:20040/?zs_interval=1732419124661/api/v1/files/6a3beffd24ee92a0b0846afaf2196b14
  110. // http://localhost:3000/?zs_interval=1732420198559/api/v1/files/aace8ebb259f6e70ef5b0c1f8efde3ae
  111. function getFileUrl(elm) {
  112. // 获取请求的来源域名
  113. const host = req.headers['host']; // 主机名 + 端口
  114. const origin = req.headers["referer"]; // 请求的来源域(适用于跨域)
  115. if (`${origin}`.indexOf("3032") > -1 || `${host}`.indexOf('3000') > -1) {
  116. return "http://localhost:3000" + `/api_files_${elm.file_id}`;
  117. }
  118. if (`${origin}`.indexOf("zs_interval") > -1) {
  119. return `http://${origin.replace('/static/', '')}/api/v1/files/${elm.file_id}`;
  120. }
  121. return `${origin}api_files_${elm.file_id}`;
  122. }
  123. res.json({
  124. code: 200,
  125. data: {
  126. headers: req.headers,
  127. ...recordInfo,
  128. time: shanghaiTimeFormat(recordInfo.time, "YYYY-MM-DD"),
  129. create_time: shanghaiTimeFormat(recordInfo.create_time),
  130. update_time: shanghaiTimeFormat(recordInfo.update_time),
  131. files: files.map((elm) => getFileUrl(elm)),
  132. },
  133. });
  134. });
  135. // 更新账本数据
  136. router.put("/:record_id", async function (req, res) {
  137. const record_id = req.params.record_id; // 获取 fileId 参数
  138. const {
  139. book_id = "",
  140. total_fee = 0,
  141. type = "",
  142. time = "",
  143. remark = "",
  144. files = [],
  145. userInfo = {},
  146. } = req.body;
  147. const getAllfiles = await getFileByRecordId(record_id);
  148. const getAllfilesIds = getAllfiles.map((elm) => elm.file_id);
  149. const dellFilesInRecordFiles = getAllfiles.filter(
  150. (elm) => files.indexOf(elm.file_id) < 0
  151. );
  152. const needAddFiles = files.filter(
  153. (file_id) => getAllfilesIds.indexOf(file_id) < 0
  154. );
  155. if (dellFilesInRecordFiles.length) {
  156. await Promise.all(
  157. dellFilesInRecordFiles.map((elm) =>
  158. delFileByRecordId(record_id, elm.file_id)
  159. )
  160. );
  161. }
  162. if (needAddFiles.length) {
  163. // 更新附件信息
  164. await Promise.all(
  165. needAddFiles.map((file_id) =>
  166. addFileByRecordId({
  167. file_id,
  168. record_id,
  169. book_id,
  170. author_id: userInfo.user_id,
  171. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  172. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  173. })
  174. )
  175. );
  176. }
  177. const typeId = await getTypeInfo({
  178. userInfo,
  179. book_id,
  180. type,
  181. });
  182. const recordInfo = await record_update({
  183. id: record_id, // 要更新的记录的唯一标识符
  184. type_id: typeId,
  185. author_id: userInfo.user_id,
  186. total_fee: total_fee,
  187. remark: remark,
  188. time: time,
  189. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  190. });
  191. res.json({
  192. code: 200,
  193. data: recordInfo ? "" : "更新失败",
  194. });
  195. });
  196. // define the about route
  197. router.get("/about", function (req, res) {
  198. res.send("About record");
  199. });
  200. // 根据日期获取数据
  201. router.get("/:book_id/:time", async function (req, res) {
  202. const time = req.params.time; // 获取 fileId 参数
  203. const book_id = req.params.book_id; // 获取 fileId 参数
  204. const recordsInfoRes = await getRecordsInfoByTime(time, book_id);
  205. res.json({
  206. code: 200,
  207. data: recordsInfoRes.map((elm) => ({
  208. ...elm,
  209. time: shanghaiTimeFormat(elm.time, "YYYY-MM-DD"),
  210. create_time: shanghaiTimeFormat(elm.create_time),
  211. update_time: shanghaiTimeFormat(elm.update_time),
  212. })),
  213. });
  214. });
  215. // 根据日期获取数据
  216. router.get("/:book_id/m/:time", async function (req, res) {
  217. const time = req.params.time; // 获取 fileId 参数
  218. const book_id = req.params.book_id; // 获取 fileId 参数
  219. const recordsInfoRes = await getRecordsInfoByMonth(time, book_id);
  220. res.json({
  221. code: 200,
  222. data: recordsInfoRes.map((elm) => ({
  223. ...elm,
  224. time: shanghaiTimeFormat(elm.time, "YYYY-MM-DD"),
  225. create_time: shanghaiTimeFormat(elm.create_time),
  226. update_time: shanghaiTimeFormat(elm.update_time),
  227. })),
  228. });
  229. });
  230. export default router;