فهرست منبع

打开图片失败

john 8 ماه پیش
والد
کامیت
13c3111b60
5فایلهای تغییر یافته به همراه56 افزوده شده و 20 حذف شده
  1. BIN
      .DS_Store
  2. 1 1
      deployment.sh
  3. 1 0
      frontEndMobile/vite.config.ts
  4. 33 8
      node_expores/app.js
  5. 21 11
      node_expores/router/record/index.js

BIN
.DS_Store


+ 1 - 1
deployment.sh

@@ -10,7 +10,7 @@ save_file_name="new_cash_book_node_$version-$current_git_branch_latest_short_id.
 
 # 执行命令
 cd ./frontEndMobile
-yarn install --registry=https://registry.npmmirror.com && yarn build
+yarn install --registry=https://registry.npmmirror.com && npm run build
 rm -rf ../node_expores/public
 cp -rf ./dist ../node_expores/public
 cd ../node_expores

+ 1 - 0
frontEndMobile/vite.config.ts

@@ -4,6 +4,7 @@ import { resolve } from 'path';
 
 // https://vitejs.dev/config/
 export default defineConfig({
+  base: '/static',
   // 路径解析
   resolve: {
     alias: {

+ 33 - 8
node_expores/app.js

@@ -1,7 +1,7 @@
 import express from "express";
 import fileUpload from "express-fileupload";
 import bodyParser from "body-parser";
-import cors from "cors"
+import cors from "cors";
 
 import authors from "./router/authors/index.js";
 import authorsLogin from "./router/authors/login.js";
@@ -14,22 +14,47 @@ import { generateToken, verifyToken } from "#utils";
 
 const port = 3000;
 const app = express();
-const json = express.json({type: '*/json'})
-// 设置静态文件目录
-app.use(express.static('public'));
+const json = express.json({ type: "*/json" });
 
 // 全局启用 CORS
 const corsOptions = {
-    origin: 'http://localhost:3032', // 仅允许这个来源
-    methods: ['GET', 'POST', "PUT", "DELETE"], // 允许的 HTTP 方法
-    allowedHeaders: ['Content-Type', 'Authorization'], // 允许的头部
+  origin: "http://localhost:3032", // 仅允许这个来源
+  methods: ["GET", "POST", "PUT", "DELETE"], // 允许的 HTTP 方法
+  allowedHeaders: ["Content-Type", "Authorization"], // 允许的头部
 };
 app.use(cors(corsOptions));
 app.use(fileUpload());
 app.use(json);
-app.use(bodyParser.urlencoded({extended: false}));
+app.use(bodyParser.urlencoded({ extended: false }));
 
+// Helper function to validate referer
+function isValidReferer(referer) {
+  return (
+    referer.indexOf("zs_interval") > -1 && referer.indexOf("/api/v1/files") > -1
+  );
+}
 
+// Helper function to extract `file_id` from referer
+function extractFileId(referer) {
+  const match = referer.match(/\/api\/v1\/files\/([^/]+)/); // 正则匹配 `file_id`
+  return match ? match[1] : null;
+}
+
+// middleware that is specific to this router
+app.use(async function timeLog(req, res, next) {
+  if (isValidReferer(req.url)) {
+    const fileId = extractFileId(req.url);
+    if (fileId) {
+      return res.redirect(`/api/v1/files/${fileId}`);
+    } else {
+      console.error("File ID not found in referer");
+    }
+  }
+  next();
+});
+
+// 静态文件目录,添加前缀路径 /static
+app.use("/static", express.static("public"));
 
 app.get("/", (req, res) => {
   res.send("Hello World!");

+ 21 - 11
node_expores/router/record/index.js

@@ -102,9 +102,6 @@ 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 host = req.headers['host']; // 主机名 + 端口
-  const origin = req.headers["host"]; // 请求的来源域(适用于跨域)
 
   const typesRes = await getTypesById({
     typeId: recordInfo.type_id,
@@ -116,20 +113,30 @@ router.get("/:record_id", async function (req, res) {
   } 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/v1/files/${elm.file_id}`;
+    }
+    if (`${origin}`.indexOf("zs_interval") > -1) {
+      return `http://${origin.replace('/static/', '')}/api/v1/files/${elm.file_id}`;
+    }
+    return `${origin}api/v1/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) =>
-          `${
-            `${origin}`.indexOf("3032") > -1 ? "http://localhost:3000" : origin
-          }/api/v1/files/${elm.file_id}`
-      ),
+      files: files.map((elm) => getFileUrl(elm)),
     },
   });
 });
@@ -149,6 +156,7 @@ router.put("/:record_id", async function (req, res) {
 
   const getAllfiles = await getFileByRecordId(record_id);
   const getAllfilesIds = getAllfiles.map((elm) => elm.file_id);
+  console.log(159, getAllfiles);
 
   const dellFilesInRecordFiles = getAllfiles.filter(
     (elm) => files.indexOf(elm.file_id) < 0
@@ -159,7 +167,9 @@ router.put("/:record_id", async function (req, res) {
 
   if (dellFilesInRecordFiles.length) {
     await Promise.all(
-      dellFilesInRecordFiles.map((elm) => delFileByRecordId(record_id, file_id))
+      dellFilesInRecordFiles.map((elm) =>
+        delFileByRecordId(record_id, elm.file_id)
+      )
     );
   }
   if (needAddFiles.length) {