|
|
@@ -1,5 +1,5 @@
|
|
|
import dayjs from 'dayjs';
|
|
|
-import { Dict } from '../model/index.js';
|
|
|
+import { Dict, Video } from '../model/index.js';
|
|
|
import resSend from '../util/resSend.js';
|
|
|
import { passWordJia } from '../util/pass.js';
|
|
|
|
|
|
@@ -29,6 +29,100 @@ const issue = {
|
|
|
await introObj.save();
|
|
|
return resSend(res, 0, '编辑项目简介成功');
|
|
|
},
|
|
|
+ getVideoList: async (req: any, res: any) => {
|
|
|
+ req.apiDescription = '内容发布-获取视频展示列表';
|
|
|
+ const { pageNum = 1, pageSize = 10, searchKey = '' } = req.body;
|
|
|
+
|
|
|
+ // 构建查询条件
|
|
|
+ const query: any = {};
|
|
|
+
|
|
|
+ if (searchKey) {
|
|
|
+ // 使用正则表达式实现模糊查询,'i'表示不区分大小写
|
|
|
+ query.name = { $regex: searchKey, $options: 'i' };
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算跳过的文档数量
|
|
|
+ const skip = (pageNum - 1) * pageSize;
|
|
|
+
|
|
|
+ // 修改排序逻辑:先按sort字段降序,再按updateTime字段降序
|
|
|
+ const sortCondition: any = { sort: -1, updateTime: -1 };
|
|
|
+
|
|
|
+ // 并行执行:获取总条数和查询当前页数据
|
|
|
+ const [total, data] = await Promise.all([
|
|
|
+ // 获取满足条件的总记录数
|
|
|
+ Video.countDocuments(query),
|
|
|
+ // 查询当前页数据
|
|
|
+ Video.find(query).skip(skip).limit(parseInt(pageSize)).sort(sortCondition), // 按sort字段降序,数字越大越靠前;相同则按updateTime降序
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 计算总页数
|
|
|
+ const totalPages = Math.ceil(total / pageSize);
|
|
|
+ return resSend(res, 0, '获取视频展示列表成功', {
|
|
|
+ list: data,
|
|
|
+ pageNum: parseInt(pageNum),
|
|
|
+ pageSize: parseInt(pageSize),
|
|
|
+ total,
|
|
|
+ totalPages,
|
|
|
+ });
|
|
|
+ },
|
|
|
+ saveVideo: async (req: any, res: any) => {
|
|
|
+ if (req.body._id) {
|
|
|
+ // 编辑视频展示
|
|
|
+ // 检查数据是否存在
|
|
|
+ const existingVideo: any = await Video.findById(req.body._id);
|
|
|
+ if (!existingVideo) return resSend(res, 404, '数据不存在');
|
|
|
+ // 更新字段
|
|
|
+
|
|
|
+ // 过滤一些字段
|
|
|
+ const filetStr: string[] = [];
|
|
|
+
|
|
|
+ Object.keys(req.body).forEach((key) => {
|
|
|
+ if (key !== '_id' && req.body[key] !== undefined) {
|
|
|
+ if (!filetStr.includes(key)) {
|
|
|
+ existingVideo[key] = req.body[key];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ existingVideo.updateTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
|
|
|
+ const updatedUser = await existingVideo.save();
|
|
|
+
|
|
|
+ const videoObj = updatedUser.toObject();
|
|
|
+ req.apiDescription = `内容发布-编辑视频展示-${videoObj.name}`;
|
|
|
+ return resSend(res, 0, '编辑视频展示成功', videoObj);
|
|
|
+ } else {
|
|
|
+ const userModel = new Video(req.body);
|
|
|
+ // 新增视频展示
|
|
|
+ // 保存数据到数据库
|
|
|
+ const dbBack = await userModel.save();
|
|
|
+ // 将文档转换为普通对象
|
|
|
+ const videoObj = dbBack.toObject();
|
|
|
+ req.apiDescription = `内容发布-新增视频展示-${videoObj.name}`;
|
|
|
+ return resSend(res, 0, '新增视频展示成功', videoObj);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ delVideo: async (req: any, res: any) => {
|
|
|
+ const { _id } = req.params; // 从URL参数中获取ID
|
|
|
+ // 1. 根据ID查找数据
|
|
|
+ const info = await Video.findById(_id);
|
|
|
+ if (!info) return resSend(res, 404, '_id错误或数据已被删除');
|
|
|
+
|
|
|
+ const deletedInfo: any = await Video.findByIdAndDelete(_id);
|
|
|
+ req.apiDescription = `内容发布-删除视频展示数据-${deletedInfo.name}`;
|
|
|
+ return resSend(res, 0, '删除视频展示数据成功');
|
|
|
+ },
|
|
|
+ getVideoInfo: async (req: any, res: any) => {
|
|
|
+ const { _id } = req.params;
|
|
|
+
|
|
|
+ if (!_id) return resSend(res, 400, '_id不能为空');
|
|
|
+
|
|
|
+ // 根据ID查询信息
|
|
|
+ const info = await Video.findById(_id);
|
|
|
+
|
|
|
+ if (!info) return resSend(res, 404, '_id错误或数据已被删除');
|
|
|
+ req.apiDescription = `内容发布-获取视频展示详情-${info.name}`;
|
|
|
+ return resSend(res, 0, '获取视频展示详情成功', info);
|
|
|
+ },
|
|
|
};
|
|
|
|
|
|
export default issue;
|