|
@@ -190,6 +190,7 @@ class OrderService extends CrudService {
|
|
|
* 进入下单页面
|
|
|
* @param {Object} body 请求参数
|
|
|
* @param body.key 缓存key
|
|
|
+ * @property {Array} data key中的数据,可能是 [string],[object]; [string]:数据是购物车id; [object] :是直接购买的数据
|
|
|
*/
|
|
|
async toMakeOrder({ key }) {
|
|
|
key = `${this.redisKey.orderKeyPrefix}${key}`;
|
|
@@ -197,22 +198,16 @@ class OrderService extends CrudService {
|
|
|
if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
|
|
|
data = JSON.parse(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:按店铺分组
|
|
|
+ // 根据缓存,整理商品数据
|
|
|
+ if (!_.isArray(data)) throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
|
|
|
+ const head = _.head(data);
|
|
|
+ if (_.isString(head)) {
|
|
|
+ // 购物车来的
|
|
|
+ } else if (_.isObject(head)) {
|
|
|
+ // 直接购买来的
|
|
|
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);
|
|
|
+ specsData = await this.getPageData(data);
|
|
|
} else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
|
|
|
// 组装页面的数据
|
|
|
const user = this.ctx.user;
|
|
@@ -222,13 +217,8 @@ class OrderService extends CrudService {
|
|
|
// 找到默认地址
|
|
|
const address = await this.addressModel.findOne({ customer, is_default: '1' });
|
|
|
pageData.address = address;
|
|
|
- // #region 团购部分
|
|
|
- const { type, group } = data;
|
|
|
- pageData.type = type; // 新添字段,订单类型
|
|
|
- pageData.group = group; // 新添字段,团购的团id
|
|
|
- // #endregion
|
|
|
// 商品总价,各店铺的价格明细
|
|
|
- specsData = this.ctx.service.util.order.makeOrder_computedShopTotal(specsData, type);
|
|
|
+ specsData = this.ctx.service.util.order.makeOrder_computedShopTotal(specsData);
|
|
|
const shopTotalDetail = this.ctx.service.util.order.makerOrder_computedOrderTotal(specsData);
|
|
|
pageData.goodsData = specsData;
|
|
|
pageData.orderTotal = shopTotalDetail;
|
|
@@ -241,6 +231,69 @@ class OrderService extends CrudService {
|
|
|
return pageData;
|
|
|
}
|
|
|
|
|
|
+ // 直接购买&购物车,这俩字段基本没差, 组织订单页商品数据
|
|
|
+ async getPageData(data) {
|
|
|
+ let arr = [];
|
|
|
+ for (const i of data) {
|
|
|
+ const { goodsSpec, num } = i;
|
|
|
+ const d = await this.goodsSpecModel.aggregate([
|
|
|
+ { $match: { _id: ObjectId(goodsSpec) } },
|
|
|
+ { $addFields: { goods_id: { $toObjectId: '$goods' } } },
|
|
|
+ {
|
|
|
+ $lookup: {
|
|
|
+ from: 'goods',
|
|
|
+ localField: 'goods_id',
|
|
|
+ foreignField: '_id',
|
|
|
+ pipeline: [
|
|
|
+ { $addFields: { shop_id: { $toObjectId: '$shop' } } },
|
|
|
+ {
|
|
|
+ $lookup: {
|
|
|
+ from: 'shop',
|
|
|
+ localField: 'shop_id',
|
|
|
+ foreignField: '_id',
|
|
|
+ pipeline: [{ $project: { name: 1 } }],
|
|
|
+ as: 'shop',
|
|
|
+ },
|
|
|
+ },
|
|
|
+ { $project: { name: 1, file: 1, tag: 1, act_tag: 1, shop: { $first: '$shop' } } },
|
|
|
+ ],
|
|
|
+ as: 'goods',
|
|
|
+ },
|
|
|
+ },
|
|
|
+ { $unwind: '$goods' },
|
|
|
+ {
|
|
|
+ $project: {
|
|
|
+ _id: 0,
|
|
|
+ shop: '$goods.shop._id',
|
|
|
+ shop_name: '$goods.shop.name',
|
|
|
+ goods_id: '$goods._id',
|
|
|
+ goods_name: '$goods.name',
|
|
|
+ goodsSpec_id: '$_id',
|
|
|
+ goodsSpec_name: '$name',
|
|
|
+ freight: { $toString: '$freight' },
|
|
|
+ sell_money: { $toString: '$sell_money' },
|
|
|
+ num: { $toDouble: num },
|
|
|
+ file: '$goods.file',
|
|
|
+ tags: '$goods.tags',
|
|
|
+ act_tags: '$goods.act_tags',
|
|
|
+ },
|
|
|
+ },
|
|
|
+ ]);
|
|
|
+ arr.push(...d);
|
|
|
+ }
|
|
|
+ arr = Object.values(_.groupBy(arr, 'shop'));
|
|
|
+ const result = [];
|
|
|
+ // 按店铺分组
|
|
|
+ for (const i of arr) {
|
|
|
+ const head = _.head(i);
|
|
|
+ const obj = { shop: _.get(head, 'shop'), shop_name: _.get(head, 'shop_name') };
|
|
|
+ const goods = i.map(e => _.omit(e, [ 'shop', 'shop_name' ]));
|
|
|
+ obj.goods = goods;
|
|
|
+ result.push(obj);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 单商品整理数据,剩下的可以简略
|
|
|
* @param {Array} list 规格数组
|
|
@@ -328,7 +381,6 @@ class OrderService extends CrudService {
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
async toMakeTask(order_id) {
|
|
|
const { taskMqConfig } = this.app.config;
|
|
|
const data = { service: 'trade.order', method: 'cancel', params: { order_id } };
|