lrf 2 tahun lalu
induk
melakukan
2d1eb0fa6e

+ 5 - 0
app/controller/user/storeShop.js

@@ -9,5 +9,10 @@ class StoreShopController extends Controller {
     super(ctx);
     this.service = this.ctx.service.user.storeShop;
   }
+  async userView() {
+    const { skip, limit } = this.ctx.query;
+    const { list, total } = await this.service.userView(this.ctx.query, { skip, limit });
+    this.ctx.ok({ data: list, total });
+  }
 }
 module.exports = CrudController(StoreShopController, meta);

+ 2 - 3
app/service/user/storeGoods.js

@@ -35,7 +35,7 @@ class StoreGoodsService extends CrudService {
   async userView(query = {}, { skip, limit } = {}) {
     const customer = _.get(this.ctx, 'user._id');
     assert(customer, '缺少用户信息');
-    const { view_num, sell_num, sell_money, name, shop } = query;
+    const { view_num, sell_num, sell_money, name, shop, time } = query;
     const searchPips = [];
     if (name) searchPips.push({ $match: { name: new RegExp(name) } });
     if (shop) searchPips.push({ $match: { shop } });
@@ -108,9 +108,8 @@ class StoreGoodsService extends CrudService {
     if (view_num) sort.view_num = parseInt(view_num);
     if (sell_num) sort.sell_num = parseInt(sell_num);
     if (sell_money) sort.sell_money = parseInt(sell_money);
-    sort.time = -1;
+    sort.time = time || -1;
     qPipline.push({ $sort: sort });
-    console.log(qPipline);
     if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });
     if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
     const list = await this.model.aggregate(qPipline);

+ 45 - 1
app/service/user/storeShop.js

@@ -25,12 +25,56 @@ class StoreShopService extends CrudService {
     return { msg: '收藏成功', result: true };
   }
 
-
   // 检查是否收藏店铺
   async check({ shop, customer }) {
     const num = await this.model.count({ shop, customer });
     return num > 0;
   }
+
+  async userView(query = {}, { skip, limit } = {}) {
+    const customer = _.get(this.ctx, 'user._id');
+    assert(customer, '缺少用户信息');
+    const { name, time } = query;
+    const searchPips = { };
+    const sort = { time: -1 };
+    if (name) searchPips.name = new RegExp(name);
+    if (time) sort.time = time;
+
+    const pipline = [{ $match: { customer } }];
+    // 关联店铺
+    pipline.push({ $addFields: { shop_id: { $toObjectId: '$shop' } } });
+    pipline.push({
+      $lookup: {
+        from: 'shop',
+        localField: 'shop_id',
+        foreignField: '_id',
+        as: 's',
+      },
+    });
+    pipline.push({ $unwind: '$s' });
+    // 格式化数据
+    pipline.push({
+      $project: {
+        _id: '$s._id',
+        time: 1,
+        name: '$s.name',
+        logo: '$s.logo',
+        status: '$s.status',
+        address: '$s.address',
+        phone: '$s.phone',
+      },
+    });
+    // 条件查询
+    if (searchPips.length > 0) pipline.push(...searchPips);
+    // 联表-规格
+    const qPipline = _.cloneDeep(pipline);
+    qPipline.push({ $sort: sort });
+    const list = await this.model.aggregate(qPipline);
+    const tPipline = _.cloneDeep(pipline);
+    tPipline.push({ $count: 'total' });
+    const total = await this.model.aggregate(tPipline);
+    return { list, total: _.get(_.head(total), 'total', 0) };
+  }
 }
 
 module.exports = StoreShopService;

+ 1 - 0
app/z_router/user/storeShop.js

@@ -7,6 +7,7 @@ const rkey = 'storeShop';
 const ckey = 'user.storeShop';
 const keyZh = '收藏店铺';
 const routes = [
+  { method: 'get', path: `${rkey}/userView`, controller: `${ckey}.userView`, name: `${ckey}userView`, zh: `${keyZh}-用户查看收藏` },
   { method: 'get', path: `${rkey}/check`, controller: `${ckey}.check`, name: `${ckey}check`, zh: `${keyZh}-检查是否收藏` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },