shaogen1995 hai 2 semanas
pai
achega
ba2dbbc764
Modificáronse 2 ficheiros con 41 adicións e 5 borrados
  1. 1 1
      src/controller/fileController.ts
  2. 40 4
      src/controller/issueController.ts

+ 1 - 1
src/controller/fileController.ts

@@ -9,7 +9,7 @@ const file = {
   upload: async (req: any, res: any) => {
     if (!req.file) return resSend(res, 404, '请选择要上传的文件');
 
-    const { isDb, myType } = req.body;
+    const { isDb, type: myType } = req.body;
 
     const upPath = req.query.upPath || 'default';
 

+ 40 - 4
src/controller/issueController.ts

@@ -1,5 +1,5 @@
 import dayjs from 'dayjs';
-import { Dict, Goods, Video } from '../model/index.js';
+import { Dict, Files, Goods, Video } from '../model/index.js';
 import resSend from '../util/resSend.js';
 import { passWordJia } from '../util/pass.js';
 
@@ -215,10 +215,46 @@ const issue = {
 
     if (!_id) return resSend(res, 400, '_id不能为空');
 
-    // 根据ID查询信息
-    const info = await Goods.findById(_id);
+    // 1. 根据ID查询商品基本信息
+    const goodsInfo = await Goods.findById(_id);
+    if (!goodsInfo) return resSend(res, 404, '_id错误或数据已被删除');
+
+    // 2. 如果 fileIds 存在且是非空数组,则查询关联的文件
+    let fileDetails: any[] = [];
+    if (goodsInfo.fileIds && goodsInfo.fileIds.length > 0) {
+      // 使用 $in 操作符批量查询 Files 集合
+      fileDetails = await Files.find({
+        _id: { $in: goodsInfo.fileIds },
+      });
+      // 3. 按照 fileIds 数组的顺序对 fileDetails 进行排序
+      if (fileDetails.length > 0) {
+        // 创建一个映射,将文件的 _id 映射到文件对象
+        const fileMap = new Map();
+        fileDetails.forEach((file) => {
+          // 统一转为字符串进行比较
+          fileMap.set(file._id.toString(), file);
+        });
+
+        // 按照 fileIds 的顺序重新构建 fileDetails 数组
+        const sortedFileDetails: any[] = [];
+        goodsInfo.fileIds.forEach((fileId) => {
+          const file = fileMap.get(fileId.toString());
+          if (file) {
+            sortedFileDetails.push(file);
+          }
+        });
+
+        // 用排序后的数组替换原始查询结果
+        fileDetails = sortedFileDetails;
+      }
+    }
+
+    // 3. 将文件详情合并到返回结果中
+    const info = {
+      ...goodsInfo.toObject(), // 将Mongoose文档转为普通JS对象
+      fileDetails: fileDetails,
+    };
 
-    if (!info) return resSend(res, 404, '_id错误或数据已被删除');
     req.apiDescription = `内容发布-获取展品展示详情-${info.name}`;
     return resSend(res, 0, '获取展品展示详情成功', info);
   },