소스 검색

首页商品查询

lrf 2 년 전
부모
커밋
8512208156
3개의 변경된 파일30개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 0
      app/controller/view/config/.goods.js
  2. 25 0
      app/service/view/goods.js
  3. 4 1
      app/z_router/view/goods.js

+ 1 - 0
app/controller/view/config/.goods.js

@@ -2,4 +2,5 @@ module.exports = {
   goodsDetail: {
     requestBody: ['!id'],
   },
+  indexGoodsList: {},
 };

+ 25 - 0
app/service/view/goods.js

@@ -35,6 +35,31 @@ class GoodsService extends CrudService {
     await this.goodsModel.updateOne({ _id: id }, { view_num: (goods.view_num || 0) + 1 });
     return returnData;
   }
+
+
+  async indexGoodsList({ skip = 0, limit = 20 } = {}) {
+    const list = await this.goodsModel.aggregate([
+      {
+        $lookup: {
+          from: 'goodsSpec',
+          localField: 'id',
+          foreignField: 'shop',
+          as: 'specs',
+        },
+      },
+      {
+        $project: { name: 1, file: 1, specs: 1 },
+      },
+      { $skip: parseInt(skip) },
+      { $limit: parseInt(limit) },
+    ]);
+    for (const i of list) {
+      const spec = _.minBy(i.specs, 'sell_money');
+      i.sell_money = _.get(spec, 'sell_money');
+      delete i.specs;
+    }
+    return list;
+  }
 }
 
 module.exports = GoodsService;

+ 4 - 1
app/z_router/view/goods.js

@@ -6,7 +6,10 @@ const routerRegister = require(regPath);
 const rkey = 'viewGoods';
 const ckey = 'view.goods';
 const keyZh = '商品相关视图';
-const routes = [{ method: 'post', path: `${rkey}/goodsDetail`, controller: `${ckey}.goodsDetail`, name: `${ckey}GoodsDetail`, zh: '商品页' }];
+const routes = [
+  { method: 'post', path: `${rkey}/goodsDetail`, controller: `${ckey}.goodsDetail`, name: `${ckey}GoodsDetail`, zh: '商品页' },
+  { method: 'get', path: `${rkey}/indexGoodsList`, controller: `${ckey}.indexGoodsList`, name: `${ckey}indexGoodsList`, zh: '首页商品列表' },
+];
 
 module.exports = app => {
   routerRegister(app, routes, keyZh, rkey, ckey);