fix: 修改订单计算逻辑

This commit is contained in:
YeMingfei666 2025-09-19 18:32:44 +08:00
parent f76dff67d4
commit 4934f20446
5 changed files with 343 additions and 239 deletions

View File

@ -231,9 +231,13 @@ export const useCartsStore = defineStore("carts", () => {
return [];
});
const coupons = ref<BackendCoupon[]>([]);
function setCoupons(cps: BackendCoupon[]) {
coupons.value = cps;
}
// 优惠券列表
const backendCoupons = computed<BackendCoupon[]>(() => {
return [];
return coupons.value;
});
// 商品加入购物车顺序
@ -759,7 +763,8 @@ export const useCartsStore = defineStore("carts", () => {
clearMerchantReduction,
seatFeeConfig,
pointDeductionRule,
userPoints
userPoints,
setCoupons
};
});

0
src/utils/goods-utils.js Normal file
View File

View File

@ -1164,6 +1164,7 @@ function getCouponStrategy(couponType: CouponType): CouponCalculationStrategy {
}
}
/**
* ID排除
* @param backendCoupons

View File

@ -110,12 +110,7 @@
</el-table-column>
<el-table-column prop="discountAmount" label="抵扣">
<template v-slot="scope">
<span class="color-red" v-if="scope.row.type == 1">
{{ scope.row.discountAmount }}
</span>
<span class="color-red" v-if="scope.row.type == 2">
{{ returnProDiscount(scope.row, scope.row.index) }}
</span>
<span class="color-red">{{ scope.row.discountAmount }}</span>
</template>
</el-table-column>
<el-table-column prop="useRestrictions" label="操作">
@ -274,34 +269,28 @@ let $goodsPayPriceMap = {};
const refCoupon = ref();
let quansSelArr = ref([]);
function openCoupon() {
refCoupon.value.open(carts.goodsTotal, props.orderInfo);
//
const price = new BigNumber(carts.orderCostSummary.goodsOriginalAmount)
.minus(carts.orderCostSummary.goodsDiscountAmount)
.toFixed(2);
refCoupon.value.open(price, props.orderInfo);
}
//
function returnProDiscount(row) {
//
const arr = quansSelArr.value.filter((v) => v.type == 2 && v.proId == row.proId);
console.log(arr);
const index = arr.findIndex((v) => v.id == row.id);
const item = goodsArr.find((v) => v.productId == row.proId);
if (index != -1) {
const n = quanUtil.returnProductCoupAllPrice(
$goodsPayPriceMap[row.proId],
index,
row.num,
props.user.id && props.user.isVip
);
return n.toFixed(2);
} else {
return 0;
}
}
function refCouponConfirm(e, goodsPayPriceMap, goodsList) {
function refCouponConfirm(e, goodsList) {
e = e.map((v) => {
return {
...v,
couponType: v.type,
title: v.name,
};
});
goodsArr = goodsList;
usePointsNumber.value = 0;
pointsDiscountAmount.value = 0;
score.sel = -1;
console.log("refCouponConfirm", e);
quansSelArr.value = e;
$goodsPayPriceMap = goodsPayPriceMap;
carts.setCoupons(e);
checkOrderPay.discountAmount = 0;
checkOrderPay.discount = 0;
score.sel = -1;

View File

@ -4,27 +4,22 @@
<div class="">
<el-tabs v-model="activeName" @tab-click="tabClick">
<el-tab-pane label="优惠券(单选)" name="youhui">
<el-table
ref="refTable"
empty-text="无可用优惠券"
:data="quans.fullReductionCoupon"
@cell-click="fullReductionCouponClick"
>
<el-table ref="refTable" empty-text="无可用优惠券" :data="quans.coupon">
<el-table-column type="index" label="">
<template v-slot="scope">
<el-checkbox
@change="fullReductionCouponClick(scope.row)"
:model-value="scope.row.id == fullReductionCouponSel.id"
@change="couponClick($event, scope.row)"
:model-value="scope.row.id == couponSel.id"
></el-checkbox>
</template>
</el-table-column>
<el-table-column type="index" label="#"></el-table-column>
<el-table-column prop="name" label="券名称"></el-table-column>
<!-- <el-table-column label="券类型" width="80">
<el-table-column label="券类型" width="80">
<template v-slot="scope">
{{ scope.row.type == 1 ? "优惠券" : "商品券" }}
{{ returnCoupType(scope.row) }}
</template>
</el-table-column> -->
</el-table-column>
<el-table-column prop="discountAmount" label="抵扣">
<template v-slot="scope">
<span class="color-red">{{ scope.row.discountAmount }}</span>
@ -55,7 +50,7 @@
<template v-slot="scope">
<el-checkbox
@change="productCouponClick($event, scope.row)"
v-model="scope.row.checked"
:model-value="goodsCouponSel.id == scope.row.id"
></el-checkbox>
</template>
</el-table-column>
@ -64,7 +59,7 @@
<el-table-column label="商品信息" width="220">
<template v-slot="scope">
<div class="u-flex">
<div class="u-flex" v-if="scope.row.useFoods.length">
<div class="u-flex">
<el-image
:src="scope.row.productImg"
@ -74,30 +69,30 @@
></el-image>
</div>
<div class="u-p-l-10">
<div class="">{{ scope.row.productName }}</div>
<div class="">x{{ scope.row.num || 1 }}</div>
<!-- <div class="">{{ scope.row.productName }}</div> -->
<div class="">x{{ scope.row.discountNum || 1 }}</div>
</div>
</div>
<div class="u-flex" v-else>任意商品 x{{ scope.row.discountNum || 1 }}</div>
</template>
</el-table-column>
<el-table-column prop="discountAmount" label="抵扣">
<template v-slot="scope">
<span class="color-red">{{ scope.row.discountAmount }}</span>
</template>
</el-table-column>
<el-table-column prop="discountAmount" label="限制" width="180">
<template v-slot="scope">
<div class="u-flex">
<span>支付满</span>
<span class="color-red no-wrap">
{{ scope.row.fullAmount }}
</span>
<span>元可用</span>
</div>
</template>
</el-table-column>
<!-- <el-table-column prop="discountAmount" label="抵扣">
<template v-slot="scope">
<span class="color-red">
{{ scope.row.discountAmount }}
</span>
</template>
</el-table-column> -->
<!-- <el-table-column label="券类型">
<template v-slot="scope">
{{ scope.row.type == 1 ? "优惠券" : "商品券" }}
</template>
</el-table-column> -->
<el-table-column prop="useRestrictions" label="描述"></el-table-column>
<!-- <el-table-column prop="useRestrictions" label="是否可用">
<template v-slot="scope">
{{ scope.row.use ? "可以" : "不可用" }}
</template>
</el-table-column> -->
</el-table>
</el-tab-pane>
</el-tabs>
@ -108,34 +103,32 @@
<el-table-column prop="name" label="券名称"></el-table-column>
<el-table-column label="券类型" width="80">
<template v-slot="scope">
{{ scope.row.type == 1 ? "优惠券" : "商品券" }}
{{ returnCoupType(scope.row) }}
</template>
</el-table-column>
<el-table-column label="商品信息" width="120">
<template v-slot="scope">
<div class="u-flex" v-if="scope.row.type == 2">
<div class="u-flex">
<el-image
:src="scope.row.productImg"
fit="cover"
style="width: 40px; height: 40px"
></el-image>
</div>
<div class="u-p-l-10">
<div class="">{{ scope.row.productName }}</div>
<div class="">x{{ scope.row.num || 1 }}</div>
<div v-if="scope.row.useFoods.length">
<div class="u-flex">
<el-image
:src="scope.row.productImg"
fit="cover"
style="width: 40px; height: 40px"
></el-image>
</div>
<div class="u-p-l-10">
<div class="">{{ scope.row.productName }}</div>
<div class="">x{{ scope.row.discountNum || 1 }}</div>
</div>
</div>
<div v-else>任意商品 x{{ scope.row.discountNum || 1 }}</div>
</div>
</template>
</el-table-column>
<el-table-column prop="discountAmount" label="抵扣">
<template v-slot="scope">
<span class="color-red" v-if="scope.row.type == 1">
{{ scope.row.discountAmount }}
</span>
<span class="color-red" v-if="scope.row.type == 2">
{{ returnProDiscount(scope.row, scope.row.index) }}
</span>
<span class="color-red">{{ scope.row.discountAmount }}</span>
</template>
</el-table-column>
<el-table-column prop="useRestrictions" label="描述"></el-table-column>
@ -173,6 +166,8 @@
</template>
<script setup>
import couponApi from "@/api/account/coupon";
import { OrderPriceCalculator } from "@/utils/goods";
import { ElMessageBox } from "element-plus";
import * as quanUtil from "../quan_util.js";
const props = defineProps({
@ -197,17 +192,19 @@ function tabClick() {}
const state = reactive({
discount: 1,
fullReductionCouponSel: {
couponSel: {
id: "",
},
goodsCouponSel: {
id: "",
},
quansSelArr: [],
quans: {
fullReductionCoupon: [],
coupon: [],
productCoupon: [],
},
currentRow: null,
multipleSelection: null,
fullReductionCouponSelId: "",
couponSelId: "",
activeName: "youhui",
form: {},
show: false,
@ -216,8 +213,8 @@ const state = reactive({
const {
discount,
fullReductionCouponSel,
quansSelArr,
couponSel,
goodsCouponSel,
quans,
currentRow,
multipleSelection,
@ -226,214 +223,326 @@ const {
show,
isSetProductCoup,
} = toRefs(state);
const quansSelArr = computed(() => {
return [couponSel.value, goodsCouponSel.value].filter((v) => v.id);
});
const refTable = ref();
const refTable1 = ref();
let orderPrice = ref(0);
let $originFullReductionCoupon = [];
//
let canDikouGoodsArr = [];
//0n
let $goodsPayPriceMap = {};
let goodsGroupMap = {};
let goodsArr = [];
/**
* 返回可以抵扣优惠券的商品列表,过滤掉赠品临时商品,价格从高到低排序
* @param arr 商品列表
*/
function returnCanDikouGoods(arr) {
return arr
.filter((v) => {
return v.is_temporary != 1 && v.is_gift != 1;
})
.sort((a, b) => {
return returnGoodsPrice(b, props.user) - returnGoodsPrice(a, props.user);
});
}
/**
* 返回商品分组
* @param arr 商品列表
*/
function returnGoodsGroupMap(arr) {
let map = {};
arr.forEach((v) => {
const key = v.productId + "_" + v.skuId;
if (!map[key]) {
map[key] = [];
}
map[key].push(v);
});
return map;
}
/**
* 优惠券类型1-满减券2-商品兑换券3-折扣券4-第二件半价券5-消费送券6-买一送一券7-固定价格券8-免配送费券
* @param coupon
*/
function returnCoupType(coupon) {
const couponTypes = {
1: "满减券",
2: "商品券",
3: "折扣券",
4: "第二件半价券",
5: "消费送券",
6: "买一送一券",
7: "固定价格券",
8: "免配送费券",
};
return couponTypes[coupon.type] || "未知类型";
}
function open(money, orderInfo) {
let arr = [];
for (let i in orderInfo.detailMap) {
arr.push(...orderInfo.detailMap[i]);
}
goodsArr = arr;
$goodsPayPriceMap = quanUtil.returnGoodsPayPriceMap(goodsArr || []);
canDikouGoodsArr = quanUtil.returnNewGoodsList(goodsArr);
console.log(canDikouGoodsArr);
canDikouGoodsArr = returnCanDikouGoods(arr);
goodsGroupMap = returnGoodsGroupMap(canDikouGoodsArr);
console.log("canDikouGoodsArr", canDikouGoodsArr);
console.log("goodsGroupMap", goodsGroupMap);
getcoup();
orderPrice.value = money;
show.value = true;
}
/**
* 判断该商品券是否可用
* @param canDikouGoodsArr 可抵扣商品列表
* @param coupon 优惠券
* @param goodsOrderPrice 商品订单金额
*/
function canUseGoodsCoupon(canDikouGoodsArr, coupon, goodsOrderPrice) {
//
const isDikouAll = coupon.useFoods.length === 0;
//
const isDikou = coupon.useFoods.some((v) =>
canDikouGoodsArr.some((goods) => goods.productId == v.id)
);
return (
coupon.type == 2 &&
(isDikouAll || isDikou) &&
coupon.use &&
goodsOrderPrice >= coupon.fullAmount
);
}
/**
* 判断该买一送一券是否可用
* @param canDikouGoodsArr 可抵扣商品列表
* @param coupon 优惠券
* @param goodsOrderPrice 商品订单金额
*/
function canUseBuyOneGiveOne(canDikouGoodsArr, coupon, goodsOrderPrice) {
//
const isDikouAll = coupon.useFoods.length === 0;
//
const isDikou = coupon.useFoods.some((v) =>
canDikouGoodsArr.some((goods) => goods.productId == v.id)
);
let canUse = false;
if (isDikouAll) {
canUse = canDikouGoodsArr.some((v) => v.num >= 2);
}
if (isDikou) {
canUse = canDikouGoodsArr
.filter((v) => {
return coupon.useFoods.find((food) => food.id == v.productId);
})
.some((v) => v.num >= 2);
}
return coupon.type == 6 && canUse && coupon.use && goodsOrderPrice >= coupon.fullAmount;
}
/**
* 判断该券是否可用
* @param canDikouGoodsArr 可抵扣商品列表
* @param coupon 优惠券
* @param goodsOrderPrice 商品订单金额
*/
function juageCouponCanUse(canDikouGoodsArr, coupon, goodsOrderPrice) {
if (coupon.type == 2) {
//
return canUseGoodsCoupon(canDikouGoodsArr, coupon, goodsOrderPrice);
}
if (coupon.type == 1) {
//
return coupon.use && goodsOrderPrice >= coupon.fullAmount;
}
if (coupon.type == 6) {
//
return (
coupon.use &&
goodsOrderPrice >= coupon.fullAmount &&
canUseBuyOneGiveOne(canDikouGoodsArr, coupon, goodsOrderPrice)
);
}
}
async function getcoup() {
const res = await couponApi.findCoupon({ shopUserId: props.user.id });
console.log(res);
quans.value.fullReductionCoupon = res.filter(
(v) => v.type == 1 && orderPrice.value * 1 >= v.fullAmount * 1
);
//
quans.value.coupon = res
.filter((v) => v.type != 2)
.filter((coupon) => {
return juageCouponCanUse(canDikouGoodsArr, coupon, orderPrice.value);
})
.map((v) => {
const discount = returnCouponDiscount(v);
return {
...v,
discount,
discountAmount: discount.discountPrice || v.discountAmount,
};
});
quans.value.productCoupon = res
.filter((v) => {
//
const isDikouAll = v.useFoods.length === 0;
//
const isDikou = v.useFoods.some((v) => v.id == goods.value.id);
return v.type == 2 && (isDikouAll || isDikou);
.filter((v) => v.type == 2)
.filter((coupon) => {
return juageCouponCanUse(canDikouGoodsArr, coupon, orderPrice.value);
})
.map((v) => {
const findGoods = goodsArr.find((goods) => goods.productId == v.proId);
const discount = returnCouponDiscount(v);
return {
...v,
productImg: findGoods ? findGoods.productImg : "",
productName: findGoods ? findGoods.productName : "",
discount,
discountAmount: discount.discountPrice,
};
});
}
function fullReductionCouponClick(row) {
if (row.id == fullReductionCouponSel.value.id) {
fullReductionCouponSel.value = { id: "" };
quansSelArr.value.splice(0, 1);
return;
}
const dikouQuan = quansSelArr.value[0];
fullReductionCouponSel.value = row;
if (dikouQuan && dikouQuan.type == 1) {
quansSelArr.value[0] = row;
} else {
quansSelArr.value.unshift(row);
}
if (!fullReductionCouponSel.value.id) {
return;
}
function couponClick(checked, row) {
couponSel.value = checked ? row : { id: "" };
}
const AllCouponPrice = computed(() => {
return quanUtil.returnCouponAllPrice(quansSelArr.value, canDikouGoodsArr, props.user);
return quansSelArr.value.reduce((pre, cur) => pre + cur.discountAmount, 0);
});
const payPrice = computed(() => {
return (orderPrice.value - AllCouponPrice.value).toFixed(2);
});
function productCouponClick(checked, item) {
console.log(checked);
if (!item.use) {
return;
}
const hasSelNum = quansSelArr.value
.filter((v) => v.type == 2 && v.proId == item.proId)
.reduce((a, b) => {
return a + (b.num || 1);
}, 0);
console.log($goodsPayPriceMap[item.proId]);
const maxSelNum = $goodsPayPriceMap[item.proId].length;
const coupMaxUseNum = Math.min(item.num || 1, maxSelNum - hasSelNum);
const canUseNum = Math.min(maxSelNum, coupMaxUseNum);
console.log("maxSelNum", maxSelNum);
console.log("coupMaxUseNum", coupMaxUseNum);
console.log("canUseNum", canUseNum);
if (checked && canUseNum <= 0) {
ElMessage.error("购物车该商品券可使用最大数量为" + maxSelNum);
setTimeout(() => {
item.checked = !checked;
}, 100);
return;
}
if (fullReductionCouponSel.value.id && !item.checked) {
const goodsQuan = quans.value.productCoupon.filter((v) => v.checked);
const fullReductionCoupon = fullReductionCouponSel.value.id
? [fullReductionCouponSel.value]
: [];
let coupArr = [...goodsQuan, { ...item, num: canUseNum }];
const payPrice =
orderPrice.value - quanUtil.returnCouponAllPrice(coupArr, canDikouGoodsArr, props.user);
console.log(payPrice);
if (payPrice <= 0) {
return ElMessageBox.confirm(
"选择该商品券后支付金额将为0继续选择将取消选择的满减券",
"提示",
{
confirmButtonText: "继续选择",
cancelButtonText: "取消",
type: "warning",
}
)
.then(() => {
fullReductionCouponSel.value = {
id: "",
};
quansSelArr.value.splice(0, 1);
})
.catch(() => {});
}
if (fullReductionCouponSel.value.fullAmount > payPrice) {
ElMessageBox.confirm(
"选择该商品券后将不满足选择抵扣券的最低满减需求,继续选择将取消选择的满减券",
"提示",
{
confirmButtonText: "继续选择",
cancelButtonText: "取消",
type: "warning",
}
)
.then(() => {
fullReductionCouponSel.value = {
id: "",
};
})
.catch(() => {
item.checked = false;
const index = quansSelArr.value.findIndex((v) => v.id == item.id);
quansSelArr.value.splice(index, 1);
});
}
}
item.checked = checked;
if (!item.checked) {
const index = quansSelArr.value.findIndex((v) => v.id == item.id);
quansSelArr.value.splice(index, 1);
} else {
quansSelArr.value.push({ ...item, num: canUseNum });
}
const CheckedArr = quans.value.productCoupon.filter((v) => v.checked);
if (CheckedArr.length <= 0) {
return quans.value.productCoupon.map((v) => {
v.use = true;
});
}
const noCheckedArr = quans.value.productCoupon.filter((v) => !v.checked);
noCheckedArr.map((v) => {
v.use = quanUtil.returnCoupCanUse(canDikouGoodsArr, v, CheckedArr);
});
goodsCouponSel.value = checked ? item : { id: "" };
}
//
function returnProDiscount(row) {
//
const arr = quansSelArr.value.filter((v) => v.type == 2 && v.proId == row.proId);
const index = arr.findIndex((v) => v.id == row.id);
const item = goodsArr.find((v) => v.productId == row.proId);
if (index != -1) {
const n = quanUtil.returnProductCoupAllPrice(
$goodsPayPriceMap[row.proId],
index,
row.num,
props.user.id && props.user.isVip
);
return n.toFixed(2);
} else {
return 0;
/**
* 计算抵扣商品金额
*/
function calcDiscountGoodsArrPrice(discountGoodsArr, discountNum) {
let hasCountNum = 0;
let discountPrice = 0;
let hasDiscountGoodsArr = [];
for (let i = 0; i < discountGoodsArr.length; i++) {
if (hasCountNum >= discountNum) {
break;
}
const goods = discountGoodsArr[i];
const shengyuNum = discountNum - hasCountNum;
const num = Math.min(goods.num, shengyuNum);
console.log("calcDiscountGoodsArrPrice");
console.log(goods);
discountPrice += returnGoodsPrice(goods, props.user) * num;
hasCountNum += num;
hasDiscountGoodsArr.push({ ...goods, num });
}
return { discountPrice, hasDiscountGoodsArr };
}
/**
*
*/
function returnCouponDiscount(coupon) {
if (coupon.type == 2) {
return returnCouponProductDiscount(coupon);
}
if (coupon.type == 6) {
return returnCouponBuyOneGiveOneDiscount(coupon);
}
}
//
function returnCouponBuyOneGiveOneDiscount(coupon) {
const { useFoods, useRule } = coupon;
//
let discountGoods = undefined;
//
const canUseGoods = canDikouGoodsArr.filter((v) => v.num >= 2);
//
if (useFoods.length === 0) {
if (useRule == "price_asc") {
discountGoods = canUseGoods[canUseGoods.length - 1];
} else {
discountGoods = canUseGoods.slice(0, 1);
}
} else {
//
const canUseGoods1 = canUseGoods.filter((v) => useFoods.find((food) => food.id == v.productId));
console.log(canUseGoods1);
if (useRule == "price_asc") {
discountGoods = canUseGoods1[canUseGoods1.length - 1];
} else {
discountGoods = canUseGoods1.slice(0, 1);
}
}
console.log("discountGoods");
console.log(discountGoods);
const discountPrice = returnGoodsPrice(discountGoods, props.user);
const hasDiscountGoodsArr = [discountGoods];
return { discountPrice, hasDiscountGoodsArr };
}
//
function returnGoodsPrice(goods, user) {
if (goods.discount_sale_amount * 1 > 0) {
return goods.discount_sale_amount;
}
if (user.isVip && goods.memberPrice * 1 <= goods.salePrice * 1 && goods.memberPrice * 1 > 0) {
return goods.memberPrice;
}
return goods.salePrice;
}
//
function returnCouponProductDiscount(coupon) {
const { useFoods, discountNum, useRule } = coupon;
//
let discountGoodsArr = [];
//
if (useFoods.length === 0) {
if (useRule == "price_asc") {
discountGoodsArr = canDikouGoodsArr
.slice(canDikouGoodsArr.length - discountNum, canDikouGoodsArr.length)
.reverse();
} else {
discountGoodsArr = canDikouGoodsArr.slice(0, discountNum);
}
} else {
//
const discountSelGoodsArr = canDikouGoodsArr.filter((v) =>
useFoods.find((food) => food.id == v.productId)
);
if (useRule == "price_asc") {
discountGoodsArr = discountSelGoodsArr
.slice(discountSelGoodsArr.length - discountNum, discountSelGoodsArr.length)
.reverse();
} else {
discountGoodsArr = discountSelGoodsArr.slice(0, discountNum);
}
}
const result = calcDiscountGoodsArrPrice(discountGoodsArr, discountNum);
console.log(result);
return result;
}
//
function delQuan(row) {
const index = quansSelArr.value.findIndex((item) => item.id == row.id);
if (row.type == 2 && index != -1) {
quansSelArr.value.splice(index, 1);
const proIndex = quans.value.productCoupon.findIndex((v) => v.id == row.id);
quans.value.productCoupon[proIndex].checked = false;
}
if (row.type == 1 && index != -1) {
fullReductionCouponSel.value = { id: "" };
quansSelArr.value.splice(0, 1);
if (row.type == 2) {
goodsCouponSel.value = { id: "" };
return;
}
couponSel.value = { id: "" };
}
function close() {
show.value = false;
}
const emits = defineEmits(["confirm"]);
function reset() {
quansSelArr.value = [];
fullReductionCouponSel.value = { id: "" };
quans.value.productCoupon = [];
couponSel.value = { id: "" };
goodsCouponSel.value = { id: "" };
}
function confirm() {
emits("confirm", [...quansSelArr.value], $goodsPayPriceMap, goodsArr);
emits("confirm", [...quansSelArr.value], goodsArr);
close();
}
defineExpose({