Browse Source

商品规格排序

lrf 2 năm trước cách đây
mục cha
commit
b4487c2eea
2 tập tin đã thay đổi với 27 bổ sung1 xóa
  1. 3 1
      app/controller/shop/config/.goodsSpec.js
  2. 24 0
      app/service/shop/goodsSpec.js

+ 3 - 1
app/controller/shop/config/.goodsSpec.js

@@ -25,6 +25,7 @@ module.exports = {
         name: 'name',
         status: 'status',
         can_group: 'can_group',
+        sort: 'sort',
       },
       // options: {
       //   "meta.state": 0 // 默认条件
@@ -33,7 +34,8 @@ module.exports = {
     service: 'query',
     options: {
       query: ['skip', 'limit'],
-      sort: ['sort', 'meta.createdAt'],
+      // sort: [{ sort: -1 }, { 'meta.createdAt': -1 }],
+      sort: { sort: -1, 'meta.createdAt': -1 },
       desc: true,
       count: true,
     },

+ 24 - 0
app/service/shop/goodsSpec.js

@@ -10,6 +10,30 @@ class GoodsSpecService extends CrudService {
     super(ctx, 'goodsspec');
     this.model = this.ctx.model.Shop.GoodsSpec;
   }
+  async query(filter, { skip = 0, limit, sort, projection } = {}) {
+    const { sort: fs } = filter;
+    console.log(fs);
+    if (fs) {
+      sort.sort = parseInt(fs);
+      delete filter.sort;
+    }
+    // 处理排序
+    // if (sort && _.isString(sort)) {
+    //   sort = { [sort]: desc ? -1 : 1 };
+    // } else if (sort && _.isArray(sort)) {
+    //   sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
+    // }
+    let condition = _.cloneDeep(filter);
+    condition = await this.beforeQuery(condition);
+    condition = this.dealFilter(condition);
+    // 过滤出ref字段
+    const { refMods, populate } = this.getRefMods();
+    // 带ref查询
+    let rs = await this.model.find(condition, projection, { skip, limit, sort }).populate(populate).exec();
+    rs = JSON.parse(JSON.stringify(rs));
+    rs = await this.afterQuery(filter, rs);
+    return rs;
+  }
 }
 
 module.exports = GoodsSpecService;