storeShop.js 1.1 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. const moment = require('moment');
  7. //
  8. class StoreShopService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'storeshop');
  11. this.model = this.ctx.model.User.StoreShop;
  12. }
  13. async create({ shop }) {
  14. assert(shop, '缺少店铺信息');
  15. const customer = _.get(this.ctx, 'user._id');
  16. assert(customer, '缺少用户信息');
  17. let data = await this.model.findOne({ customer, shop });
  18. if (data) {
  19. await this.model.deleteOne({ customer, shop });
  20. return { msg: '取消收藏', result: false };
  21. }
  22. data = await this.model.create({ customer, shop, time: moment().format('YYYY-MM-DD HH:mm:ss') });
  23. return { msg: '收藏成功', result: true };
  24. }
  25. // 用户查看收藏店铺
  26. async userView() {}
  27. // 检查是否收藏店铺
  28. async check({ shop, customer }) {
  29. const num = await this.model.count({ shop, customer });
  30. return num > 0;
  31. }
  32. }
  33. module.exports = StoreShopService;