|
@@ -14,6 +14,8 @@ class OrderService extends CrudService {
|
|
|
this.goodsModel = this.ctx.model.Shop.Goods;
|
|
|
this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
|
|
|
this.addressModel = this.ctx.model.User.Address;
|
|
|
+
|
|
|
+ this.cartModel = this.ctx.model.Trade.Cart;
|
|
|
}
|
|
|
/**
|
|
|
* 进入下单页面
|
|
@@ -25,14 +27,22 @@ class OrderService extends CrudService {
|
|
|
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();
|
|
|
+ console.log(data);
|
|
|
let specsData = [];
|
|
|
if (_.isArray(data)) {
|
|
|
// 购物车来的: 1.循环校验 规格商品; 2.按店铺分组
|
|
|
+ const { populate } = this.ctx.service.trade.cart.getRefMods();
|
|
|
+ const carts = await this.cartModel.find({ _id: data }).populate(populate);
|
|
|
+ for (const cart of carts) {
|
|
|
+ const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(cart, false);
|
|
|
+ if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
|
|
|
+ }
|
|
|
+ specsData = this.setCartsGoodsToPageData(carts);
|
|
|
} 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 { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
|
|
|
const list = await this.goodsSpecModel.find({ _id: data.goodsSpec }).populate(populate);
|
|
|
specsData = this.setGoodsToPageData(list, data.num);
|
|
|
} else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
|
|
@@ -44,7 +54,7 @@ class OrderService extends CrudService {
|
|
|
// 找到默认地址
|
|
|
const address = await this.addressModel.findOne({ customer, is_default: '1' });
|
|
|
pageData.address = address;
|
|
|
- // 商品总价,各店铺的价格明细
|
|
|
+ // // 商品总价,各店铺的价格明细
|
|
|
specsData = this.computedShopTotal(specsData);
|
|
|
const shopTotalDetail = this.computedAllTotal(specsData);
|
|
|
pageData.goodsData = specsData;
|
|
@@ -59,6 +69,7 @@ class OrderService extends CrudService {
|
|
|
for (const i of list) {
|
|
|
i.goods_total = i.goods.reduce((p, n) => p + (n.money || 0) * (n.num || 0), 0);
|
|
|
i.freight_total = i.goods.reduce((p, n) => p + (n.freight || 0) * (n.num || 0), 0);
|
|
|
+ console.log(i);
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
@@ -76,7 +87,7 @@ class OrderService extends CrudService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 单商品整理数据,需要商品与规格的快照,剩下的可以简略
|
|
|
+ * 单商品整理数据,剩下的可以简略
|
|
|
* @param {Array} list 规格数组
|
|
|
* @param num 购买数量
|
|
|
*/
|
|
@@ -101,6 +112,38 @@ class OrderService extends CrudService {
|
|
|
}
|
|
|
return arr;
|
|
|
}
|
|
|
+ /**
|
|
|
+ *购物车数据整理
|
|
|
+ * @param {Array} list 规格数组
|
|
|
+ */
|
|
|
+ setCartsGoodsToPageData(list) {
|
|
|
+ const arr = [];
|
|
|
+ list = _.groupBy(list, 'shop._id');
|
|
|
+ for (const key in list) {
|
|
|
+ const data = list[key];
|
|
|
+ const shopData = _.get(_.head(data), 'shop');
|
|
|
+ const obj = {};
|
|
|
+ obj.shop = _.get(shopData, '_id');
|
|
|
+ obj.shop_name = _.get(shopData, 'name');
|
|
|
+ const goodsList = [];
|
|
|
+ for (const i of data) {
|
|
|
+ const goods = {};
|
|
|
+ goods.cart_id = _.get(i, '_id');
|
|
|
+ goods.goods_id = _.get(i.goods, '_id');
|
|
|
+ goods.goods_name = _.get(i.goods, 'name');
|
|
|
+ goods.goodsSpec_id = _.get(i.goodsSpec, '_id');
|
|
|
+ goods.goodsSpec_name = _.get(i.goodsSpec, 'name');
|
|
|
+ goods.freight = _.get(i.goodsSpec, 'freight');
|
|
|
+ goods.money = _.get(i.goodsSpec, 'sell_money');
|
|
|
+ goods.num = _.get(i, 'num');
|
|
|
+ goods.file = _.get(i.goods, 'file', []);
|
|
|
+ goodsList.push(goods);
|
|
|
+ }
|
|
|
+ obj.goods = goodsList;
|
|
|
+ arr.push(obj);
|
|
|
+ }
|
|
|
+ return arr;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 重组购物车商品
|