shop.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const { pipeline } = require('stream');
  7. // 店铺类视图
  8. class ShopService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'shop');
  11. this.model = this.ctx.model.Shop.Shop;
  12. }
  13. // 微店页面查询
  14. async microIndex(condition, options) {
  15. // const pipline = [{ $sort: { 'meta.createdAt': -1 } }];
  16. // pipline.push({ $project: { name: 1, file: 1 } });
  17. // pipline.push({ $addFields: { shop_id: { $toString: '$_id' } } });
  18. // // 表关联
  19. // pipline.push({
  20. // $lookup: {
  21. // from: 'goods',
  22. // localField: 'shop_id',
  23. // foreignField: 'shop',
  24. // as: 'goods',
  25. // },
  26. // });
  27. // const list = await this.model.aggregate(pipline);
  28. let list = await this.ctx.service.shop.shop.query(condition, { ...options, projection: [ 'name', 'logo' ] });
  29. list = JSON.parse(JSON.stringify(list));
  30. const arr = [];
  31. for (const s of list) {
  32. const { _id: shop, name, logo } = s;
  33. const market_num = await this.ctx.service.shop.goods.count({ shop });
  34. // TODO: 收藏
  35. const follow_num = 0;
  36. // TODO:当前用户是否收藏
  37. const is_follow = false;
  38. const goodsList = await this.ctx.service.view.goods.indexGoodsList({ shop }, { skip: 0, limit: 3 });
  39. const obj = { _id: shop, logo, name, market_num, follow_num, is_follow, market: goodsList };
  40. arr.push(obj);
  41. }
  42. return arr;
  43. }
  44. }
  45. module.exports = ShopService;