goods.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 GoodsService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'goods');
  10. this.goodsModel = this.ctx.model.Shop.Goods;
  11. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  12. }
  13. /**
  14. *
  15. * @param {Object} query 查询条件
  16. * @param query.id 商品数据id
  17. */
  18. async goodsDetail({ id }) {
  19. const { populate } = this.ctx.service.shop.goods.getRefMods();
  20. let goods = await this.goodsModel.findById(id, { file: 1, tags: 1, name: 1, shot_brief: 1, brief: 1, send_time: 1, shop: 1 }).populate(populate);
  21. if (!goods) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到商品数据');
  22. goods = JSON.parse(JSON.stringify(goods));
  23. let specs = await this.goodsSpecModel.find({ goods: id, status: '0' }, { sell_money: 1, flow_money: 1, freight: 1, name: 1, num: 1 });
  24. specs = JSON.parse(JSON.stringify(specs));
  25. goods = _.omit(goods, [ 'meta', '__v' ]);
  26. const shop = _.pick(goods.shop, [ 'logo', 'name', 'person', 'phone' ]);
  27. delete goods.shop;
  28. // goods: 商品信息; specs:商品规格信息; shop:店铺信息
  29. const returnData = { goods, specs, shop };
  30. return returnData;
  31. }
  32. }
  33. module.exports = GoodsService;