order.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 OrderService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'order');
  10. this.model = this.ctx.model.Trade.Order;
  11. this.goodsModel = this.ctx.model.Shop.Goods;
  12. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  13. this.addressModel = this.ctx.model.User.Address;
  14. }
  15. /**
  16. * 从购物车页进入下单页面
  17. * @param {Object} body 请求参数
  18. */
  19. async fromCartToOrder(body) {}
  20. /**
  21. * 从商品页直接购买进入下单页
  22. * @param {Object} body 请求参数
  23. * @param body.goods_id 商品id
  24. * @param body.goodsSpec_id 商品规格id
  25. * @param body.num 购买数量
  26. */
  27. async fromGoodsToOrder({ goods_id, goodsSpec_id, num }) {
  28. const { result, msg } = await this.ctx.service.util.trade.checkCanBuy({ goods_id, goodsSpec_id, num });
  29. if (!result) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
  30. // 组装页面的数据
  31. const user = this.ctx.user;
  32. const customer = _.get(user, '_id');
  33. if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
  34. // 找到默认地址
  35. const address = await this.addressModel.findOne({ customer, is_default: '1' });
  36. // 组装商品数据
  37. // TODO 优惠券列表 及 其他优惠
  38. }
  39. }
  40. module.exports = OrderService;