|
@@ -26,16 +26,15 @@ class OrderService extends CrudService {
|
|
|
if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
|
|
|
data = JSON.parse(data);
|
|
|
const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
|
|
|
+ let specsData = [];
|
|
|
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);
|
|
|
-
|
|
|
+ specsData = this.setGoodsToPageData(list);
|
|
|
} else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
|
|
|
// 组装页面的数据
|
|
|
const user = this.ctx.user;
|
|
@@ -45,14 +44,56 @@ class OrderService extends CrudService {
|
|
|
// 找到默认地址
|
|
|
const address = await this.addressModel.findOne({ customer, is_default: '1' });
|
|
|
pageData.address = address;
|
|
|
+ // 商品总价,各店铺的价格明细
|
|
|
+ pageData.goodsData = specsData;
|
|
|
+ return pageData;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 整理数据,需要商品与规格的快照,剩下的可以简略
|
|
|
+ * 单商品整理数据,需要商品与规格的快照,剩下的可以简略
|
|
|
* @param {Array} list 规格数组
|
|
|
*/
|
|
|
- setOrderGoodsData(list) {
|
|
|
+ setGoodsToPageData(list) {
|
|
|
// 按店铺分组,精简字段
|
|
|
+ const arr = [];
|
|
|
+ for (const i of list) {
|
|
|
+ const obj = {};
|
|
|
+ obj.shop_name = _.get(i.goods, 'shop.name');
|
|
|
+ obj.shop = _.get(i.goods, 'shop._id');
|
|
|
+ const goods = {};
|
|
|
+ goods.goods_id = _.get(i.goods, '_id');
|
|
|
+ goods.goods_name = _.get(i.goods, 'name');
|
|
|
+ goods.goodsSpec_id = _.get(i, '_id');
|
|
|
+ goods.goodsSpec_name = _.get(i, 'name');
|
|
|
+ goods.freight = _.get(i, 'freight');
|
|
|
+ goods.money = _.get(i, 'sell_money');
|
|
|
+ goods.num = _.get(i, 'num');
|
|
|
+ goods.file = _.get(i.goods, 'file');
|
|
|
+ obj.goods = goods;
|
|
|
+ arr.push(obj);
|
|
|
+ }
|
|
|
+ return arr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重组购物车商品
|
|
|
+ * @param {Object} goods 商品
|
|
|
+ * @param {Object} goodsSpec 商品规格
|
|
|
+ * @param {Object} data 购物车数据
|
|
|
+ */
|
|
|
+ setCartGoodsData(data) {
|
|
|
+ const { goods, goodsSpec, _id, num = 1 } = data;
|
|
|
+ const obj = {};
|
|
|
+ obj.cart_id = _id;
|
|
|
+ obj.goods_id = _.get(goods, '_id');
|
|
|
+ obj.goods_name = _.get(goods, 'name');
|
|
|
+ obj.goodsSpec_id = _.get(goodsSpec, '_id');
|
|
|
+ obj.goodsSpec_name = _.get(goodsSpec, 'name');
|
|
|
+ obj.freight = _.get(goodsSpec, 'freight');
|
|
|
+ obj.money = _.get(goodsSpec, 'sell_money');
|
|
|
+ obj.num = num;
|
|
|
+ obj.file = _.get(goods, 'file');
|
|
|
+ return obj;
|
|
|
}
|
|
|
}
|
|
|
|