utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import {
  2. record_insert,
  3. record_update,
  4. isType,
  5. type_insert,
  6. getRecordInfoById,
  7. addFileByRecordId,
  8. getFileByRecordId,
  9. getRecordsInfoByTime,
  10. getRecordsInfoByMonth,
  11. getTypesById,
  12. delFileByRecordId,
  13. delByRecordId,
  14. } from "#db";
  15. import { shanghaiTime, shanghaiTimeFormat } from "#utils";
  16. export async function getTypeInfoFn({ userInfo, book_id, type }) {
  17. console.log(1919191919, userInfo, book_id, type )
  18. if (!type) {
  19. return "";
  20. }
  21. // type 是否存在重复项,有就返回id,没有就新增 再返回id
  22. const isAddType = await isType({
  23. book_id,
  24. author_id: userInfo.user_id,
  25. type,
  26. });
  27. let typeInfo = {};
  28. if (!isAddType) {
  29. await type_insert({
  30. book_id,
  31. author_id: userInfo.user_id,
  32. type,
  33. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  34. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  35. });
  36. typeInfo = await isType({
  37. book_id,
  38. author_id: userInfo.user_id,
  39. type,
  40. });
  41. } else {
  42. typeInfo = { ...isAddType };
  43. }
  44. return Promise.resolve(typeInfo.id);
  45. }
  46. export async function setFilesByRecord(params) {
  47. const {files = [], more_id, userInfo, book_id} = params
  48. if(!files.length) return
  49. await Promise.all(
  50. files.map((file_id) =>
  51. addFileByRecordId({
  52. file_id,
  53. record_id: more_id,
  54. book_id,
  55. author_id: userInfo.user_id,
  56. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  57. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  58. })
  59. )
  60. );
  61. }
  62. export async function setFilesById(params) {
  63. const {record_id, userInfo, book_id, files = []} = params
  64. const getAllFiles = await getFileByRecordId(record_id);
  65. const getAllFilesIds = getAllFiles.map((elm) => elm.file_id);
  66. const dellFilesInRecordFiles = getAllFiles.filter(
  67. (elm) => files.indexOf(elm.file_id) < 0
  68. );
  69. const needAddFiles = files.filter(
  70. (file_id) => getAllFilesIds.indexOf(file_id) < 0
  71. );
  72. if (dellFilesInRecordFiles.length) {
  73. await Promise.all(
  74. dellFilesInRecordFiles.map((elm) =>
  75. delFileByRecordId(record_id, elm.file_id)
  76. )
  77. );
  78. }
  79. if (needAddFiles.length) {
  80. // 更新附件信息
  81. await Promise.all(
  82. needAddFiles.map((file_id) =>
  83. addFileByRecordId({
  84. file_id,
  85. record_id,
  86. book_id,
  87. author_id: userInfo.user_id,
  88. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  89. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  90. })
  91. )
  92. );
  93. }
  94. }
  95. export function getFileUrl(req,elm) {
  96. // 获取请求的来源域名
  97. const host = req.headers["host"]; // 主机名 + 端口
  98. const origin = req.headers["referer"]; // 请求的来源域(适用于跨域)
  99. if (`${origin}`.indexOf("3032") > -1 || `${host}`.indexOf("3000") > -1) {
  100. return "http://localhost:3000" + `/api_files_${elm.file_id}`;
  101. }
  102. if (`${origin}`.indexOf("zs_interval") > -1) {
  103. return `api_files_${elm.file_id}`;
  104. }
  105. return `${origin}api_files_${elm.file_id}`;
  106. }