lrf 2 năm trước cách đây
mục cha
commit
36e1ceb36b

+ 24 - 0
app/controller/view/config/.shop.js

@@ -0,0 +1,24 @@
+module.exports = {
+  microIndex: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        user: 'user',
+        name: '%name%',
+        code: 'code',
+        person: 'person',
+        phone: 'phone',
+        status: 'status',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+    },
+  },
+};

+ 13 - 0
app/controller/view/shop.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.shop.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class ShopController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.view.shop;
+  }
+}
+module.exports = CrudController(ShopController, meta);

+ 49 - 0
app/service/view/shop.js

@@ -0,0 +1,49 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+const { pipeline } = require('stream');
+
+// 店铺类视图
+class ShopService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'shop');
+    this.model = this.ctx.model.Shop.Shop;
+  }
+
+  // 微店页面查询
+  async microIndex(condition, options) {
+    // const pipline = [{ $sort: { 'meta.createdAt': -1 } }];
+    // pipline.push({ $project: { name: 1, file: 1 } });
+    // pipline.push({ $addFields: { shop_id: { $toString: '$_id' } } });
+    // // 表关联
+    // pipline.push({
+    //   $lookup: {
+    //     from: 'goods',
+    //     localField: 'shop_id',
+    //     foreignField: 'shop',
+    //     as: 'goods',
+    //   },
+    // });
+    // const list = await this.model.aggregate(pipline);
+    let list = await this.ctx.service.shop.shop.query(condition, { ...options, projection: [ 'name', 'file' ] });
+    list = JSON.parse(JSON.stringify(list));
+    const arr = [];
+    for (const s of list) {
+      const { _id: shop, name, file } = s;
+      const market_num = await this.ctx.service.shop.goods.count({ shop });
+      // TODO: 收藏
+      const follow_num = 0;
+      // TODO:当前用户是否收藏
+      const is_follow = false;
+      const goodsList = await this.ctx.service.view.goods.indexGoodsList({ shop }, { skip: 0, limit: 3 });
+      const obj = { _id: shop, file, name, market_num, follow_num, is_follow, market: goodsList };
+      arr.push(obj);
+    }
+
+    return arr;
+  }
+}
+
+module.exports = ShopService;

+ 1 - 0
app/z_router/view/index.js

@@ -3,4 +3,5 @@
  */
 module.exports = app => {
   require('./goods')(app); // 商品视图
+  require('./shop')(app); // 店铺视图
 };

+ 13 - 0
app/z_router/view/shop.js

@@ -0,0 +1,13 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'viewShop';
+const ckey = 'view.shop';
+const keyZh = 'dianpu相关视图';
+const routes = [{ method: 'get', path: `${rkey}/microIndex`, controller: `${ckey}.microIndex`, name: `${ckey}microIndex`, zh: '微店页面查询' }];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};