瀏覽代碼

feat:添加查询文章字段和修改文章排序

wangsisi 13 小時之前
父節點
當前提交
22389ee822
共有 1 個文件被更改,包括 27 次插入1 次删除
  1. 27 1
      routes/admin/articles.js

+ 27 - 1
routes/admin/articles.js

@@ -17,6 +17,7 @@ const {Op} = require('sequelize')
  * @apiParam {String|Array} [cropIds] 作物筛选ID,支持逗号分隔的多个ID或数组形式
  * @apiParam {Number} [categoryId] 用户分类ID,根据用户传递的category参数值进行精确匹配
  * @apiParam {Number} [isRecommended] 推荐筛选,0-非推荐文章,1-推荐文章
+ * @apiParam {String|Array} [newsTypeIds] 文章类型筛选ID,支持逗号分隔的多个ID或数组形式,根据type字段进行筛选
  * 
  * @apiSuccess {Boolean} status 请求状态
  * @apiSuccess {String} message 响应消息
@@ -108,9 +109,14 @@ router.get('/', async function(req, res, next) {
         const offset = (currentPage - 1) * pageSize
 
         const condition = {
-            order:[['id','DESC']],
+            order:[['updatedAt','DESC']],
             limit:pageSize,
             offset,
+            attributes: [
+                'id', 'title', 'subtitle', 'content', 'type', 'img', 'date', 
+                'author', 'category', 'crop', 'isRecommended', 'seoKeyword', 
+                'seoDescription', 'createdAt', 'updatedAt'
+            ],
             include: [{
                 model: Category,
                 as: 'cropInfo',
@@ -168,6 +174,26 @@ router.get('/', async function(req, res, next) {
             }
         }
 
+        // 文章类型筛选 - 支持多选
+        if(query.newsTypeIds){
+            let typeIds = [];
+            
+            // 处理newsTypeIds参数(支持逗号分隔的多个ID)
+            if(typeof query.newsTypeIds === 'string'){
+                typeIds = query.newsTypeIds.split(',').map(id => parseInt(id.trim())).filter(id => !isNaN(id));
+            } else if(Array.isArray(query.newsTypeIds)){
+                typeIds = query.newsTypeIds.map(id => parseInt(id)).filter(id => !isNaN(id));
+            } else {
+                typeIds = [parseInt(query.newsTypeIds)].filter(id => !isNaN(id));
+            }
+
+            if(typeIds.length > 0){
+                whereConditions.type = {
+                    [Op.in]: typeIds
+                };
+            }
+        }
+
         // 如果有查询条件,添加到condition中
         if(Object.keys(whereConditions).length > 0){
             condition.where = whereConditions;