Files
ai_dianshang/miniprogram/model/order/orderConfirm.js
2025-11-17 14:11:46 +08:00

170 lines
4.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { mockIp, mockReqId } from '../../utils/mock';
export const transformGoodsDataToConfirmData = (goodsDataList) => {
const list = [];
goodsDataList.forEach((goodsData) => {
list.push({
storeId: goodsData.storeId,
spuId: goodsData.spuId,
skuId: goodsData.skuId,
goodsName: goodsData.title,
image: goodsData.primaryImage,
reminderStock: 119,
quantity: goodsData.quantity,
payPrice: goodsData.price,
totalSkuPrice: goodsData.price,
discountSettlePrice: goodsData.price,
realSettlePrice: goodsData.price,
settlePrice: goodsData.price,
oriPrice: goodsData.originPrice,
tagPrice: null,
tagText: null,
skuSpecLst: goodsData.specInfo,
promotionIds: null,
weight: 0.0,
unit: 'KG',
volume: null,
masterGoodsType: 0,
viceGoodsType: 0,
roomId: goodsData.roomId,
egoodsName: null,
});
});
return list;
};
/** 生成结算数据 */
export function genSettleDetail(params) {
const { userAddressReq, couponList, goodsRequestList } = params;
const resp = {
data: {
settleType: 0,
userAddress: null,
totalGoodsCount: 3,
packageCount: 1,
totalAmount: '289997',
totalPayAmount: '',
totalDiscountAmount: '110000',
totalPromotionAmount: '1100',
totalCouponAmount: '0',
totalSalePrice: '289997',
totalGoodsAmount: '289997',
totalDeliveryFee: '0',
invoiceRequest: null,
skuImages: null,
deliveryFeeList: null,
storeGoodsList: [
{
storeId: '1000',
storeName: '云Mall深圳旗舰店',
remark: null,
goodsCount: 1,
deliveryFee: '0',
deliveryWords: null,
storeTotalAmount: '0',
storeTotalPayAmount: '179997',
storeTotalDiscountAmount: '110000',
storeTotalCouponAmount: '0',
skuDetailVos: [],
couponList: [],
},
],
inValidGoodsList: null,
outOfStockGoodsList: null,
limitGoodsList: null,
abnormalDeliveryGoodsList: null,
invoiceSupport: 1,
},
code: 'Success',
msg: null,
requestId: mockReqId(),
clientIp: mockIp(),
rt: 244,
success: true,
};
const list = transformGoodsDataToConfirmData(goodsRequestList);
// 获取购物车传递的商品数据
if (resp.data.storeGoodsList && resp.data.storeGoodsList.length > 0 && resp.data.storeGoodsList[0]) {
resp.data.storeGoodsList[0].skuDetailVos = list;
}
// 处理传入的优惠券数据
const discountPrice = [];
const selectedCoupons = [];
if (couponList && couponList.length > 0) {
couponList.forEach((coupon) => {
if (coupon.status === 'default') {
// 添加到折扣计算
discountPrice.push({
type: coupon.type,
value: coupon.value,
});
// 添加到响应的couponList中
selectedCoupons.push({
couponId: coupon.id || coupon.couponId,
storeId: coupon.storeId || '1000',
});
}
});
}
// 将选中的优惠券设置到响应数据中
if (resp.data.storeGoodsList && resp.data.storeGoodsList.length > 0 && resp.data.storeGoodsList[0]) {
resp.data.storeGoodsList[0].couponList = selectedCoupons;
}
// 模拟计算场景
// 计算总价
const totalPrice = list.reduce((pre, cur) => {
return pre + cur.quantity * Number(cur.settlePrice);
}, 0);
// 计算折扣
const totalDiscountPrice =
discountPrice.length > 0
? discountPrice.reduce((pre, cur) => {
if (cur.type === 1) {
// 满减券:直接减去固定金额
return pre + cur.value;
}
if (cur.type === 2) {
// 折扣券:计算折扣金额
// cur.value 是折扣值如80表示8折
// 8折意味着支付80%的价格,所以折扣金额 = 总价 * (100 - 折扣值) / 100
const discountAmount = (Number(totalPrice) * (100 - cur.value)) / 100;
console.log('折扣券计算:', {
totalPrice: Number(totalPrice),
discountValue: cur.value,
finalPrice: Number(totalPrice) * cur.value / 100,
discountAmount: discountAmount,
formula: `折扣金额 = ${Number(totalPrice)} * (100 - ${cur.value}) / 100 = ${discountAmount}`
});
return pre + discountAmount;
}
return pre + cur;
}, 0)
: 0;
resp.data.totalSalePrice = totalPrice;
resp.data.totalCouponAmount = totalDiscountPrice;
resp.data.totalPayAmount =
totalPrice - totalDiscountPrice - Number(resp.data.totalPromotionAmount);
if (userAddressReq) {
resp.data.settleType = 1;
resp.data.userAddress = userAddressReq;
}
return resp;
}