1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- //
- class OrderService extends CrudService {
- constructor(ctx) {
- super(ctx, 'order');
- this.redis = this.app.redis;
- this.redisKey = this.app.config.redisKey;
- this.model = this.ctx.model.Trade.Order;
- this.goodsModel = this.ctx.model.Shop.Goods;
- this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
- this.addressModel = this.ctx.model.User.Address;
- }
- /**
- * 进入下单页面
- * @param {Object} body 请求参数
- * @param body.key 缓存key
- */
- async toMakeOrder({ key }) {
- key = `${this.redisKey.orderKeyPrefix}${key}`;
- let data = await this.redis.get(key);
- if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
- data = JSON.parse(data);
- const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
- if (_.isArray(data)) {
- // 购物车来的: 1.循环校验 规格商品; 2.按店铺分组
- console.log('line 31 in function:cart');
- } else if (_.isObject(data)) {
- // 商品页单独买: 1.校验规格商品; 2:按店铺分组
- const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);
- if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
- const list = await this.goodsSpecModel.find({ _id: data.goodsSpec }).populate(populate);
- console.log(list);
- } else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
- // 组装页面的数据
- const user = this.ctx.user;
- const customer = _.get(user, '_id');
- if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
- const pageData = {};
- // 找到默认地址
- const address = await this.addressModel.findOne({ customer, is_default: '1' });
- pageData.address = address;
- }
- /**
- * 整理数据,需要商品与规格的快照,剩下的可以简略
- * @param {Array} list 规格数组
- */
- setOrderGoodsData(list) {
- // 按店铺分组,精简字段
- }
- }
- module.exports = OrderService;
|