Procházet zdrojové kódy

素材共享完成

shaogen1995 před 1 měsícem
rodič
revize
771defbb27

+ 48 - 9
src/controller/issueController.ts

@@ -33,7 +33,7 @@ const issue = {
   // ---------------视频展示
   getVideoList: async (req: any, res: any) => {
     req.apiDescription = '内容发布-获取视频展示列表';
-    const { pageNum = 1, pageSize = 10, searchKey = '' } = req.body;
+    const { pageNum = 1, pageSize = 10, searchKey = '', type = '' } = req.body;
 
     // 构建查询条件
     const query: any = {};
@@ -42,6 +42,7 @@ const issue = {
       // 使用正则表达式实现模糊查询,'i'表示不区分大小写
       query.name = { $regex: searchKey, $options: 'i' };
     }
+    if (type) query.type = type;
 
     // 计算跳过的文档数量
     const skip = (pageNum - 1) * pageSize;
@@ -130,7 +131,7 @@ const issue = {
   // ---------------展品展示
   getGoodsList: async (req: any, res: any) => {
     req.apiDescription = '内容发布-获取展品展示列表';
-    const { pageNum = 1, pageSize = 10, searchKey = '' } = req.body;
+    const { pageNum = 1, pageSize = 10, searchKey = '', type = '' } = req.body;
 
     // 构建查询条件
     const query: any = {};
@@ -139,6 +140,7 @@ const issue = {
       // 使用正则表达式实现模糊查询,'i'表示不区分大小写
       query.name = { $regex: searchKey, $options: 'i' };
     }
+    if (type) query.type = type;
 
     // 计算跳过的文档数量
     const skip = (pageNum - 1) * pageSize;
@@ -268,7 +270,7 @@ const issue = {
   // ---------------先进光源
   getAdvancedList: async (req: any, res: any) => {
     req.apiDescription = '内容发布-获取先进光源列表';
-    const { pageNum = 1, pageSize = 10, searchKey = '' } = req.body;
+    const { pageNum = 1, pageSize = 10, searchKey = '', type = '' } = req.body;
 
     // 构建查询条件
     const query: any = {};
@@ -277,6 +279,7 @@ const issue = {
       // 使用正则表达式实现模糊查询,'i'表示不区分大小写
       query.name = { $regex: searchKey, $options: 'i' };
     }
+    if (type) query.type = type;
 
     // 计算跳过的文档数量
     const skip = (pageNum - 1) * pageSize;
@@ -365,7 +368,7 @@ const issue = {
   // ---------------素材共享
   getShareList: async (req: any, res: any) => {
     req.apiDescription = '内容发布-获取素材共享列表';
-    const { pageNum = 1, pageSize = 10, searchKey = '' } = req.body;
+    const { pageNum = 1, pageSize = 10, searchKey = '', type = '' } = req.body;
 
     // 构建查询条件
     const query: any = {};
@@ -374,6 +377,7 @@ const issue = {
       // 使用正则表达式实现模糊查询,'i'表示不区分大小写
       query.name = { $regex: searchKey, $options: 'i' };
     }
+    if (type) query.type = type;
 
     // 计算跳过的文档数量
     const skip = (pageNum - 1) * pageSize;
@@ -401,11 +405,10 @@ const issue = {
   },
   saveShare: async (req: any, res: any) => {
     if (req.body._id) {
-      // 编辑素材共享
+      // 编辑展品展示
       // 检查数据是否存在
       const existing: any = await Share.findById(req.body._id);
       if (!existing) return resSend(res, 404, '数据不存在');
-
       // 更新字段
       // 过滤一些字段
       const filetStr: string[] = [];
@@ -452,10 +455,46 @@ const issue = {
 
     if (!_id) return resSend(res, 400, '_id不能为空');
 
-    // 根据ID查询信息
-    const info = await Share.findById(_id);
+    // 1. 根据ID查询基本信息
+    const findInfo = await Share.findById(_id);
+    if (!findInfo) return resSend(res, 404, '_id错误或数据已被删除');
+
+    // 2. 如果 fileIds 存在且是非空数组,则查询关联的文件
+    let fileDetails: any[] = [];
+    if (findInfo.fileIds && findInfo.fileIds.length > 0) {
+      // 使用 $in 操作符批量查询 Files 集合
+      fileDetails = await Files.find({
+        _id: { $in: findInfo.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[] = [];
+        findInfo.fileIds.forEach((fileId) => {
+          const file = fileMap.get(fileId.toString());
+          if (file) {
+            sortedFileDetails.push(file);
+          }
+        });
+
+        // 用排序后的数组替换原始查询结果
+        fileDetails = sortedFileDetails;
+      }
+    }
+
+    // 3. 将文件详情合并到返回结果中
+    const info = {
+      ...findInfo.toObject(), // 将Mongoose文档转为普通JS对象
+      fileDetails: fileDetails,
+    };
 
-    if (!info) return resSend(res, 404, '_id错误或数据已被删除');
     req.apiDescription = `内容发布-获取素材共享详情-${info.name}`;
     return resSend(res, 0, '获取素材共享详情成功', info);
   },

+ 0 - 2
src/middleware/validator/issueValidator.ts

@@ -35,6 +35,4 @@ export const addShareVali = errorBack([
   body('releaseDate').notEmpty().withMessage('发布日期不能为空'),
   body('type').notEmpty().withMessage('类别不能为空'),
   body('cover').notEmpty().withMessage('封面地址不能为空'),
-  body('videoUrl').notEmpty().withMessage('视频地址不能为空'),
-  body('videoName').notEmpty().withMessage('视频名称不能为空'),
 ]);

+ 1 - 1
src/model/goodsModel.ts

@@ -37,7 +37,7 @@ const GoodsSchema = new mongoose.Schema({
     type: [String],
     default: [],
   },
-  // 多张图片id集合
+  // 附件id集合
   fileIds: {
     type: [String],
     default: [],

+ 16 - 8
src/model/shareModel.ts

@@ -27,15 +27,23 @@ const ShareSchema = new mongoose.Schema({
     type: String,
     default: '',
   },
-  // 视频地址
-  videoUrl: {
-    type: String,
-    require: true,
-  },
-  videoName: {
-    type: String,
-    require: true,
+  // 图片id集合
+  fileIds: {
+    type: [String],
+    default: [],
   },
+
+  // 视频地址
+  videoUrl: String,
+  videoName: String,
+
+  // 文档地址
+  docUrl: String,
+  docName: String,
+
+  // 图片介绍
+  imgTxt: String,
+
   // 排序值
   sort: {
     type: Number,

+ 3 - 2
src/util/clearAll.ts

@@ -2,7 +2,7 @@ import cron from 'node-cron';
 import fs from 'fs/promises'; // 使用 Promise 版本的 fs API
 import path from 'path';
 
-import { Files, Goods } from '../model/index.js';
+import { Files, Goods, Share } from '../model/index.js';
 import { clearLoginCode } from '../controller/userController.js';
 import { isEnv, upStaticFileUrl, writeLogUrl } from '../config/config.default.js';
 import { AddTxtFileFu } from './index.js';
@@ -139,8 +139,9 @@ export const clearAllFu = () => {
     '0 0 * * *',
     () => {
       console.log('执行定时任务...');
-      // 清理 展品展示中 没有使用的file文件
+      // 清理 多个集合中 有fileIds 没有使用的file文件
       clearFileFu(Goods);
+      clearFileFu(Share);
       // 清理用户登录的验证码对象
       clearLoginCode();
     },