fix: 修改订单计算逻辑
This commit is contained in:
parent
f76dff67d4
commit
4934f20446
|
|
@ -231,9 +231,13 @@ export const useCartsStore = defineStore("carts", () => {
|
||||||
return [];
|
return [];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const coupons = ref<BackendCoupon[]>([]);
|
||||||
|
function setCoupons(cps: BackendCoupon[]) {
|
||||||
|
coupons.value = cps;
|
||||||
|
}
|
||||||
// 优惠券列表
|
// 优惠券列表
|
||||||
const backendCoupons = computed<BackendCoupon[]>(() => {
|
const backendCoupons = computed<BackendCoupon[]>(() => {
|
||||||
return [];
|
return coupons.value;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 商品加入购物车顺序
|
// 商品加入购物车顺序
|
||||||
|
|
@ -759,7 +763,8 @@ export const useCartsStore = defineStore("carts", () => {
|
||||||
clearMerchantReduction,
|
clearMerchantReduction,
|
||||||
seatFeeConfig,
|
seatFeeConfig,
|
||||||
pointDeductionRule,
|
pointDeductionRule,
|
||||||
userPoints
|
userPoints,
|
||||||
|
setCoupons
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1164,6 +1164,7 @@ function getCouponStrategy(couponType: CouponType): CouponCalculationStrategy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算优惠券抵扣金额(处理互斥逻辑,选择最优优惠券,按商品ID排除,新增细分统计)
|
* 计算优惠券抵扣金额(处理互斥逻辑,选择最优优惠券,按商品ID排除,新增细分统计)
|
||||||
* @param backendCoupons 后端优惠券列表
|
* @param backendCoupons 后端优惠券列表
|
||||||
|
|
|
||||||
|
|
@ -110,12 +110,7 @@
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column prop="discountAmount" label="抵扣">
|
<el-table-column prop="discountAmount" label="抵扣">
|
||||||
<template v-slot="scope">
|
<template v-slot="scope">
|
||||||
<span class="color-red" v-if="scope.row.type == 1">
|
<span class="color-red">¥{{ scope.row.discountAmount }}</span>
|
||||||
¥{{ scope.row.discountAmount }}
|
|
||||||
</span>
|
|
||||||
<span class="color-red" v-if="scope.row.type == 2">
|
|
||||||
¥{{ returnProDiscount(scope.row, scope.row.index) }}
|
|
||||||
</span>
|
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column prop="useRestrictions" label="操作">
|
<el-table-column prop="useRestrictions" label="操作">
|
||||||
|
|
@ -274,34 +269,28 @@ let $goodsPayPriceMap = {};
|
||||||
const refCoupon = ref();
|
const refCoupon = ref();
|
||||||
let quansSelArr = ref([]);
|
let quansSelArr = ref([]);
|
||||||
function openCoupon() {
|
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) {
|
function refCouponConfirm(e, goodsList) {
|
||||||
//相同商品抵扣券数组
|
e = e.map((v) => {
|
||||||
const arr = quansSelArr.value.filter((v) => v.type == 2 && v.proId == row.proId);
|
return {
|
||||||
console.log(arr);
|
...v,
|
||||||
const index = arr.findIndex((v) => v.id == row.id);
|
couponType: v.type,
|
||||||
const item = goodsArr.find((v) => v.productId == row.proId);
|
title: v.name,
|
||||||
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) {
|
|
||||||
goodsArr = goodsList;
|
goodsArr = goodsList;
|
||||||
usePointsNumber.value = 0;
|
usePointsNumber.value = 0;
|
||||||
pointsDiscountAmount.value = 0;
|
pointsDiscountAmount.value = 0;
|
||||||
score.sel = -1;
|
score.sel = -1;
|
||||||
|
console.log("refCouponConfirm", e);
|
||||||
quansSelArr.value = e;
|
quansSelArr.value = e;
|
||||||
$goodsPayPriceMap = goodsPayPriceMap;
|
carts.setCoupons(e);
|
||||||
checkOrderPay.discountAmount = 0;
|
checkOrderPay.discountAmount = 0;
|
||||||
checkOrderPay.discount = 0;
|
checkOrderPay.discount = 0;
|
||||||
score.sel = -1;
|
score.sel = -1;
|
||||||
|
|
|
||||||
|
|
@ -4,27 +4,22 @@
|
||||||
<div class="">
|
<div class="">
|
||||||
<el-tabs v-model="activeName" @tab-click="tabClick">
|
<el-tabs v-model="activeName" @tab-click="tabClick">
|
||||||
<el-tab-pane label="优惠券(单选)" name="youhui">
|
<el-tab-pane label="优惠券(单选)" name="youhui">
|
||||||
<el-table
|
<el-table ref="refTable" empty-text="无可用优惠券" :data="quans.coupon">
|
||||||
ref="refTable"
|
|
||||||
empty-text="无可用优惠券"
|
|
||||||
:data="quans.fullReductionCoupon"
|
|
||||||
@cell-click="fullReductionCouponClick"
|
|
||||||
>
|
|
||||||
<el-table-column type="index" label="">
|
<el-table-column type="index" label="">
|
||||||
<template v-slot="scope">
|
<template v-slot="scope">
|
||||||
<el-checkbox
|
<el-checkbox
|
||||||
@change="fullReductionCouponClick(scope.row)"
|
@change="couponClick($event, scope.row)"
|
||||||
:model-value="scope.row.id == fullReductionCouponSel.id"
|
:model-value="scope.row.id == couponSel.id"
|
||||||
></el-checkbox>
|
></el-checkbox>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column type="index" label="#"></el-table-column>
|
<el-table-column type="index" label="#"></el-table-column>
|
||||||
<el-table-column prop="name" 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">
|
<template v-slot="scope">
|
||||||
{{ scope.row.type == 1 ? "优惠券" : "商品券" }}
|
{{ returnCoupType(scope.row) }}
|
||||||
</template>
|
</template>
|
||||||
</el-table-column> -->
|
</el-table-column>
|
||||||
<el-table-column prop="discountAmount" label="抵扣">
|
<el-table-column prop="discountAmount" label="抵扣">
|
||||||
<template v-slot="scope">
|
<template v-slot="scope">
|
||||||
<span class="color-red">¥{{ scope.row.discountAmount }}</span>
|
<span class="color-red">¥{{ scope.row.discountAmount }}</span>
|
||||||
|
|
@ -55,7 +50,7 @@
|
||||||
<template v-slot="scope">
|
<template v-slot="scope">
|
||||||
<el-checkbox
|
<el-checkbox
|
||||||
@change="productCouponClick($event, scope.row)"
|
@change="productCouponClick($event, scope.row)"
|
||||||
v-model="scope.row.checked"
|
:model-value="goodsCouponSel.id == scope.row.id"
|
||||||
></el-checkbox>
|
></el-checkbox>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
|
|
@ -64,7 +59,7 @@
|
||||||
|
|
||||||
<el-table-column label="商品信息" width="220">
|
<el-table-column label="商品信息" width="220">
|
||||||
<template v-slot="scope">
|
<template v-slot="scope">
|
||||||
<div class="u-flex">
|
<div class="u-flex" v-if="scope.row.useFoods.length">
|
||||||
<div class="u-flex">
|
<div class="u-flex">
|
||||||
<el-image
|
<el-image
|
||||||
:src="scope.row.productImg"
|
:src="scope.row.productImg"
|
||||||
|
|
@ -74,30 +69,30 @@
|
||||||
></el-image>
|
></el-image>
|
||||||
</div>
|
</div>
|
||||||
<div class="u-p-l-10">
|
<div class="u-p-l-10">
|
||||||
<div class="">{{ scope.row.productName }}</div>
|
<!-- <div class="">{{ scope.row.productName }}</div> -->
|
||||||
<div class="">x{{ scope.row.num || 1 }}</div>
|
<div class="">x{{ scope.row.discountNum || 1 }}</div>
|
||||||
</div>
|
</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>
|
</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>
|
|
||||||
</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="描述"></el-table-column>
|
||||||
<!-- <el-table-column prop="useRestrictions" label="是否可用">
|
|
||||||
<template v-slot="scope">
|
|
||||||
{{ scope.row.use ? "可以" : "不可用" }}
|
|
||||||
</template>
|
|
||||||
</el-table-column> -->
|
|
||||||
</el-table>
|
</el-table>
|
||||||
</el-tab-pane>
|
</el-tab-pane>
|
||||||
</el-tabs>
|
</el-tabs>
|
||||||
|
|
@ -108,12 +103,13 @@
|
||||||
<el-table-column prop="name" 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">
|
<template v-slot="scope">
|
||||||
{{ scope.row.type == 1 ? "优惠券" : "商品券" }}
|
{{ returnCoupType(scope.row) }}
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column label="商品信息" width="120">
|
<el-table-column label="商品信息" width="120">
|
||||||
<template v-slot="scope">
|
<template v-slot="scope">
|
||||||
<div class="u-flex" v-if="scope.row.type == 2">
|
<div class="u-flex" v-if="scope.row.type == 2">
|
||||||
|
<div v-if="scope.row.useFoods.length">
|
||||||
<div class="u-flex">
|
<div class="u-flex">
|
||||||
<el-image
|
<el-image
|
||||||
:src="scope.row.productImg"
|
:src="scope.row.productImg"
|
||||||
|
|
@ -123,19 +119,16 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="u-p-l-10">
|
<div class="u-p-l-10">
|
||||||
<div class="">{{ scope.row.productName }}</div>
|
<div class="">{{ scope.row.productName }}</div>
|
||||||
<div class="">x{{ scope.row.num || 1 }}</div>
|
<div class="">x{{ scope.row.discountNum || 1 }}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div v-else>任意商品 x{{ scope.row.discountNum || 1 }}</div>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column prop="discountAmount" label="抵扣">
|
<el-table-column prop="discountAmount" label="抵扣">
|
||||||
<template v-slot="scope">
|
<template v-slot="scope">
|
||||||
<span class="color-red" v-if="scope.row.type == 1">
|
<span class="color-red">¥{{ scope.row.discountAmount }}</span>
|
||||||
¥{{ scope.row.discountAmount }}
|
|
||||||
</span>
|
|
||||||
<span class="color-red" v-if="scope.row.type == 2">
|
|
||||||
¥{{ returnProDiscount(scope.row, scope.row.index) }}
|
|
||||||
</span>
|
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column prop="useRestrictions" label="描述"></el-table-column>
|
<el-table-column prop="useRestrictions" label="描述"></el-table-column>
|
||||||
|
|
@ -173,6 +166,8 @@
|
||||||
</template>
|
</template>
|
||||||
<script setup>
|
<script setup>
|
||||||
import couponApi from "@/api/account/coupon";
|
import couponApi from "@/api/account/coupon";
|
||||||
|
import { OrderPriceCalculator } from "@/utils/goods";
|
||||||
|
|
||||||
import { ElMessageBox } from "element-plus";
|
import { ElMessageBox } from "element-plus";
|
||||||
import * as quanUtil from "../quan_util.js";
|
import * as quanUtil from "../quan_util.js";
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
|
|
@ -197,17 +192,19 @@ function tabClick() {}
|
||||||
|
|
||||||
const state = reactive({
|
const state = reactive({
|
||||||
discount: 1,
|
discount: 1,
|
||||||
fullReductionCouponSel: {
|
couponSel: {
|
||||||
|
id: "",
|
||||||
|
},
|
||||||
|
goodsCouponSel: {
|
||||||
id: "",
|
id: "",
|
||||||
},
|
},
|
||||||
quansSelArr: [],
|
|
||||||
quans: {
|
quans: {
|
||||||
fullReductionCoupon: [],
|
coupon: [],
|
||||||
productCoupon: [],
|
productCoupon: [],
|
||||||
},
|
},
|
||||||
currentRow: null,
|
currentRow: null,
|
||||||
multipleSelection: null,
|
multipleSelection: null,
|
||||||
fullReductionCouponSelId: "",
|
couponSelId: "",
|
||||||
activeName: "youhui",
|
activeName: "youhui",
|
||||||
form: {},
|
form: {},
|
||||||
show: false,
|
show: false,
|
||||||
|
|
@ -216,8 +213,8 @@ const state = reactive({
|
||||||
|
|
||||||
const {
|
const {
|
||||||
discount,
|
discount,
|
||||||
fullReductionCouponSel,
|
couponSel,
|
||||||
quansSelArr,
|
goodsCouponSel,
|
||||||
quans,
|
quans,
|
||||||
currentRow,
|
currentRow,
|
||||||
multipleSelection,
|
multipleSelection,
|
||||||
|
|
@ -226,214 +223,326 @@ const {
|
||||||
show,
|
show,
|
||||||
isSetProductCoup,
|
isSetProductCoup,
|
||||||
} = toRefs(state);
|
} = toRefs(state);
|
||||||
|
const quansSelArr = computed(() => {
|
||||||
|
return [couponSel.value, goodsCouponSel.value].filter((v) => v.id);
|
||||||
|
});
|
||||||
const refTable = ref();
|
const refTable = ref();
|
||||||
const refTable1 = ref();
|
const refTable1 = ref();
|
||||||
|
|
||||||
let orderPrice = ref(0);
|
let orderPrice = ref(0);
|
||||||
let $originFullReductionCoupon = [];
|
|
||||||
//可以抵扣的商品列表
|
//可以抵扣的商品列表
|
||||||
let canDikouGoodsArr = [];
|
let canDikouGoodsArr = [];
|
||||||
//商品数量从0到n每一个对应的价格
|
let goodsGroupMap = {};
|
||||||
let $goodsPayPriceMap = {};
|
|
||||||
let goodsArr = [];
|
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) {
|
function open(money, orderInfo) {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i in orderInfo.detailMap) {
|
for (let i in orderInfo.detailMap) {
|
||||||
arr.push(...orderInfo.detailMap[i]);
|
arr.push(...orderInfo.detailMap[i]);
|
||||||
}
|
}
|
||||||
goodsArr = arr;
|
goodsArr = arr;
|
||||||
$goodsPayPriceMap = quanUtil.returnGoodsPayPriceMap(goodsArr || []);
|
canDikouGoodsArr = returnCanDikouGoods(arr);
|
||||||
canDikouGoodsArr = quanUtil.returnNewGoodsList(goodsArr);
|
goodsGroupMap = returnGoodsGroupMap(canDikouGoodsArr);
|
||||||
console.log(canDikouGoodsArr);
|
console.log("canDikouGoodsArr", canDikouGoodsArr);
|
||||||
|
console.log("goodsGroupMap", goodsGroupMap);
|
||||||
getcoup();
|
getcoup();
|
||||||
orderPrice.value = money;
|
orderPrice.value = money;
|
||||||
show.value = true;
|
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() {
|
async function getcoup() {
|
||||||
const res = await couponApi.findCoupon({ shopUserId: props.user.id });
|
const res = await couponApi.findCoupon({ shopUserId: props.user.id });
|
||||||
console.log(res);
|
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
|
quans.value.productCoupon = res
|
||||||
.filter((v) => {
|
.filter((v) => v.type == 2)
|
||||||
//是否抵扣全部商品
|
.filter((coupon) => {
|
||||||
const isDikouAll = v.useFoods.length === 0;
|
return juageCouponCanUse(canDikouGoodsArr, coupon, orderPrice.value);
|
||||||
//是否包含抵扣商品
|
|
||||||
const isDikou = v.useFoods.some((v) => v.id == goods.value.id);
|
|
||||||
return v.type == 2 && (isDikouAll || isDikou);
|
|
||||||
})
|
})
|
||||||
.map((v) => {
|
.map((v) => {
|
||||||
const findGoods = goodsArr.find((goods) => goods.productId == v.proId);
|
const findGoods = goodsArr.find((goods) => goods.productId == v.proId);
|
||||||
|
const discount = returnCouponDiscount(v);
|
||||||
return {
|
return {
|
||||||
...v,
|
...v,
|
||||||
productImg: findGoods ? findGoods.productImg : "",
|
productImg: findGoods ? findGoods.productImg : "",
|
||||||
productName: findGoods ? findGoods.productName : "",
|
productName: findGoods ? findGoods.productName : "",
|
||||||
|
discount,
|
||||||
|
discountAmount: discount.discountPrice,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function fullReductionCouponClick(row) {
|
function couponClick(checked, row) {
|
||||||
if (row.id == fullReductionCouponSel.value.id) {
|
couponSel.value = checked ? row : { 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const AllCouponPrice = computed(() => {
|
const AllCouponPrice = computed(() => {
|
||||||
return quanUtil.returnCouponAllPrice(quansSelArr.value, canDikouGoodsArr, props.user);
|
return quansSelArr.value.reduce((pre, cur) => pre + cur.discountAmount, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
const payPrice = computed(() => {
|
const payPrice = computed(() => {
|
||||||
return (orderPrice.value - AllCouponPrice.value).toFixed(2);
|
return (orderPrice.value - AllCouponPrice.value).toFixed(2);
|
||||||
});
|
});
|
||||||
function productCouponClick(checked, item) {
|
function productCouponClick(checked, item) {
|
||||||
console.log(checked);
|
goodsCouponSel.value = checked ? item : { id: "" };
|
||||||
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]
|
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 };
|
||||||
|
}
|
||||||
|
|
||||||
let coupArr = [...goodsQuan, { ...item, num: canUseNum }];
|
/**
|
||||||
const payPrice =
|
*
|
||||||
orderPrice.value - quanUtil.returnCouponAllPrice(coupArr, canDikouGoodsArr, props.user);
|
*/
|
||||||
console.log(payPrice);
|
function returnCouponDiscount(coupon) {
|
||||||
if (payPrice <= 0) {
|
if (coupon.type == 2) {
|
||||||
return ElMessageBox.confirm(
|
return returnCouponProductDiscount(coupon);
|
||||||
"选择该商品券后支付金额将为0,继续选择将取消选择的满减券",
|
|
||||||
"提示",
|
|
||||||
{
|
|
||||||
confirmButtonText: "继续选择",
|
|
||||||
cancelButtonText: "取消",
|
|
||||||
type: "warning",
|
|
||||||
}
|
}
|
||||||
)
|
if (coupon.type == 6) {
|
||||||
.then(() => {
|
return returnCouponBuyOneGiveOneDiscount(coupon);
|
||||||
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);
|
function returnCouponBuyOneGiveOneDiscount(coupon) {
|
||||||
quansSelArr.value.splice(index, 1);
|
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 {
|
} else {
|
||||||
quansSelArr.value.push({ ...item, num: canUseNum });
|
discountGoods = canUseGoods.slice(0, 1);
|
||||||
}
|
}
|
||||||
const CheckedArr = quans.value.productCoupon.filter((v) => v.checked);
|
} else {
|
||||||
if (CheckedArr.length <= 0) {
|
//符合抵扣条件的商品
|
||||||
return quans.value.productCoupon.map((v) => {
|
const canUseGoods1 = canUseGoods.filter((v) => useFoods.find((food) => food.id == v.productId));
|
||||||
v.use = true;
|
console.log(canUseGoods1);
|
||||||
});
|
if (useRule == "price_asc") {
|
||||||
|
discountGoods = canUseGoods1[canUseGoods1.length - 1];
|
||||||
|
} else {
|
||||||
|
discountGoods = canUseGoods1.slice(0, 1);
|
||||||
}
|
}
|
||||||
const noCheckedArr = quans.value.productCoupon.filter((v) => !v.checked);
|
|
||||||
noCheckedArr.map((v) => {
|
|
||||||
v.use = quanUtil.returnCoupCanUse(canDikouGoodsArr, v, CheckedArr);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
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 returnProDiscount(row) {
|
function returnCouponProductDiscount(coupon) {
|
||||||
//相同商品抵扣券数组
|
const { useFoods, discountNum, useRule } = coupon;
|
||||||
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);
|
let discountGoodsArr = [];
|
||||||
if (index != -1) {
|
|
||||||
const n = quanUtil.returnProductCoupAllPrice(
|
//抵扣全部商品
|
||||||
$goodsPayPriceMap[row.proId],
|
if (useFoods.length === 0) {
|
||||||
index,
|
if (useRule == "price_asc") {
|
||||||
row.num,
|
discountGoodsArr = canDikouGoodsArr
|
||||||
props.user.id && props.user.isVip
|
.slice(canDikouGoodsArr.length - discountNum, canDikouGoodsArr.length)
|
||||||
);
|
.reverse();
|
||||||
return n.toFixed(2);
|
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
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) {
|
function delQuan(row) {
|
||||||
const index = quansSelArr.value.findIndex((item) => item.id == row.id);
|
if (row.type == 2) {
|
||||||
if (row.type == 2 && index != -1) {
|
goodsCouponSel.value = { id: "" };
|
||||||
quansSelArr.value.splice(index, 1);
|
return;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
couponSel.value = { id: "" };
|
||||||
}
|
}
|
||||||
function close() {
|
function close() {
|
||||||
show.value = false;
|
show.value = false;
|
||||||
}
|
}
|
||||||
const emits = defineEmits(["confirm"]);
|
const emits = defineEmits(["confirm"]);
|
||||||
function reset() {
|
function reset() {
|
||||||
quansSelArr.value = [];
|
couponSel.value = { id: "" };
|
||||||
fullReductionCouponSel.value = { id: "" };
|
goodsCouponSel.value = { id: "" };
|
||||||
quans.value.productCoupon = [];
|
|
||||||
}
|
}
|
||||||
function confirm() {
|
function confirm() {
|
||||||
emits("confirm", [...quansSelArr.value], $goodsPayPriceMap, goodsArr);
|
emits("confirm", [...quansSelArr.value], goodsArr);
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
defineExpose({
|
defineExpose({
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue