shop.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. // 店铺类视图
  7. class ShopService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'shop');
  10. this.model = this.ctx.model.Shop.Shop;
  11. this.storeShopModel = this.ctx.model.User.StoreShop;
  12. }
  13. // 微店页面查询
  14. async microIndex(condition, options) {
  15. let list = await this.ctx.service.shop.shop.query(condition, { ...options, projection: [ 'name', 'logo' ] });
  16. list = JSON.parse(JSON.stringify(list));
  17. const customer = _.get(this.ctx, 'user._id');
  18. const arr = [];
  19. for (const s of list) {
  20. const { _id: shop, name, logo } = s;
  21. const market_num = await this.ctx.service.shop.goods.count({ shop, status: '1' });
  22. // 收藏
  23. const follow_num = await this.storeShopModel.count({ shop });
  24. // 当前用户是否收藏
  25. let is_follow = false;
  26. if (customer) {
  27. const store_num = await this.storeShopModel.count({ shop, customer });
  28. is_follow = store_num > 0;
  29. }
  30. const res = await this.ctx.service.view.goods.indexGoodsList({ shop }, { skip: 0, limit: 3 });
  31. const obj = { _id: shop, logo, name, market_num, follow_num, is_follow, market: res.list };
  32. arr.push(obj);
  33. }
  34. return arr;
  35. }
  36. }
  37. module.exports = ShopService;