|
@@ -8,43 +8,52 @@ 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 fromCartToOrder(body) {}
|
|
|
+ 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);
|
|
|
|
|
|
- /**
|
|
|
- * 从商品页直接购买进入下单页
|
|
|
- * @param {Object} body 请求参数
|
|
|
- * @param body.shop 店铺id
|
|
|
- * @param body.goods 商品id
|
|
|
- * @param body.goodsSpec 商品规格id
|
|
|
- * @param body.num 购买数量
|
|
|
- */
|
|
|
- async fromGoodsToOrder({ shop, goods, goodsSpec, num }) {
|
|
|
- const { result, msg } = await this.ctx.service.util.trade.checkCanBuy({ shop, goods, goodsSpec, num });
|
|
|
- if (!result) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
|
|
|
+ } 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 data = {};
|
|
|
+ const pageData = {};
|
|
|
// 找到默认地址
|
|
|
const address = await this.addressModel.findOne({ customer, is_default: '1' });
|
|
|
- data.address = address;
|
|
|
- // 组装商品数据
|
|
|
- const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
|
|
|
- const goodsSpecData = await this.goodsSpecModel.findById(goodsSpec).populate(populate);
|
|
|
- // TODO 优惠券列表 及 其他优惠
|
|
|
+ pageData.address = address;
|
|
|
}
|
|
|
|
|
|
- setOrderGoodsData() {}
|
|
|
+ /**
|
|
|
+ * 整理数据,需要商品与规格的快照,剩下的可以简略
|
|
|
+ * @param {Array} list 规格数组
|
|
|
+ */
|
|
|
+ setOrderGoodsData(list) {
|
|
|
+ // 按店铺分组,精简字段
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = OrderService;
|