Compare commits

...

2 Commits

Author SHA1 Message Date
YeMingfei666 4934f20446 fix: 修改订单计算逻辑 2025-09-19 18:32:44 +08:00
YeMingfei666 f76dff67d4 fix: 修改订单计算逻辑 2025-09-19 18:32:25 +08:00
21 changed files with 2135 additions and 932 deletions

View File

@ -41,6 +41,7 @@
"@wangeditor-next/editor-for-vue": "^5.1.14",
"ali-oss": "^6.22.0",
"axios": "^1.7.9",
"bignumber.js": "^9.3.1",
"bwip-js": "^4.5.1",
"codemirror": "^5.65.18",
"codemirror-editor-vue3": "^2.8.0",

View File

@ -1,6 +1,6 @@
import request from "@/utils/request";
import { Account_BaseUrl } from "@/api/config";
const baseURL = Account_BaseUrl + "/admin/coupon";
import { Market_BaseUrl } from "@/api/config";
const baseURL = Market_BaseUrl + "/admin/coupon";
const API = {
getList(params: getListRequest) {
return request<any>({

View File

@ -17,6 +17,22 @@ const API = {
params
});
},
// 删除用户优惠券
delete(params: any) {
return request({
url: `${baseURL}/deleteRecord`,
method: "delete",
params,
});
},
//优惠券发放
giveCoupon(data: any) {
return request<any>({
url: `${baseURL}/grant`,
method: "post",
data
});
},
}
export default API;

View File

@ -0,0 +1,85 @@
<template>
<div>
<div v-for="(item, index) in modelValue" :key="index" class="flex gap-4 mb-2">
<el-select
v-model="item.coupon.id"
placeholder="请选择优惠券"
@change="changeCoupon($event, index)"
>
<el-option
v-for="coupon in couponList"
:key="coupon.id"
:label="coupon.title"
:value="coupon.id"
/>
</el-select>
<el-input v-model="item.num" placeholder="请输入数量">
<template #append>数量</template>
</el-input>
<div>
<el-link
:underline="false"
type="danger"
class="no-wrap"
@click="modelValue.splice(index, 1)"
>
删除
</el-link>
</div>
</div>
<div class="flex">
<div class="flex gap-1 cursor-pointer" @click="addCoupon()">
<el-icon color="#3F9EFF">
<CirclePlus />
</el-icon>
<el-link :underline="false" type="primary" class="no-wrap">新增券</el-link>
</div>
</div>
</div>
</template>
<script setup>
import couponApi from "@/api/market/coupon";
import { ref, reactive, onMounted } from "vue";
const modelValue = defineModel({
type: Array,
default: () => [],
});
//
const couponList = ref([]);
function addCoupon() {
if (!modelValue.value) {
modelValue.value = [
{
num: 1,
coupon: {
id: null,
},
},
];
return;
}
modelValue.value.push({
num: 1,
coupon: {
id: null,
},
});
console.log(modelValue.value);
}
onMounted(() => {
couponApi.getList({ size: 999 }).then((res) => {
if (res) {
couponList.value = res.records || [];
}
});
});
function changeCoupon(e, index) {
const coupon = couponList.value.find((item) => item.id === e);
console.log(coupon);
modelValue.value[index].coupon = coupon;
}
</script>

File diff suppressed because it is too large Load Diff

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

View File

@ -1,7 +1,15 @@
import { BigNumber } from 'bignumber.js';
// 配置BigNumber精度
BigNumber.set({
DECIMAL_PLACES: 2,
ROUNDING_MODE: BigNumber.ROUND_DOWN // 向下取整,符合业务需求
});
/**
*
*
* 10.129 10.1215.998 15.99
* 使bignumber.js确保精度 10.129 10.1215.998 15.99
* /
*
* - foods字段=ID字符串=ID
@ -22,17 +30,20 @@ export enum GoodsType {
PACKAGE = 'package'// 打包商品(如套餐/预打包商品,按普通商品逻辑处理,可扩展特殊规则)
}
/** 优惠券计算结果类型 */
/** 优惠券计算结果类型(新增细分字段) */
interface CouponResult {
deductionAmount: number; // 抵扣金额
excludedProductIds: string[]; // 不适用商品ID列表注意是商品ID非购物车ID
usedCoupon: Coupon | undefined; // 实际使用的优惠券
productCouponDeduction: number; // 新增:商品优惠券抵扣(兑换券等)
fullCouponDeduction: number; // 新增:满减优惠券抵扣
}
/** 兑换券计算结果类型 */
/** 兑换券计算结果类型(新增细分字段) */
interface ExchangeCalculationResult {
deductionAmount: number;
excludedProductIds: string[]; // 不适用商品ID列表商品ID
productCouponDeduction: number; // 新增:兑换券属于商品券,同步记录
}
/** 优惠券类型枚举 */
@ -193,10 +204,23 @@ export interface SeatFeeConfig {
personCount: number; // 用餐人数默认1
isEnabled: boolean; // 是否启用餐位费默认false
}
/** 商家减免类型枚举 */
export enum MerchantReductionType {
FIXED_AMOUNT = 'fixed_amount', // 固定金额减免(如直接减 10 元)
DISCOUNT_RATE = 'discount_rate' // 比例折扣减免(如打 9 折,即减免 10%
}
/** 商家减免配置(新增,替代原单一金额字段) */
export interface MerchantReductionConfig {
type: MerchantReductionType; // 减免类型(二选一)
fixedAmount?: number; // 固定减免金额(元,仅 FIXED_AMOUNT 生效≥0
discountRate?: number; // 折扣率(%,仅 DISCOUNT_RATE 生效0-100如 90 代表 9 折)
}
/** 订单额外费用配置 */
export interface OrderExtraConfig {
merchantReduction: number; // 商家减免金额默认0
// merchantReduction: number; // 商家减免金额默认0
// 替换原单一金额字段,支持两种减免形式
merchantReduction: MerchantReductionConfig;
additionalFee: number; // 附加费如余额充值、券包默认0
pointDeductionRule: PointDeductionRule; // 积分抵扣规则
seatFeeConfig: SeatFeeConfig; // 餐位费配置
@ -206,19 +230,27 @@ export interface OrderExtraConfig {
memberDiscountRate?: number; // 会员折扣率如0.95=95折无会员价时用
}
/** 订单费用汇总(所有子项清晰展示,方便调用方使用 */
/** 订单费用汇总(修改:补充商家减免类型和明细 */
export interface OrderCostSummary {
goodsOriginalAmount: number; // 商品原价总和
goodsDiscountAmount: number; // 商品折扣金额(原价-实际价)
couponDeductionAmount: number; // 优惠券抵扣金额
pointDeductionAmount: number; // 积分抵扣金额
seatFee: number; // 餐位费
packFee: number; // 打包费
merchantReductionAmount: number; // 商家减免金额
additionalFee: number; // 附加费
finalPayAmount: number; // 最终实付金额
couponUsed?: Coupon; // 实际使用的优惠券(互斥时选最优)
pointUsed: number; // 实际使用的积分
goodsRealAmount: number; // 商品真实原价总和
goodsOriginalAmount: number; // 商品原价总和
goodsDiscountAmount: number; // 商品折扣金额
couponDeductionAmount: number; // 优惠券总抵扣
productCouponDeduction: number; // 商品优惠券抵扣
fullCouponDeduction: number; // 满减优惠券抵扣
pointDeductionAmount: number; // 积分抵扣金额
seatFee: number; // 餐位费
packFee: number; // 打包费
// 新增:商家减免明细
merchantReduction: {
type: MerchantReductionType; // 实际使用的减免类型
originalConfig: MerchantReductionConfig; // 原始配置(便于前端展示)
actualAmount: number; // 实际减免金额计算后的值≥0
};
additionalFee: number; // 附加费
finalPayAmount: number; // 最终实付金额
couponUsed?: Coupon; // 实际使用的优惠券
pointUsed: number; // 实际使用的积分
}
// ============================ 2. 基础工具函数核心修正所有商品ID匹配用product_id ============================
@ -265,9 +297,8 @@ export function convertBackendCouponToToolCoupon(
vipPriceShare: backendCoupon.vipPriceShare === 1,
useType: backendCoupon.useType ? backendCoupon.useType.split(',') : [],
isValid: isCouponInValidPeriod(backendCoupon, currentTime),
applicableProductIds: applicableProductIds
applicableProductIds: applicableProductIds,
};
// 5. 按券类型补充专属字段
switch (couponType) {
case CouponType.FULL_REDUCTION:
@ -336,9 +367,6 @@ function isCouponAvailable(
// 1. 状态校验必须启用status=1
if (backendCoupon.status !== 1) return false;
// 2. 库存校验:剩余数量>0-10086表示不限量
if (backendCoupon.leftNum !== -10086 && (backendCoupon.leftNum || 0) <= 0) return false;
// 3. 有效期校验:必须在有效期内
if (!isCouponInValidPeriod(backendCoupon, currentTime)) return false;
@ -377,6 +405,7 @@ function isCouponInValidPeriod(backendCoupon: BackendCoupon, currentTime: Date):
if (validType === 'custom' && createTime && validDays) {
const create = new Date(createTime);
const end = new Date(create.getTime() + validDays * 24 * 60 * 60 * 1000); // 加N天
return currentTime <= end;
}
@ -501,12 +530,13 @@ export function formatDiscountRate(backendDiscountRate?: number): number {
}
/**
* 10.129 10.1215.998 15.99
*
* 10.129 10.121.01 1.011.0100000001 1.01
* @param num
* @returns
*/
export function truncateToTwoDecimals(num: number): number {
return Math.floor(num * 100) / 100;
export function truncateToTwoDecimals(num: number | string): number {
return new BigNumber(num).decimalPlaces(2, BigNumber.ROUND_DOWN).toNumber();
}
/**
@ -539,7 +569,7 @@ export function calcMemberPrice(
isMember: boolean,
memberDiscountRate?: number
): number {
if (!isMember) return goods.salePrice;
if (!isMember) return truncateToTwoDecimals(goods.salePrice);
// 优先级SKU会员价 > 商品会员价 > 商品原价(无会员价时用会员折扣)
const basePrice = goods.skuData?.memberPrice
@ -547,9 +577,13 @@ export function calcMemberPrice(
?? goods.salePrice;
// 仅当无SKU会员价、无商品会员价时才应用会员折扣率
return memberDiscountRate && !goods.skuData?.memberPrice && !goods.memberPrice
? truncateToTwoDecimals(basePrice * memberDiscountRate)
: basePrice;
if (memberDiscountRate && !goods.skuData?.memberPrice && !goods.memberPrice) {
return truncateToTwoDecimals(
new BigNumber(basePrice).multipliedBy(memberDiscountRate).toNumber()
);
}
return truncateToTwoDecimals(basePrice);
}
/**
@ -631,37 +665,39 @@ export function calcCouponThresholdAmount(
config: Pick<OrderExtraConfig, 'isMember' | 'memberDiscountRate'>,
activities: ActivityConfig[] = []
): number {
return truncateToTwoDecimals(
eligibleGoods.reduce((total, goods) => {
const availableNum = Math.max(0, goods.number - (goods.returnNum || 0));
if (availableNum <= 0) return total;
let total = new BigNumber(0);
// 1. 基础金额默认用商品原价SKU原价优先
const basePrice = goods.skuData?.salePrice ?? goods.salePrice;
let itemAmount = basePrice * availableNum;
for (const goods of eligibleGoods) {
const availableNum = Math.max(0, goods.number - (goods.returnNum || 0));
if (availableNum <= 0) continue;
// 2. 处理「与会员价/会员折扣同享」规则:若开启,用会员价计算
if (coupon.vipPriceShare) {
const memberPrice = calcMemberPrice(goods, config.isMember, config.memberDiscountRate);
itemAmount = memberPrice * availableNum;
// 1. 基础金额默认用商品原价SKU原价优先
const basePrice = new BigNumber(goods.skuData?.salePrice ?? goods.salePrice);
let itemAmount = basePrice.multipliedBy(availableNum);
// 2. 处理「与会员价/会员折扣同享」规则:若开启,用会员价计算
if (coupon.vipPriceShare) {
const memberPrice = new BigNumber(calcMemberPrice(goods, config.isMember, config.memberDiscountRate));
itemAmount = memberPrice.multipliedBy(availableNum);
}
// 3. 处理「与限时折扣同享」规则若开启叠加限时折扣按商品ID匹配活动
if (coupon.discountShare) {
const activity = goods.activityInfo
?? activities.find(act =>
act.type === ActivityType.TIME_LIMIT_DISCOUNT
&& (act.applicableProductIds || []).includes(String(goods.product_id)) // 核心修正用商品ID匹配活动
);
if (activity) {
itemAmount = itemAmount.multipliedBy(activity.discountRate); // 叠加限时折扣
}
}
// 3. 处理「与限时折扣同享」规则若开启叠加限时折扣按商品ID匹配活动
if (coupon.discountShare) {
const activity = goods.activityInfo
?? activities.find(act =>
act.type === ActivityType.TIME_LIMIT_DISCOUNT
&& (act.applicableProductIds || []).includes(String(goods.product_id)) // 核心修正用商品ID匹配活动
);
total = total.plus(itemAmount);
}
if (activity) {
itemAmount = itemAmount * activity.discountRate; // 叠加限时折扣
}
}
return total + itemAmount;
}, 0)
);
return truncateToTwoDecimals(total.toNumber());
}
// ============================ 3. 商品核心价格计算核心修正按商品ID匹配营销活动 ============================
@ -685,25 +721,30 @@ export function calcSingleGoodsRealPrice(
}
// 2. 优先级2会员价含会员折扣率SKU会员价优先
const memberPrice = calcMemberPrice(goods, isMember, memberDiscountRate);
const memberPrice = new BigNumber(calcMemberPrice(goods, isMember, memberDiscountRate));
// 3. 优先级3营销活动折扣如限时折扣需按商品ID匹配活动
const isActivityApplicable = activity
? (activity.applicableProductIds || []).includes(String(goods.product_id)) // 核心修正用商品ID匹配活动
: false;
if (!activity || !isActivityApplicable) {
return memberPrice;
return memberPrice.toNumber();
}
// 处理活动与会员的同享/不同享逻辑
if (activity.vipPriceShare) {
// 同享:会员价基础上叠加工活动折扣
return truncateToTwoDecimals(memberPrice * activity.discountRate);
return truncateToTwoDecimals(
memberPrice.multipliedBy(activity.discountRate).toNumber()
);
} else {
// 不同享取会员价和活动价的最小值活动价用SKU原价计算
const basePriceForActivity = goods.skuData?.salePrice ?? goods.salePrice;
const activityPrice = truncateToTwoDecimals(basePriceForActivity * activity.discountRate);
return Math.min(memberPrice, activityPrice);
const basePriceForActivity = new BigNumber(goods.skuData?.salePrice ?? goods.salePrice);
const activityPrice = basePriceForActivity.multipliedBy(activity.discountRate);
return truncateToTwoDecimals(
memberPrice.isLessThanOrEqualTo(activityPrice) ? memberPrice.toNumber() : activityPrice.toNumber()
);
}
}
@ -713,13 +754,15 @@ export function calcSingleGoodsRealPrice(
* @returns
*/
export function calcGoodsOriginalAmount(goodsList: BaseCartItem[]): number {
return truncateToTwoDecimals(
goodsList.reduce((total, goods) => {
const availableNum = Math.max(0, goods.number - (goods.returnNum || 0));
const basePrice = goods.skuData?.salePrice ?? goods.salePrice; // SKU原价优先
return total + basePrice * availableNum;
}, 0)
);
let total = new BigNumber(0);
for (const goods of goodsList) {
const availableNum = Math.max(0, goods.number - (goods.returnNum || 0));
const basePrice = new BigNumber(goods.skuData?.salePrice ?? goods.salePrice); // SKU原价优先
total = total.plus(basePrice.multipliedBy(availableNum));
}
return truncateToTwoDecimals(total.toNumber());
}
/**
@ -734,21 +777,23 @@ export function calcGoodsRealAmount(
config: Pick<OrderExtraConfig, 'isMember' | 'memberDiscountRate'>,
activities: ActivityConfig[] = []
): number {
return truncateToTwoDecimals(
goodsList.reduce((total, goods) => {
const availableNum = Math.max(0, goods.number - (goods.returnNum || 0));
if (availableNum <= 0) return total;
let total = new BigNumber(0);
// 匹配商品参与的营销活动按商品ID匹配优先商品自身配置
const activity = goods.activityInfo
?? activities.find(act =>
(act.applicableProductIds || []).includes(String(goods.product_id)) // 核心修正用商品ID匹配活动
);
for (const goods of goodsList) {
const availableNum = Math.max(0, goods.number - (goods.returnNum || 0));
if (availableNum <= 0) continue;
const realPrice = calcSingleGoodsRealPrice(goods, { ...config, activity });
return total + realPrice * availableNum;
}, 0)
);
// 匹配商品参与的营销活动按商品ID匹配优先商品自身配置
const activity = goods.activityInfo
?? activities.find(act =>
(act.applicableProductIds || []).includes(String(goods.product_id)) // 核心修正用商品ID匹配活动
);
const realPrice = new BigNumber(calcSingleGoodsRealPrice(goods, { ...config, activity }));
total = total.plus(realPrice.multipliedBy(availableNum));
}
return truncateToTwoDecimals(total.toNumber());
}
/**
@ -761,7 +806,11 @@ export function calcGoodsDiscountAmount(
goodsOriginalAmount: number,
goodsRealAmount: number
): number {
return truncateToTwoDecimals(Math.max(0, goodsOriginalAmount - goodsRealAmount));
const original = new BigNumber(goodsOriginalAmount);
const real = new BigNumber(goodsRealAmount);
const discount = original.minus(real);
return truncateToTwoDecimals(Math.max(0, discount.toNumber()));
}
// ============================ 4. 优惠券抵扣计算策略模式核心修正按商品ID匹配 ============================
@ -778,6 +827,8 @@ interface CouponCalculationStrategy {
): {
deductionAmount: number;
excludedProductIds: string[]; // 排除的商品ID列表商品ID
productCouponDeduction?: number; // 新增:当前券属于商品券时的抵扣金额
fullCouponDeduction?: number; // 新增:当前券属于满减券时的抵扣金额
};
}
@ -787,35 +838,48 @@ class FullReductionStrategy implements CouponCalculationStrategy {
coupon: FullReductionCoupon,
goodsList: BaseCartItem[],
config: any
): { deductionAmount: number; excludedProductIds: string[] } {
): { deductionAmount: number; excludedProductIds: string[]; fullCouponDeduction: number } {
// 1. 基础校验:优惠券不可用/门店不匹配 → 抵扣0
if (!coupon.available || !isStoreMatchByList(coupon.useShops, config.currentStoreId)) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], fullCouponDeduction: 0 };
}
// 2. 第一步:排除临时菜/赠菜/已抵扣商品基础合格商品按商品ID排除
const baseEligibleGoods = filterCouponEligibleGoods(goodsList, config.excludedProductIds || []);
// 3. 第二步按商品ID筛选门槛商品匹配applicableProductIds
const thresholdGoods = filterThresholdGoods(baseEligibleGoods, coupon.applicableProductIds);
if (thresholdGoods.length === 0) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], fullCouponDeduction: 0 };
}
// 4. 按同享规则计算门槛金额按商品ID匹配活动
const thresholdAmount = calcCouponThresholdAmount(
const thresholdAmount = new BigNumber(calcCouponThresholdAmount(
thresholdGoods,
coupon,
{ isMember: config.isMember, memberDiscountRate: config.memberDiscountRate },
config.activities
);
));
// 5. 满门槛则抵扣否则0不超过最大减免
if (thresholdAmount < coupon.fullAmount) {
return { deductionAmount: 0, excludedProductIds: [] };
if (thresholdAmount.isLessThan(coupon.fullAmount)) {
return { deductionAmount: 0, excludedProductIds: [], fullCouponDeduction: 0 };
}
const maxReduction = coupon.maxDiscountAmount ?? coupon.discountAmount;
const deductionAmount = truncateToTwoDecimals(Math.min(coupon.discountAmount, maxReduction));
return { deductionAmount, excludedProductIds: [] };
const deductionAmount = truncateToTwoDecimals(
new BigNumber(coupon.discountAmount).isLessThan(maxReduction)
? coupon.discountAmount
: maxReduction
);
// 满减券计入满减优惠券抵扣
return {
deductionAmount,
excludedProductIds: [],
fullCouponDeduction: deductionAmount
};
}
}
@ -825,10 +889,10 @@ class DiscountStrategy implements CouponCalculationStrategy {
coupon: DiscountCoupon,
goodsList: BaseCartItem[],
config: any
): { deductionAmount: number; excludedProductIds: string[] } {
): { deductionAmount: number; excludedProductIds: string[]; productCouponDeduction: number } {
// 1. 基础校验:优惠券不可用/门店不匹配 → 抵扣0
if (!coupon.available || !isStoreMatchByList(coupon.useShops, config.currentStoreId)) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], productCouponDeduction: 0 };
}
// 2. 第一步:排除临时菜/赠菜/已抵扣商品基础合格商品按商品ID排除
@ -836,21 +900,31 @@ class DiscountStrategy implements CouponCalculationStrategy {
// 3. 第二步按商品ID筛选门槛商品匹配applicableProductIds
const thresholdGoods = filterThresholdGoods(baseEligibleGoods, coupon.applicableProductIds);
if (thresholdGoods.length === 0) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], productCouponDeduction: 0 };
}
// 4. 按同享规则计算折扣基数按商品ID匹配活动
const discountBaseAmount = calcCouponThresholdAmount(
const discountBaseAmount = new BigNumber(calcCouponThresholdAmount(
thresholdGoods,
coupon,
{ isMember: config.isMember, memberDiscountRate: config.memberDiscountRate },
config.activities
);
));
// 5. 计算折扣金额(不超过最大减免)
const discountAmount = truncateToTwoDecimals(discountBaseAmount * (1 - coupon.discountRate));
const deductionAmount = truncateToTwoDecimals(Math.min(discountAmount, coupon.maxDiscountAmount));
return { deductionAmount, excludedProductIds: [] };
const discountAmount = discountBaseAmount.multipliedBy(new BigNumber(1).minus(coupon.discountRate));
const deductionAmount = truncateToTwoDecimals(
discountAmount.isLessThan(coupon.maxDiscountAmount)
? discountAmount.toNumber()
: coupon.maxDiscountAmount
);
// 折扣券计入商品优惠券抵扣(可根据业务调整归类)
return {
deductionAmount,
excludedProductIds: [],
productCouponDeduction: deductionAmount
};
}
}
@ -860,13 +934,13 @@ class SecondHalfPriceStrategy implements CouponCalculationStrategy {
coupon: SecondHalfPriceCoupon,
goodsList: BaseCartItem[],
config: any
): { deductionAmount: number; excludedProductIds: string[] } {
): { deductionAmount: number; excludedProductIds: string[]; productCouponDeduction: number } {
// 1. 基础校验:优惠券不可用/门店不匹配 → 抵扣0
if (!coupon.available || !isStoreMatchByList(coupon.useShops, config.currentStoreId)) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], productCouponDeduction: 0 };
}
let totalDeduction = 0;
let totalDeduction = new BigNumber(0);
const excludedProductIds: string[] = []; // 存储排除的商品ID商品ID
// 2. 第一步:排除临时菜/赠菜/已抵扣商品基础合格商品按商品ID排除
@ -874,7 +948,7 @@ class SecondHalfPriceStrategy implements CouponCalculationStrategy {
// 3. 第二步按商品ID筛选门槛商品匹配applicableProductIds
const thresholdGoods = filterThresholdGoods(baseEligibleGoods, coupon.applicableProductIds);
if (thresholdGoods.length === 0) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], productCouponDeduction: 0 };
}
// 按商品ID分组避免同商品多次处理用商品ID作为分组key
@ -905,20 +979,23 @@ class SecondHalfPriceStrategy implements CouponCalculationStrategy {
const activity = config.activities.find((act: ActivityConfig) =>
(act.applicableProductIds || []).includes(productIdStr) // 按商品ID匹配活动
);
const realPrice = calcSingleGoodsRealPrice(sampleGood, {
const realPrice = new BigNumber(calcSingleGoodsRealPrice(sampleGood, {
isMember: config.isMember,
memberDiscountRate: config.memberDiscountRate,
activity
});
}));
// 累计抵扣金额并标记已优惠商品记录商品ID
totalDeduction += realPrice * 0.5 * discountCount;
totalDeduction = totalDeduction.plus(realPrice.multipliedBy(0.5).multipliedBy(discountCount));
excludedProductIds.push(productIdStr);
}
const deductionAmount = truncateToTwoDecimals(totalDeduction.toNumber());
// 第二件半价券计入商品优惠券抵扣
return {
deductionAmount: truncateToTwoDecimals(totalDeduction),
excludedProductIds
deductionAmount,
excludedProductIds,
productCouponDeduction: deductionAmount
};
}
}
@ -929,13 +1006,13 @@ class BuyOneGetOneStrategy implements CouponCalculationStrategy {
coupon: BuyOneGetOneCoupon,
goodsList: BaseCartItem[],
config: any
): { deductionAmount: number; excludedProductIds: string[] } {
): { deductionAmount: number; excludedProductIds: string[]; productCouponDeduction: number } {
// 1. 基础校验:优惠券不可用/门店不匹配 → 抵扣0
if (!coupon.available || !isStoreMatchByList(coupon.useShops, config.currentStoreId)) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], productCouponDeduction: 0 };
}
let totalDeduction = 0;
let totalDeduction = new BigNumber(0);
const excludedProductIds: string[] = []; // 存储排除的商品ID商品ID
// 2. 第一步:排除临时菜/赠菜/已抵扣商品基础合格商品按商品ID排除
@ -943,7 +1020,7 @@ class BuyOneGetOneStrategy implements CouponCalculationStrategy {
// 3. 第二步按商品ID筛选门槛商品匹配applicableProductIds
const thresholdGoods = filterThresholdGoods(baseEligibleGoods, coupon.applicableProductIds);
if (thresholdGoods.length === 0) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], productCouponDeduction: 0 };
}
// 按商品ID分组用商品ID作为分组key
@ -971,20 +1048,23 @@ class BuyOneGetOneStrategy implements CouponCalculationStrategy {
const activity = config.activities.find((act: ActivityConfig) =>
(act.applicableProductIds || []).includes(productIdStr) // 按商品ID匹配活动
);
const realPrice = calcSingleGoodsRealPrice(sampleGood, {
const realPrice = new BigNumber(calcSingleGoodsRealPrice(sampleGood, {
isMember: config.isMember,
memberDiscountRate: config.memberDiscountRate,
activity
});
}));
// 累计抵扣金额送1件=减免1件价格并标记商品ID
totalDeduction += realPrice * 1 * discountCount;
totalDeduction = totalDeduction.plus(realPrice.multipliedBy(1).multipliedBy(discountCount));
excludedProductIds.push(productIdStr);
}
const deductionAmount = truncateToTwoDecimals(totalDeduction.toNumber());
// 买一送一券计入商品优惠券抵扣
return {
deductionAmount: truncateToTwoDecimals(totalDeduction),
excludedProductIds
deductionAmount,
excludedProductIds,
productCouponDeduction: deductionAmount
};
}
}
@ -995,10 +1075,10 @@ class ExchangeCouponStrategy implements CouponCalculationStrategy {
coupon: ExchangeCoupon,
goodsList: BaseCartItem[],
config: any
): { deductionAmount: number; excludedProductIds: string[] } {
): { deductionAmount: number; excludedProductIds: string[]; productCouponDeduction: number } {
// 1. 基础校验:优惠券不可用/门店不匹配 → 抵扣0
if (!coupon.available || !isStoreMatchByList(coupon.useShops, config.currentStoreId)) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], productCouponDeduction: 0 };
}
// 2. 第一步:排除临时菜/赠菜/已抵扣商品基础合格商品按商品ID排除
@ -1006,13 +1086,13 @@ class ExchangeCouponStrategy implements CouponCalculationStrategy {
// 3. 第二步按商品ID筛选门槛商品匹配applicableProductIds
const thresholdGoods = filterThresholdGoods(baseEligibleGoods, coupon.applicableProductIds);
if (thresholdGoods.length === 0) {
return { deductionAmount: 0, excludedProductIds: [] };
return { deductionAmount: 0, excludedProductIds: [], productCouponDeduction: 0 };
}
// 按规则排序商品(按价格/数量/加入顺序)
const sortedGoods = sortGoodsForCoupon(thresholdGoods, coupon.sortRule, config.cartOrder);
let remainingCount = coupon.deductCount;
let totalDeduction = 0;
let totalDeduction = new BigNumber(0);
const excludedProductIds: string[] = []; // 存储排除的商品ID商品ID
// 计算兑换抵扣金额按商品ID累计避免重复抵扣
@ -1030,22 +1110,25 @@ class ExchangeCouponStrategy implements CouponCalculationStrategy {
const activity = config.activities.find((act: ActivityConfig) =>
(act.applicableProductIds || []).includes(productIdStr) // 按商品ID匹配活动
);
const realPrice = calcSingleGoodsRealPrice(goods, {
const realPrice = new BigNumber(calcSingleGoodsRealPrice(goods, {
isMember: config.isMember,
memberDiscountRate: config.memberDiscountRate,
activity
});
}));
// 累计抵扣金额并标记商品
totalDeduction += realPrice * deductCount;
totalDeduction = totalDeduction.plus(realPrice.multipliedBy(deductCount));
excludedProductIds.push(productIdStr);
usedProductIds.add(productIdStr);
remainingCount -= deductCount;
}
const deductionAmount = truncateToTwoDecimals(totalDeduction.toNumber());
// 商品兑换券计入商品优惠券抵扣
return {
deductionAmount: truncateToTwoDecimals(totalDeduction),
excludedProductIds
deductionAmount,
excludedProductIds,
productCouponDeduction: deductionAmount
};
}
}
@ -1081,12 +1164,13 @@ function getCouponStrategy(couponType: CouponType): CouponCalculationStrategy {
}
}
/**
* ID排除
* ID排除
* @param backendCoupons
* @param goodsList
* @param config
* @returns
* @returns /
*/
export function calcCouponDeduction(
backendCoupons: BackendCoupon[],
@ -1099,6 +1183,8 @@ export function calcCouponDeduction(
}
): {
deductionAmount: number;
productCouponDeduction: number; // 新增:商品优惠券抵扣(兑换券/折扣券/买一送一等)
fullCouponDeduction: number; // 新增:满减优惠券抵扣
usedCoupon?: Coupon;
excludedProductIds: string[]; // 排除的商品ID列表商品ID
} {
@ -1111,20 +1197,26 @@ export function calcCouponDeduction(
config.currentTime
))
.filter(Boolean) as Coupon[];
if (toolCoupons.length === 0) {
return { deductionAmount: 0, excludedProductIds: [] };
return {
deductionAmount: 0,
productCouponDeduction: 0,
fullCouponDeduction: 0,
excludedProductIds: []
};
}
// 2. 优惠券互斥逻辑:兑换券与其他券互斥,优先选最优
const exchangeCoupons = toolCoupons.filter(c => c.type === CouponType.EXCHANGE);
const nonExchangeCoupons = toolCoupons.filter(c => c.type !== CouponType.EXCHANGE);
// 3. 计算非兑换券最优抵扣传递已抵扣商品ID避免重复
// 3. 计算非兑换券最优抵扣传递已抵扣商品ID避免重复,统计细分字段
let nonExchangeResult: CouponResult = {
deductionAmount: 0,
excludedProductIds: [],
usedCoupon: undefined
usedCoupon: undefined,
productCouponDeduction: 0,
fullCouponDeduction: 0
};
if (nonExchangeCoupons.length > 0) {
nonExchangeResult = nonExchangeCoupons.reduce((best, coupon) => {
@ -1134,15 +1226,26 @@ export function calcCouponDeduction(
excludedProductIds: best.excludedProductIds // 传递已排除的商品ID商品ID
});
const currentResult: CouponResult = {
...result,
usedCoupon: coupon
deductionAmount: result.deductionAmount,
excludedProductIds: result.excludedProductIds,
usedCoupon: coupon,
// 按策略返回的字段赋值细分抵扣
productCouponDeduction: result.productCouponDeduction || 0,
fullCouponDeduction: result.fullCouponDeduction || 0
};
return currentResult.deductionAmount > best.deductionAmount ? currentResult : best;
// 按总抵扣金额选择最优
return new BigNumber(currentResult.deductionAmount).isGreaterThan(best.deductionAmount)
? currentResult
: best;
}, nonExchangeResult);
}
// 4. 计算兑换券抵扣排除非兑换券已抵扣的商品ID
let exchangeResult: ExchangeCalculationResult = { deductionAmount: 0, excludedProductIds: [] };
// 4. 计算兑换券抵扣排除非兑换券已抵扣的商品ID统计商品券细分
let exchangeResult: ExchangeCalculationResult = {
deductionAmount: 0,
excludedProductIds: [],
productCouponDeduction: 0
};
if (exchangeCoupons.length > 0) {
exchangeResult = exchangeCoupons.reduce((best, coupon) => {
const strategy = getCouponStrategy(coupon.type);
@ -1150,14 +1253,25 @@ export function calcCouponDeduction(
...config,
excludedProductIds: [...nonExchangeResult.excludedProductIds, ...best.excludedProductIds] // 合并排除的商品ID
});
return result.deductionAmount > best.deductionAmount ? result : best;
return new BigNumber(result.deductionAmount).isGreaterThan(best.deductionAmount)
? {
deductionAmount: result.deductionAmount,
excludedProductIds: result.excludedProductIds,
productCouponDeduction: result.productCouponDeduction || 0 // 兑换券属于商品券
}
: best;
}, exchangeResult);
}
// 5. 汇总结果:兑换券与非兑换券不可同时使用,取抵扣金额大的
const isExchangeBetter = exchangeResult.deductionAmount > nonExchangeResult.deductionAmount;
const exchangeBn = new BigNumber(exchangeResult.deductionAmount);
const nonExchangeBn = new BigNumber(nonExchangeResult.deductionAmount);
const isExchangeBetter = exchangeBn.isGreaterThan(nonExchangeBn);
return {
deductionAmount: truncateToTwoDecimals(isExchangeBetter ? exchangeResult.deductionAmount : nonExchangeResult.deductionAmount),
productCouponDeduction: isExchangeBetter ? exchangeResult.productCouponDeduction : nonExchangeResult.productCouponDeduction,
fullCouponDeduction: isExchangeBetter ? 0 : nonExchangeResult.fullCouponDeduction, // 兑换券与满减券互斥满减券抵扣置0
usedCoupon: isExchangeBetter ? undefined : nonExchangeResult.usedCoupon,
excludedProductIds: isExchangeBetter ? exchangeResult.excludedProductIds : nonExchangeResult.excludedProductIds
};
@ -1174,22 +1288,24 @@ export function calcTotalPackFee(
goodsList: BaseCartItem[],
dinnerType: 'dine-in' | 'take-out'
): number {
return truncateToTwoDecimals(
goodsList.reduce((total, goods) => {
const availableNum = Math.max(0, goods.number - (goods.returnNum || 0));
if (availableNum === 0) return total;
let total = new BigNumber(0);
// 计算单个商品打包数量外卖全打包堂食按配置称重商品≤1
let packNum = dinnerType === 'take-out'
? availableNum
: (goods.packNumber || 0);
if (goods.product_type === GoodsType.WEIGHT) {
packNum = Math.min(packNum, 1);
}
for (const goods of goodsList) {
const availableNum = Math.max(0, goods.number - (goods.returnNum || 0));
if (availableNum === 0) continue;
return total + (goods.packFee || 0) * packNum;
}, 0)
);
// 计算单个商品打包数量外卖全打包堂食按配置称重商品≤1
let packNum = dinnerType === 'take-out'
? availableNum
: (goods.packNumber || 0);
if (goods.product_type === GoodsType.WEIGHT) {
packNum = Math.min(packNum, 1);
}
total = total.plus(new BigNumber(goods.packFee || 0).multipliedBy(packNum));
}
return truncateToTwoDecimals(total.toNumber());
}
/**
@ -1198,9 +1314,11 @@ export function calcTotalPackFee(
* @returns 0
*/
export function calcSeatFee(config: SeatFeeConfig): number {
if (!config.isEnabled) return 0;
if (!config.isEnabled || config.personCount == 0) return 0;
const personCount = Math.max(1, config.personCount); // 至少1人
return truncateToTwoDecimals(config.pricePerPerson * personCount);
return truncateToTwoDecimals(
new BigNumber(config.pricePerPerson).multipliedBy(personCount).toNumber()
);
}
/**
@ -1222,23 +1340,30 @@ export function calcPointDeduction(
return { deductionAmount: 0, usedPoints: 0 };
}
const userPointsBn = new BigNumber(userPoints);
const pointsPerYuanBn = new BigNumber(rule.pointsPerYuan);
const maxLimitBn = new BigNumber(maxDeductionLimit);
// 最大可抵扣金额(积分可抵金额 vs 规则最大 vs 订单上限)
const maxDeductByPoints = truncateToTwoDecimals(userPoints / rule.pointsPerYuan);
const maxDeductAmount = Math.min(
maxDeductByPoints,
rule.maxDeductionAmount ?? Infinity,
maxDeductionLimit
);
const maxDeductByPoints = userPointsBn.dividedBy(pointsPerYuanBn);
const maxDeductAmount = maxDeductByPoints
.isLessThan(rule.maxDeductionAmount ?? Infinity)
? maxDeductByPoints
: new BigNumber(rule.maxDeductionAmount || Infinity)
.isLessThan(maxLimitBn)
? maxDeductByPoints
: maxLimitBn;
// 实际使用积分 = 抵扣金额 * 积分兑换比例
const usedPoints = truncateToTwoDecimals(maxDeductAmount * rule.pointsPerYuan);
const usedPoints = maxDeductAmount.multipliedBy(pointsPerYuanBn);
return {
deductionAmount: maxDeductAmount,
usedPoints: Math.min(usedPoints, userPoints) // 避免积分超扣
deductionAmount: truncateToTwoDecimals(maxDeductAmount.toNumber()),
usedPoints: truncateToTwoDecimals(Math.min(usedPoints.toNumber(), userPoints)) // 避免积分超扣
};
}
// ============================ 6. 订单总费用汇总与实付金额计算(核心入口,逻辑不变 ============================
// ============================ 6. 订单总费用汇总与实付金额计算(核心入口,补充细分字段 ============================
/**
*
* @param goodsList
@ -1248,7 +1373,7 @@ export function calcPointDeduction(
* @param config
* @param cartOrder key=IDvalue=
* @param currentTime
* @returns
* @returns /
*/
export function calculateOrderCostSummary(
goodsList: BaseCartItem[],
@ -1259,7 +1384,7 @@ export function calculateOrderCostSummary(
cartOrder: Record<string, number> = {},
currentTime: Date = new Date()
): OrderCostSummary {
// 1. 基础费用计算
// 1. 基础费用计算(原有逻辑不变)
const goodsOriginalAmount = calcGoodsOriginalAmount(goodsList);
const goodsRealAmount = calcGoodsRealAmount(goodsList, {
isMember: config.isMember,
@ -1267,8 +1392,14 @@ export function calculateOrderCostSummary(
}, activities);
const goodsDiscountAmount = calcGoodsDiscountAmount(goodsOriginalAmount, goodsRealAmount);
// 2. 优惠券抵扣(传递就餐类型用于可用性判断)
const { deductionAmount: couponDeductionAmount, usedCoupon, excludedProductIds } = calcCouponDeduction(
// 2. 优惠券抵扣(原有逻辑不变)
const {
deductionAmount: couponDeductionAmount,
usedCoupon,
excludedProductIds,
productCouponDeduction,
fullCouponDeduction
} = calcCouponDeduction(
backendCoupons,
goodsList,
{
@ -1282,47 +1413,103 @@ export function calculateOrderCostSummary(
}
);
// 3. 其他费用计算
// 3. 其他费用计算(原有逻辑不变)
const packFee = calcTotalPackFee(goodsList, dinnerType);
const seatFee = calcSeatFee(config.seatFeeConfig);
// 积分抵扣上限:商品实际价 - 优惠券抵扣(避免负抵扣)
const additionalFee = Math.max(0, config.additionalFee);
// 4. 积分抵扣(原有逻辑不变,先于商家减免计算)
const maxPointDeductionLimit = Math.max(0, goodsRealAmount - couponDeductionAmount);
const { deductionAmount: pointDeductionAmount, usedPoints } = calcPointDeduction(
const {
deductionAmount: pointDeductionAmount,
usedPoints
} = calcPointDeduction(
config.userPoints,
config.pointDeductionRule,
maxPointDeductionLimit
);
const merchantReductionAmount = Math.max(0, config.merchantReduction);
const additionalFee = Math.max(0, config.additionalFee);
// 4. 计算最终实付金额(确保非负)
const finalPayAmount = truncateToTwoDecimals(
goodsOriginalAmount
- goodsDiscountAmount
- couponDeductionAmount
- pointDeductionAmount
+ seatFee
+ packFee
- merchantReductionAmount
+ additionalFee
);
const finalPayAmountNonNegative = Math.max(0, finalPayAmount);
// ============================ 新增:商家减免计算(支持两种形式) ============================
// 商家减免计算时机:在商品折扣、优惠券、积分抵扣之后(避免过度减免)
const merchantReductionConfig = config.merchantReduction;
let merchantReductionActualAmount = 0;
// 计算商家减免的可抵扣上限:商品实际金额 - 优惠券 - 积分(避免减免后为负)
const maxMerchantReductionLimit = new BigNumber(goodsRealAmount)
.minus(couponDeductionAmount)
.minus(pointDeductionAmount)
.isGreaterThan(0)
? new BigNumber(goodsRealAmount)
.minus(couponDeductionAmount)
.minus(pointDeductionAmount)
: new BigNumber(0);
switch (merchantReductionConfig.type) {
case MerchantReductionType.FIXED_AMOUNT:
// 固定金额减免取配置金额与上限的最小值且不小于0
const fixedAmount = new BigNumber(merchantReductionConfig.fixedAmount || 0);
merchantReductionActualAmount = fixedAmount
.isLessThanOrEqualTo(maxMerchantReductionLimit)
? fixedAmount.toNumber()
: maxMerchantReductionLimit.toNumber();
merchantReductionActualAmount = Math.max(0, merchantReductionActualAmount);
break;
case MerchantReductionType.DISCOUNT_RATE:
// 比例折扣减免先校验折扣率0-100%),再按比例计算减免金额
const validDiscountRate = Math.max(0, Math.min(100, merchantReductionConfig.discountRate || 0));
// 折扣率转小数(如 90% → 0.9),减免金额 = 可抵扣上限 * (1 - 折扣率)
merchantReductionActualAmount = maxMerchantReductionLimit
.multipliedBy(new BigNumber(1).minus(validDiscountRate / 100))
.toNumber();
// 确保减免金额不超过上限且非负
merchantReductionActualAmount = Math.min(
merchantReductionActualAmount,
maxMerchantReductionLimit.toNumber()
);
break;
}
// 5. 最终实付金额计算(整合所有费用)
const finalPayAmount = new BigNumber(goodsOriginalAmount) // 商品原价总和
.minus(goodsDiscountAmount) // 减去商品折扣
.minus(couponDeductionAmount) // 减去优惠券抵扣
.minus(pointDeductionAmount) // 减去积分抵扣
.minus(merchantReductionActualAmount) // 减去商家实际减免金额
.plus(seatFee) // 加上餐位费(不参与减免)
.plus(packFee) // 加上打包费(不参与减免)
.plus(additionalFee); // 加上附加费
const finalPayAmountNonNegative = Math.max(0, finalPayAmount.toNumber());
// 6. 返回完整费用汇总(包含商家减免明细)
return {
goodsRealAmount,
goodsOriginalAmount,
goodsDiscountAmount,
couponDeductionAmount,
productCouponDeduction: truncateToTwoDecimals(productCouponDeduction || 0),
fullCouponDeduction: truncateToTwoDecimals(fullCouponDeduction || 0),
pointDeductionAmount,
seatFee,
packFee,
merchantReductionAmount,
// 商家减免明细(含类型、原始配置、实际金额)
merchantReduction: {
type: merchantReductionConfig.type,
originalConfig: merchantReductionConfig,
actualAmount: truncateToTwoDecimals(merchantReductionActualAmount)
},
additionalFee,
finalPayAmount: finalPayAmountNonNegative,
finalPayAmount: truncateToTwoDecimals(finalPayAmountNonNegative),
couponUsed: usedCoupon,
pointUsed: usedPoints
};
}
export function isWeightGoods(goods: BaseCartItem): boolean {
return goods.product_type === GoodsType.WEIGHT;
}
// ============================ 7. 对外暴露工具库 ============================
export const OrderPriceCalculator = {
// 基础工具
@ -1331,6 +1518,7 @@ export const OrderPriceCalculator = {
isGiftGoods,
formatDiscountRate,
filterThresholdGoods,
isWeightGoods,
// 优惠券转换
convertBackendCouponToToolCoupon,
// 商品价格计算

View File

@ -161,6 +161,7 @@ const testBackendCoupons: BackendCoupon[] = [
{
id: 1,
title: "满减券",
status: 1,
couponType: 1,
fullAmount: 2,
discountAmount: 1,
@ -172,6 +173,9 @@ const testBackendCoupons: BackendCoupon[] = [
validEndTime: '2025-09-30 16:00:00',
useDays: '周一,周二,周三,周四,周五',
useTimeType: 'all',
shopId: 101,
createTime: '2025-09-16 13:30:50',
validDays: 2
},
];
const testOrderConfig = {
@ -192,7 +196,7 @@ const testOrderConfig = {
additionalFee: 0,
};
const testActivities: ActivityConfig[] = [];
const testCurrentTime = new Date("2024-06-01 12:00:00");
const testCurrentTime = new Date();
// 调用函数(此时类型完全匹配)
const result = OrderPriceCalculator.calculateOrderCostSummary(

View File

@ -10,7 +10,7 @@
</div>
</div>
<div>
<el-switch v-model="isOpen" v-if="props.showSwitch" />
<el-switch v-model="isOpen" active-value="1" inactive-value="0" v-if="props.showSwitch" />
</div>
</div>
</template>

View File

@ -8,7 +8,7 @@
<el-form-item label="周期价格" required>
<el-input v-model="form.price" placeholder="周期价格" type="number" />
</el-form-item>
<el-form-item label="赠送成长值" required>
<el-form-item label="赠送成长值">
<el-input v-model="form.reward" placeholder="开通后立刻获得经验" type="number" />
</el-form-item>
<el-form-item label="赠送优惠券">
@ -117,6 +117,9 @@ function submit() {
ElMessage.error("请选择会员周期");
return;
}
if (!form.value.circleUnit) {
ElMessage.error("请选择会员周期单位");
}
const ispass = form.value.couponList.every((item) => item.num && item.coupon.id);
if (!ispass) {
ElMessage.error("请选择优惠券并输入数量");

View File

@ -5,7 +5,7 @@
intro="用户会员管理设置"
icon="super_vip"
showSwitch
v-model:isOpen="isOpenSuperVip"
v-model:isOpen="basicForm.isOpen"
></HeaderCard>
<el-tabs class="mt-4" v-model="activeTab" type="border-card">
<el-tab-pane :label="item.label" v-for="item in configs" :key="item.name" :name="item.name">
@ -68,6 +68,10 @@
style="margin-left: 20px"
v-if="item.label != '绑定手机号' && item.checked"
v-model="item.value"
:step="item.step"
:precision="item.precision || 0"
:step-strictly="item.stepStrictly || false"
:min="item.min || 0"
/>
</div>
</div>
@ -96,8 +100,8 @@
</el-form-item>
<el-form-item label="享受会员价">
<el-radio-group v-model="basicForm.isMemberPrice">
<el-radio :value="1">所有支付方式</el-radio>
<el-radio :value="0">仅余额支付</el-radio>
<el-radio :value="1"></el-radio>
<el-radio :value="0"></el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="规则说明">
@ -113,23 +117,31 @@
<h3>升级规则</h3>
<el-form-item label="获取成长值升级">
<div class="color-666">
<div class="m-b-4">
<span>每消费1元获得</span>
<el-input-number
class="m-x-2"
v-model="basicForm.costReward"
placeholder="请输入内容"
/>
<span>成长值</span>
</div>
<div>
<span>每充值1元获得</span>
<el-input-number
class="m-x-2"
v-model="basicForm.rechargeReward"
placeholder="请输入内容"
/>
<span>成长值</span>
<div class="flex">
<div>
<div class="m-b-4">
<span>每消费1元获得</span>
<el-input-number
class="m-x-2"
v-model="basicForm.costReward"
placeholder="请输入内容"
/>
<span>成长值</span>
</div>
<div>
<span>每充值1元获得</span>
<el-input-number
class="m-x-2"
v-model="basicForm.rechargeReward"
placeholder="请输入内容"
/>
<span>成长值</span>
</div>
</div>
<span class="ml-4" style="color: red; font-size: 12px">
*两个条件必选有一条是大于0的数值
</span>
</div>
</div>
</el-form-item>
@ -152,6 +164,7 @@
style="margin-top: 20px"
@tab-remove="removeLevel"
@tab-add="addLevel"
@tab-change="levelTabChange"
editable
>
<el-tab-pane
@ -162,7 +175,7 @@
>
<!-- 编辑会员等级参数 -->
<el-form :model="level" label-width="150px">
<el-form-item label="会员标题">
<el-form-item label="会员标题" required>
<el-input
v-model="level.name"
:style="inputStyle"
@ -170,18 +183,24 @@
placeholder="请输入会员标题"
/>
</el-form-item>
<el-form-item label="所需成长值">
<el-input
<el-form-item label="所需成长值" required>
<el-input-number
:disabled="index == 0"
v-model="level.experienceValue"
type="number"
placeholder="请输入所需成长值"
:style="inputStyle"
:min="levelExperienceValueMin(index, level)"
/>
</el-form-item>
<el-form-item label="会员折扣">
<el-input
<el-form-item label="会员折扣" required>
<el-input-number
v-model="level.discount"
:style="inputStyle"
:step="1"
step-strictly
:min="1"
:max="100"
type="number"
placeholder="请输入会员折扣"
/>
@ -210,8 +229,11 @@
style="margin-top: 10px"
class="color-666 flex"
>
<span class="no-wrap">每消耗</span>
<el-input
<span class="no-wrap">每消费</span>
<el-input-number
:min="0.01"
:precision="2"
:controls="false"
class="m-x-2"
v-model="level.costRewardPoints"
type="number"
@ -221,16 +243,17 @@
</div>
</div>
</el-form-item>
<el-form-item label="等级说明">
<el-form-item label="等级说明" required>
<el-input
v-model="level.remark"
style="width: 400px"
:maxlength="250"
:autosize="{ minRows: 4, maxRows: 5 }"
type="textarea"
placeholder="请输入等级说明,最多 250 字"
/>
</el-form-item>
<el-form-item label="周期时间">
<!-- <el-form-item label="周期时间">
<div class="flex w-full gap-2">
<el-input
style="width: 140px"
@ -244,7 +267,7 @@
<el-option label="年" value="年" />
</el-select>
</div>
</el-form-item>
</el-form-item> -->
<el-form-item label="自动发放">
<div>
<el-switch
@ -257,7 +280,7 @@
<el-radio :value="false">已禁用</el-radio>
</el-radio-group> -->
<div v-if="level.isCycleReward" style="margin-top: 10px">
<!-- <div class="flex">
<div class="flex">
<span class="color-666 no-wrap mr-4">周期时间</span>
<el-input
v-model="level.cycleTime"
@ -269,10 +292,13 @@
<el-option label="月" value="月" />
<el-option label="年" value="年" />
</el-select>
</div> -->
</div>
<div class="flex mt-4">
<span class="color-666 no-wrap mr-4">赠送积分</span>
<el-input
<el-input-number
:step="1"
step-strictly
:min="1"
v-model="level.cycleRewardPoints"
type="number"
placeholder="赠送积分"
@ -332,9 +358,6 @@ const inputStyle = {
const router = useRouter();
//
const isOpenSuperVip = ref(false);
const refDialogPlans = ref();
const configs = [
{ name: "basic", label: "会员基础设置" },
@ -344,9 +367,31 @@ const configs = [
const activeTab = ref("basic");
const conditionLists = ref([
{ label: "绑定手机号", checked: false, code: "BIND_PHONE" },
{ label: "订单达成指定次数", checked: false, value: "", code: "ORDER" },
{ label: "消费达到指定金额", checked: false, value: "", code: "COST_AMOUNT" },
{ label: "充值达到指定金额", checked: false, value: "", code: "RECHARGE_AMOUNT" },
{
label: "订单达成指定次数",
checked: false,
value: "",
code: "ORDER",
step: 1,
stepStrictly: true,
min: 1,
},
{
label: "消费达到指定金额",
checked: false,
value: "",
code: "COST_AMOUNT",
precision: 2,
min: 0.01,
},
{
label: "充值达到指定金额",
checked: false,
value: "",
code: "RECHARGE_AMOUNT",
precision: 2,
min: 0.01,
},
]);
const basicForm = reactive({
isSubmitInfo: 1,
@ -358,6 +403,7 @@ const basicForm = reactive({
rechargeReward: 0,
memberPriceShopIdList: [],
isMemberPrice: 1,
isOpen: 0,
});
function deletePlan(row) {
const index = basicForm.configList.indexOf(row);
@ -384,6 +430,27 @@ function basicSubmit() {
// if (data.openType == "CONDITION") {
// data.configList = null;
// }
console.log(data);
if (basicForm.openType == "PAY" && (!basicForm.configList || basicForm.configList.length <= 0)) {
return ElMessage.error("请添加会员方案");
}
data.conditionList = conditionLists.value
.filter((v) => v.checked)
.map((v) => {
return {
code: v.code,
value: v.value,
};
});
if (
basicForm.openType == "CONDITION" &&
(!data.conditionList || data.conditionList.length <= 0)
) {
return ElMessage.error("请选择成为会员条件");
}
if (basicForm.costReward <= 0 && basicForm.rechargeReward <= 0) {
return ElMessage.error("获取成长值升级规则两个条件必选有一条是大于0的数值");
}
data.conditionList = conditionLists.value
.filter((v) => v.checked)
.map((v) => {
@ -404,11 +471,6 @@ const levels = ref([]);
//
const selectedLevel = ref(null);
//
const couponList = ref([
{ id: 1, name: "满100减10" },
{ id: 2, name: "满200减30" },
]);
let activeLevelId = ref(null);
//
function addLevel() {
@ -425,15 +487,16 @@ function addLevel() {
const newLevel = {
name,
experienceValue: 0,
discount: 1,
discount: 100,
logo: "",
costRewardPoints: 1,
isCostRewardPoints: 1,
isCostRewardPoints: 0,
isCycleReward: 0,
cycleTime: 1,
cycleUnit: "月",
cycleRewardPoints: 1,
cycleRewardCouponList: [],
remark: "",
};
console.log(newLevel);
levels.value.push(newLevel);
@ -476,10 +539,36 @@ async function removeLevel(index) {
}
//
async function saveLevel(level) {
const isPass = level.cycleRewardCouponList.every((item) => item.num && item.coupon.id);
if (!isPass) {
ElMessage.error("请选择优惠券并输入数量");
return;
if (level.isCycleReward) {
if (level.cycleRewardCouponList && level.cycleRewardCouponList.length) {
const isPass = (level.cycleRewardCouponList || []).every(
(item) => item.num && item.coupon.id
);
if (!isPass) {
ElMessage.error("请选择优惠券并输入数量");
return;
}
}
if (
(!level.cycleRewardCouponList || level.cycleRewardCouponList.length <= 0) &&
level.cycleRewardPoints <= 0
) {
ElMessage.error("赠送积分和送优惠券必须填充一项");
return;
}
}
if (level.name.trim() === "") {
return ElMessage.error("请输入会员标题");
}
if (level.experienceValue === "") {
return ElMessage.error("请输入所需成长值");
}
if (level.discount === "") {
return ElMessage.error("请输入会员折扣");
}
if (level.remark.trim() === "") {
return ElMessage.error("请输入等级说明");
}
const res = level.id ? await memberApi.levelEdit(level) : await memberApi.levelAdd(level);
if (res) {
@ -509,6 +598,8 @@ async function init() {
});
memberApi.getConfig().then((res) => {
Object.assign(basicForm, res);
res.conditionList = res.conditionList || [];
res.configList = res.configList || [];
conditionLists.value = conditionLists.value.map((v) => {
const findItem = res.conditionList.find((cond) => cond.code == v.code);
if (findItem) {
@ -538,10 +629,19 @@ function totalCount(arr) {
return total + item.num * 1;
}, 0);
}
//
function levelExperienceValueMin(index, level) {
if (index == 0) {
return 0;
}
return levels.value[index - 1].experienceValue + 1;
}
//
function close() {
router.back();
}
//
function levelTabChange(index) {}
</script>
<style lang="scss" scoped>

View File

@ -206,9 +206,7 @@ const proGroupInfo = computed(() => {
return [];
}
});
const discountNewPrice = computed(() => {
return 0;
});
const vipAllPrice = computed(() => {
const n = (props.item.memberPrice || props.item.salePrice) * props.item.number;
return n;

View File

@ -255,9 +255,6 @@ function itemClick(item) {
function changeNumber(step, item) {
carts.changeNumber(step * 1, item);
}
// const totalMoney = computed(() => {
// return (carts.payMoney * 1 + (carts.oldOrder.originAmount || 0) * 1).toFixed(2);
// });
defineExpose({
carts,

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="操作">
@ -161,19 +156,25 @@
<div class="order-info">
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">订单号</span>
<span class="u-m-l-10 value">{{ orderInfo.orderNo }}</span>
</div>
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">餐位费</span>
<span class="u-m-l-10 value">{{ seatAmount }}</span>
<span class="u-m-l-10 value">{{ carts.orderCostSummary.seatFee }}</span>
</div>
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">打包费</span>
<span class="u-m-l-10 value">{{ carts.packFee }}</span>
<span class="u-m-l-10 value">{{ carts.orderCostSummary.packFee }}</span>
</div>
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">商品订单金额</span>
<span class="u-m-l-10 value">{{ carts.goodsTotal }}</span>
<span class="u-m-l-10 value">
{{
carts.orderCostSummary.goodsOriginalAmount -
carts.orderCostSummary.goodsDiscountAmount
}}
</span>
</div>
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">商品优惠券</span>
@ -186,11 +187,13 @@
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">整单改价</span>
<span class="u-m-l-10 value color-red">-{{ discountAmount }}</span>
<span class="u-m-l-10 value color-red">
-{{ carts.orderCostSummary.merchantReduction.actualAmount }}
</span>
</div>
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">积分抵扣</span>
<span class="u-m-l-10 value">-{{ pointsDiscountAmount }}</span>
<span class="u-m-l-10 value">-{{ carts.orderCostSummary.pointDeductionAmount }}</span>
</div>
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">抹零</span>
@ -198,11 +201,11 @@
</div>
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">总价(商品订单金额-商品优惠券-满减券-整单改价-积分抵扣)</span>
<span class="u-m-l-10 value color-red">{{ totalMoney }}</span>
<span class="u-m-l-10 value color-red">{{ totalPrice }}</span>
</div>
<div class="u-flex u-m-b-10 u-row-between">
<span class="title">应付金额(总价+客座费+打包费)</span>
<span class="u-m-l-10 value price">{{ currentpayMoney }}</span>
<span class="u-m-l-10 value price">{{ carts.orderCostSummary.finalPayAmount }}</span>
</div>
</div>
</div>
@ -228,13 +231,12 @@
<script setup>
import * as quanUtil from "../quan_util.js";
import { OrderPriceCalculator } from "@/utils/goods";
import { useCartsStore } from "@/store/modules/carts";
import { useUserStore } from "@/store/modules/user";
import chooseGuaZahng from "./popup-choose-guazhang.vue";
const shopUser = useUserStore();
const carts = useCartsStore();
import popupCoupon from "./popup-coupon.vue";
import PointsApi from "@/api/account/points";
@ -244,7 +246,13 @@ import scanPay from "./scan-pay.vue";
import discount from "./discount.vue";
import { ElLoading } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import { BigNumber } from "bignumber.js";
// BigNumber
BigNumber.set({
DECIMAL_PLACES: 2,
ROUNDING_MODE: BigNumber.ROUND_DOWN, //
});
//
const refGuaZhang = ref();
function refGuaZhangConfirm(guazhangRen) {
@ -261,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;
@ -312,17 +314,40 @@ function discountConfirm(e) {
console.log(e);
Object.assign(checkOrderPay, e);
if (e.discount) {
checkOrderPay.discountAmount = carts.goodsTotal - (carts.goodsTotal * (100 - e.discount)) / 100;
carts.setMerchantDiscountReduction(e.discount);
} else {
carts.setMerchantFixedReduction(e.discountAmount);
checkOrderPay.discount = 0;
}
score.sel = -1;
usePointsNumber.value = 0;
}
//
function returnMerchantReductionDiscount() {
const {
goodsOriginalAmount, //
goodsDiscountAmount, //
couponDeductionAmount, //
pointDeductionAmount, //
merchantReductionActualAmount, //
seatFee, //
packFee, //
additionalFee,
} = carts.orderCostSummary;
const total =
goodsOriginalAmount - //
goodsDiscountAmount - //
couponDeductionAmount - //
pointDeductionAmount; //
return total <= 0 ? 0 : total;
}
function discountShow(e) {
refDiscount.value.open({
amount: carts.goodsTotal - productCouponDiscountAmount.value,
// amount: carts.goodsTotal - productCouponDiscountAmount.value,
amount: returnMerchantReductionDiscount(),
});
}
@ -392,17 +417,25 @@ watch(
}
);
//
function returnPointsDiscountAmount() {
const total = currentpayMoney.value * 1 + carts.orderCostSummary.pointDeductionAmount * 1;
return total <= 0 ? 0 : total;
}
async function pointsInit() {
if (!props.user.id || score.sel == -1) {
return;
}
const orderAmount = currentpayMoney.value * 1 + pointsDiscountAmount.value * 1;
const res = await PointsApi.calcOrderUsablePoints({
shopUserId: props.user.id,
orderAmount: orderAmount <= 0 ? 0 : orderAmount,
orderAmount: returnPointsDiscountAmount(),
});
pointsRes.value = res;
carts.pointDeductionRule.pointsPerYuan = res.equivalentPoints;
usePointsNumber.value = res.usable ? res.maxUsablePoints : 0;
carts.userPoints = usePointsNumber.value;
if (res.usable) {
pointsToMoney();
} else {
@ -412,12 +445,20 @@ async function pointsInit() {
}
//
async function pointsToMoney() {
const res = await PointsApi.calcPointsToMoney({
shopUserId: props.user.id,
orderAmount: currentpayMoney.value * 1 + pointsDiscountAmount.value * 1,
points: usePointsNumber.value,
});
pointsDiscountAmount.value = res;
try {
const res = await PointsApi.calcPointsToMoney({
shopUserId: props.user.id,
orderAmount: returnPointsDiscountAmount(),
points: usePointsNumber.value,
});
if (!res) {
score.sel = -1;
return;
}
pointsDiscountAmount.value = res;
} catch (e) {
score.sel = -1;
}
}
const emits = defineEmits(["chooseUser", "paysuccess"]);
function chooseUser() {
@ -483,6 +524,8 @@ function returnPayParams() {
originAmount: carts.payMoney * 1 + seatAmount.value * 1,
discountAmount: discountAmount.value,
productCouponDiscountAmount: productCouponDiscountAmount.value * 1,
otherCouponDiscountAmount:
carts.orderCostSummary.couponDeductionAmount - productCouponDiscountAmount.value * 1,
orderAmount: currentpayMoney.value * 1,
roundAmount: props.orderInfo.roundAmount,
pointsDiscountAmount: pointsDiscountAmount.value * 1,
@ -616,19 +659,18 @@ const discountAmount = computed(() => {
});
//
const fullCouponDiscountAmount = computed(() => {
return quansSelArr.value
.reduce((pre, cur) => {
return pre + cur.discountAmount;
}, 0)
.toFixed(2);
return (
carts.orderCostSummary.fullCouponDeduction || props.orderInfo.fullCouponDiscountAmount || 0
);
});
//
const productCouponDiscountAmount = computed(() => {
let index = -1;
return quansSelArr.value.reduce((pre, cur) => {
index++;
return pre + returnProDiscount(cur, index) * 1;
}, 0);
// Store props
return (
carts.orderCostSummary.productCouponDeduction ||
props.orderInfo.productCouponDiscountAmount ||
0
);
});
//
const totalMoney = computed(() => {
@ -640,9 +682,30 @@ const totalMoney = computed(() => {
pointsDiscountAmount.value
).toFixed(2);
});
const totalPrice = computed(() => {
// 使bignumber.js
const originalAmount = new BigNumber(carts.orderCostSummary.goodsOriginalAmount);
const discountAmount = new BigNumber(carts.orderCostSummary.goodsDiscountAmount);
const couponDeduction = new BigNumber(carts.orderCostSummary.couponDeductionAmount);
const merchantReduction = new BigNumber(carts.orderCostSummary.merchantReduction.actualAmount);
const pointDeduction = new BigNumber(carts.orderCostSummary.pointDeductionAmount);
// - - - -
const total = originalAmount
.minus(discountAmount)
.minus(couponDeduction)
.minus(merchantReduction)
.minus(pointDeduction)
.decimalPlaces(2, BigNumber.ROUND_DOWN); //
return total.toNumber();
});
//
const currentpayMoney = computed(() => {
return (totalMoney.value * 1 + carts.packFee * 1 + seatAmount.value * 1).toFixed(2);
return carts.orderCostSummary.finalPayAmount;
// return (totalMoney.value * 1 + carts.packFee * 1 + seatAmount.value * 1).toFixed(2);
});
watch(
() => currentpayMoney.value,

View File

@ -0,0 +1,456 @@
<template>
<el-dialog width="700px" :title="title" v-model="show" top="20px" @close="reset">
<div class="u-p-15">
<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-column type="index" label="">
<template v-slot="scope">
<el-checkbox
@change="fullReductionCouponClick(scope.row)"
:model-value="scope.row.id == fullReductionCouponSel.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">
<template v-slot="scope">
{{ scope.row.type == 1 ? "优惠券" : "商品券" }}
</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="useRestrictions" label="描述"></el-table-column>
</el-table>
</el-tab-pane>
<el-tab-pane label="商品券(多选)" name="goods">
<el-table
ref="refTable1"
empty-text="无可用商品券"
:data="quans.productCoupon"
style="width: 100%"
>
<el-table-column width="80">
<template v-slot="scope">
<el-checkbox
@change="productCouponClick($event, scope.row)"
v-model="scope.row.checked"
></el-checkbox>
</template>
</el-table-column>
<el-table-column type="index" width="50" label="#"></el-table-column>
<el-table-column prop="name" label="券名称"></el-table-column>
<el-table-column label="商品信息" width="220">
<template v-slot="scope">
<div class="u-flex">
<div class="u-flex">
<el-image
:src="scope.row.productImg"
fit="cover"
style="width: 40px; height: 40px"
:preview-src-list="[scope.row.productImg]"
></el-image>
</div>
<div class="u-p-l-10">
<div class="">{{ scope.row.productName }}</div>
<div class="">x{{ scope.row.num || 1 }}</div>
</div>
</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>
<div v-if="quansSelArr.length > 0">
<div class="font-bold u-m-b-10">已选优惠券</div>
<el-table empty-text="未选择优惠券" :data="quansSelArr">
<el-table-column type="index" width="50" label="#"></el-table-column>
<el-table-column prop="name" label="券名称"></el-table-column>
<el-table-column label="券类型" width="80">
<template v-slot="scope">
{{ scope.row.type == 1 ? "优惠券" : "商品券" }}
</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>
</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>
</template>
</el-table-column>
<el-table-column prop="useRestrictions" label="描述"></el-table-column>
<el-table-column prop="useRestrictions" label="">
<template v-slot="scope">
<el-button type="danger" size="small" @click="delQuan(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
<div class="u-flex u-row-between u-m-t-20">
<div class="u-flex">
<span>抵扣金额:</span>
<span class="color-red">{{ AllCouponPrice }}</span>
</div>
<div class="u-flex u-relative">
<span>支付金额:</span>
<span class="color-red">{{ payPrice }}</span>
<div
class="u-absolute u-flex line-th color-999"
style="right: 0; bottom: 100%"
v-if="orderPrice * 1 != payPrice * 1"
>
<span class="">{{ orderPrice }}</span>
</div>
</div>
</div>
<div class="u-flex u-row-center u-m-t-50">
<el-button size="large" @click="close">取消</el-button>
<el-button size="large" type="primary" @click="confirm">确定</el-button>
</div>
</div>
</div>
</el-dialog>
</template>
<script setup>
import couponApi from "@/api/account/coupon";
import { ElMessageBox } from "element-plus";
import * as quanUtil from "../quan_util.js";
const props = defineProps({
title: {
type: String,
default: "选择优惠券",
},
goodsArr: {
type: Array,
default: [],
},
user: {
type: Object,
default: () => {
return {
isVip: false,
};
},
},
});
function tabClick() {}
const state = reactive({
discount: 1,
fullReductionCouponSel: {
id: "",
},
quansSelArr: [],
quans: {
fullReductionCoupon: [],
productCoupon: [],
},
currentRow: null,
multipleSelection: null,
fullReductionCouponSelId: "",
activeName: "youhui",
form: {},
show: false,
isSetProductCoup: false,
});
const {
discount,
fullReductionCouponSel,
quansSelArr,
quans,
currentRow,
multipleSelection,
activeName,
form,
show,
isSetProductCoup,
} = toRefs(state);
const refTable = ref();
const refTable1 = ref();
let orderPrice = ref(0);
let $originFullReductionCoupon = [];
//
let canDikouGoodsArr = [];
//0n
let $goodsPayPriceMap = {};
let goodsArr = [];
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);
getcoup();
orderPrice.value = money;
show.value = true;
}
async function getcoup() {
const res = await couponApi.findCoupon({ shopUserId: props.user.id });
quans.value.fullReductionCoupon = res.filter(
(v) => v.type == 1 && orderPrice.value * 1 >= v.fullAmount * 1
);
quans.value.productCoupon = res
.filter((v) => v.type == 2 && canDikouGoodsArr.find((goods) => v.proId == goods.productId))
.map((v) => {
const findGoods = goodsArr.find((goods) => goods.productId == v.proId);
return {
...v,
productImg: findGoods ? findGoods.productImg : "",
productName: findGoods ? findGoods.productName : "",
};
});
}
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;
}
}
const AllCouponPrice = computed(() => {
return quanUtil.returnCouponAllPrice(quansSelArr.value, canDikouGoodsArr, props.user);
});
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);
});
}
//
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 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);
}
}
function close() {
show.value = false;
}
const emits = defineEmits(["confirm"]);
function reset() {
quansSelArr.value = [];
fullReductionCouponSel.value = { id: "" };
quans.value.productCoupon = [];
}
function confirm() {
emits("confirm", [...quansSelArr.value], $goodsPayPriceMap, goodsArr);
close();
}
defineExpose({
close,
open,
});
</script>
<style lang="scss" scoped>
.line-th {
text-decoration: line-through;
}
.codeImg {
width: 160px;
border: 1px solid rgb(220, 223, 230);
height: 160px;
}
:deep(.el-input .el-input__inner::-webkit-inner-spin-button) {
-webkit-appearance: none;
margin: 0;
}
:deep(.el-input .el-input__inner::-webkit-outer-spin-button) {
-webkit-appearance: none;
margin: 0;
}
</style>

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>
@ -44,7 +39,7 @@
<el-table-column prop="useRestrictions" label="描述"></el-table-column>
</el-table>
</el-tab-pane>
<el-tab-pane label="商品券(选)" name="goods">
<el-tab-pane label="商品券(选)" name="goods">
<el-table
ref="refTable1"
empty-text="无可用商品券"
@ -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,207 +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 });
quans.value.fullReductionCoupon = res.filter(
(v) => v.type == 1 && orderPrice.value * 1 >= v.fullAmount * 1
);
console.log(res);
//
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) => v.type == 2 && canDikouGoodsArr.find((goods) => v.proId == goods.productId))
.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({

View File

@ -882,6 +882,12 @@ watch(
}
}
);
watch(
() => perpole.value,
(newval) => {
carts.seatFeeConfig.personCount = newval;
}
);
</script>
<style lang="scss" scoped>

View File

@ -0,0 +1,113 @@
<template>
<div>
<el-dialog title="赠送优惠券" v-model="show" @close="reset" width="400px">
<el-form :model="form" label-width="120px">
<el-form-item label="赠送用户">
<div class="flex" v-if="user">
<el-avatar :src="form.name" :size="60" shape="square" />
<div class="ml-[18px]">
<p class="text-size-lg">{{ user.nickName }}</p>
<p class="color-999 mt-1">id:{{ user.userId }}</p>
</div>
</div>
</el-form-item>
<el-form-item label="手机号:">
<span>{{ user.phone }}</span>
</el-form-item>
<el-form-item label="选择优惠券" required>
<el-select v-model="form.couponId" placeholder="请选择优惠券">
<el-option
v-for="coupon in couponList"
:key="coupon.id"
:label="coupon.title"
:value="coupon.id"
/>
</el-select>
</el-form-item>
<el-form-item label="数量" required>
<el-input-number
style="width: 160px"
v-model="form.num"
:min="1"
:step="1"
placeholder="请输入数量"
></el-input-number>
<span class="ml-2"></span>
</el-form-item>
</el-form>
<div style="text-align: right; margin-top: 20px">
<el-button @click="close">取消</el-button>
<el-button type="primary" @click="submit">{{ isedit ? "更新" : "提交" }}</el-button>
</div>
</el-dialog>
</div>
</template>
<script setup>
import couponApi from "@/api/market/coupon";
import { ref, toRaw } from "vue";
import { ElMessage } from "element-plus";
//
const show = ref(false);
//
const form = ref({
userId: "",
couponId: "",
num: 1,
});
//
const couponList = ref([]);
//
function reset() {
form.value = {
userId: "",
couponId: "",
num: 1,
};
}
const emits = defineEmits(["submitSuccess"]);
//
function submit() {
console.log("提交表单数据:", form.value);
if (!form.value.couponId) {
return ElMessage.error("请选择优惠券");
}
if (form.value.num <= 0) {
return ElMessage.error("请输入数量");
}
couponApi.giveCoupon(form.value).then((res) => {
if (res) {
ElMessage.success("优惠券发放成功");
emits("submitSuccess", form.value);
close();
}
});
}
const user = ref("");
function open(data) {
console.log("打开表单:", data);
user.value = data;
form.value.userId = data.userId;
show.value = true;
}
function close() {
show.value = false;
reset();
}
defineExpose({ open, close, reset, submit });
onMounted(() => {
couponApi.getList({ size: 999 }).then((res) => {
if (res) {
couponList.value = res.records || [];
}
});
});
</script>

View File

@ -47,14 +47,21 @@
<el-table-column label="操作">
<template #default="scope">
<el-link :underline="false" type="primary" size="mini">查看</el-link>
<el-button :underline="false" type="danger" size="mini">删除</el-button>
<el-link
:underline="false"
type="danger"
class="ml-4"
@click="deleteCoupon(scope.row)"
size="mini"
>
删除
</el-link>
</template>
</el-table-column>
</el-table>
<el-pagination
v-model:current-page="pagination.currentPage"
v-model:page-size="pagination.pageSize"
v-model:current-page="pagination.page"
v-model:page-size="pagination.size"
:total="pagination.total"
layout="total, sizes, prev, pager, next, jumper"
@current-change="getList"
@ -82,11 +89,16 @@ const form = reactive({
userId: "",
page: 1,
});
const tableData = ref([]);
const pagination = reactive({
total: 0,
size: 10,
page: 1,
});
function open(data) {
console.log(data);
data.userId = form.userId;
form.page = 1;
form.userId = data.userId;
pagination.page = 1;
visible.value = true;
getList();
}
@ -94,19 +106,25 @@ function close() {
visible.value = false;
}
function getList() {
couponApi.getDetail(form).then((res) => {
couponApi.getDetail({ ...form, ...pagination }).then((res) => {
console.log(res);
tableData.value = res.records;
pagination.total = res.totalRow;
});
}
const tableData = ref([]);
const pagination = ref({
total: 0,
pageSize: 10,
currentPage: 1,
});
function deleteCoupon(row) {
couponApi
.delete({
id: row.id,
})
.then((res) => {
if (res.code === 200) {
ElMessage.success("删除成功");
getList();
}
});
}
defineExpose({
open,
close,

View File

@ -106,6 +106,7 @@ const contentConfig: IContentConfig<any> = {
options: [
{ label: '增减余额', command: 'change-money' },
{ label: '充值记录', command: 'charge-list' },
{ label: '赠送券', command: 'give-coupon' },
]
},
],

View File

@ -120,11 +120,14 @@
<!-- 用户优惠券详情 -->
<UserCouponDialog ref="userCouponDialogRef"></UserCouponDialog>
<!-- 赠送券 -->
<GiveCoupon ref="GiveCouponRef"></GiveCoupon>
</div>
</template>
<script setup >
import UserCouponDialog from "./components/user-coupon-dialog.vue";
import GiveCoupon from "./components/give-coupon.vue";
import usePage from "@/components/CURD/usePage";
import addModalConfig from "./config/add";
import contentConfig from "./config/content";
@ -135,7 +138,7 @@ import { returnOptionsLabel } from "./config/config";
import shopUserApi from "@/api/account/shopUser";
const editMoneyModalRef = ref(null);
const userCouponDialogRef = ref(null);
const GiveCouponRef = ref(null);
//
function handleViewCoupon(row) {
userCouponDialogRef.value.open(row);
@ -214,6 +217,8 @@ function handleToolbarClick(name) {
}
}
//
function toGiveCoupon() {}
//
async function handleOperatClick(data) {
const row = data.row;
@ -228,6 +233,10 @@ async function handleOperatClick(data) {
toCharge({ userId: data.row.userId });
return;
}
if (data.command === "give-coupon") {
GiveCouponRef.value.open(row);
return;
}
return;
}
}