优化代客下单限时折扣

This commit is contained in:
gyq
2025-11-13 18:16:11 +08:00
parent 2db23c3370
commit fa7a9f87fb
8 changed files with 330 additions and 398 deletions

View File

@@ -3,6 +3,7 @@ import WebSocketManager, { type ApifoxModel, msgType } from "@/utils/websocket";
import orderApi from "@/api/order/order";
import { useUserStoreHook } from "@/store/modules/user";
import productApi from "@/api/product/index";
import shopUserApi from '@/api/account/shopUser'
import limitTimeDiscountApi from '@/api/market/limitTimeDiscount.js'
import * as UTILS from "@/utils/coupon-utils.js";
import { BigNumber } from "bignumber.js";
@@ -49,6 +50,13 @@ interface PointDeductionRule {
}
export const useCartsStore = defineStore("carts", () => {
const shopInfoStr = localStorage.getItem('userInfo') || '{}';
const shopInfo = JSON.parse(shopInfoStr);
const limitDiscountRes = ref<Record<string, any> | null>(null);
const shopInfoTyped = shopInfo as Record<string, any>;
const ldRes = limitDiscountRes.value as LimitDiscountResult | null;
// ------------------------------ 1. 移到内部的工具函数(核心修复) ------------------------------
// 适配工具库 BaseCartItem 接口的商品数据转换函数(原外部函数,现在内部)
const convertToBaseCartItem = (item: any): BaseCartItem => {
@@ -60,6 +68,8 @@ export const useCartsStore = defineStore("carts", () => {
}
: undefined;
// console.log('skuData===========', skuData);
let productType: GoodsType = GoodsType.NORMAL;
switch (item.product_type) {
case 'weight': productType = GoodsType.WEIGHT; break;
@@ -68,7 +78,10 @@ export const useCartsStore = defineStore("carts", () => {
default: productType = GoodsType.NORMAL;
}
return {
...item,
id: item.id,
product_id: item.product_id,
number: item.number || 0,
@@ -88,9 +101,7 @@ export const useCartsStore = defineStore("carts", () => {
}
: undefined,
skuData,
is_time_discount: item.is_time_discount,
isTimeDiscount: item.is_time_discount,
salePrice: item.limitDiscountPrice || 0
salePrice: item.salePrice
};
};
@@ -103,31 +114,72 @@ export const useCartsStore = defineStore("carts", () => {
const oldOrderGoods = Object.values(oldOrder.value.detailMap || {})
.flat()
.map(convertToBaseCartItem);
// console.log('list=====================================', list.value);
// console.log('giftList=====================================', giftList.value);
// console.log('oldOrder.value.detailMap=====================================', oldOrder.value.detailMap);
// console.log('getAllGoodsList.[]===================', [...currentGoods, ...giftGoods, ...oldOrderGoods])
return [...currentGoods, ...giftGoods, ...oldOrderGoods];
};
const limitDiscountRes = ref<Record<string, any> | null>(null);
// ------------------------------ 2. Store 内部原有响应式变量 ------------------------------
// 选择用户
const vipUser = ref<{ id?: string | number, isVip?: boolean }>({});
async function changeUser(user: any) {
vipUser.value = {
...user,
isMemberPrice: shopUser.userInfo.isMemberPrice
};
console.log('changeUser', vipUser.value);
await getGoods({})
// 选择用户后重新刷新历史订单限时折扣信息
setOldOrder(oldOrder.value)
allGoods.value = getAllGoodsList()
console.log('选择用户后刷新所有订单购物车数据====', allGoods.value);
sendWsTimeDiscount()
payParamsInit()
}
// 给长连接发送更新购物车限时折扣信息
const sendWsTimeDiscount = _.throttle(function () {
// 在这里准备给ws发送刷新消息
const cart = (list.value || []).map(convertToBaseCartItem);
const history = Object.values(oldOrder.value.detailMap || {})
.flat()
.map(convertToBaseCartItem);
if (cart.length || history.length) {
WebSocketManager.sendMessage({
type: "shopping",
operate_type: "bulk_edit",
table_code: table_code.value,
shop_id: localStorage.getItem("shopId"),
data: {
cart: cart.map(item => {
return {
id: item.id,
is_time_discount: limitUtils.canUseLimitTimeDiscount(item, limitDiscountRes.value, shopInfoTyped, vipUser.value) ? 1 : 0
}
}),
history: history.map(item => {
return {
id: item.id,
is_time_discount: limitUtils.canUseLimitTimeDiscount(item, limitDiscountRes.value, shopInfoTyped, vipUser.value) ? 1 : 0
}
})
},
})
}
}, 3000)
// 就餐类型
let dinnerType = ref<'dine-in' | 'take-out'>('dine-in');
@@ -176,6 +228,16 @@ export const useCartsStore = defineStore("carts", () => {
});
}
const sendThrottledMessage = _.throttle(function () {
WebSocketManager.sendMessage({
type: "shopping",
operate_type: "time_discount_save",
table_code: table_code.value,
shop_id: localStorage.getItem("shopId"),
data: limitDiscountRes.value,
});
}, 3000);
// 代客下单页面商品缓存
const goods = useStorage<any[]>("Instead_goods", []);
async function getGoods(query: any) {
@@ -193,13 +255,7 @@ export const useCartsStore = defineStore("carts", () => {
if (limitDiscountRes.value !== null) {
checkLinkFinished().then(() => {
console.log('给ws发送限时折扣信息');
WebSocketManager.sendMessage({
type: "shopping",
operate_type: "time_discount_save",
table_code: table_code.value,
shop_id: localStorage.getItem("shopId"),
data: limitDiscountRes.value,
});
sendThrottledMessage()
})
}
@@ -212,9 +268,6 @@ export const useCartsStore = defineStore("carts", () => {
...query,
});
const shopInfoStr = localStorage.getItem('userInfo') || '{}';
const shopInfo = JSON.parse(shopInfoStr);
interface ProductItem {
id: string | number;
lowMemberPrice?: number;
@@ -232,21 +285,21 @@ export const useCartsStore = defineStore("carts", () => {
limitDiscountPrice: number;
}
const shopInfoTyped = shopInfo as Record<string, any>;
const ldRes = limitDiscountRes.value as LimitDiscountResult | null;
goods.value = (res.records as ProductItem[]).map((item: ProductItem): GoodsWithDiscount => {
item.salePrice = item.lowPrice
item.memberPrice = item.lowMemberPrice
return {
...item,
isLimitDiscount: ldRes !== null && limitUtils.canUseLimitTimeDiscount(item, ldRes, shopInfoTyped, vipUser.value, 'id'),
limitDiscountPrice: limitUtils.returnPrice({
goods: { ...item, memberPrice: item.lowMemberPrice, salePrice: item.lowPrice },
is_time_discount: limitUtils.canUseLimitTimeDiscount(item, limitDiscountRes.value, shopInfoTyped, vipUser.value) ? 1 : 0,
time_discount_price: limitUtils.returnPrice({
goods: { ...item },
shopInfo: shopInfoTyped,
limitTimeDiscountRes: ldRes,
limitTimeDiscountRes: limitDiscountRes.value,
shopUserInfo: vipUser.value,
idKey: 'id'
})
} as GoodsWithDiscount;
}
});
console.log('代客下单页面商品缓存.goods.value', goods.value);
@@ -361,7 +414,7 @@ export const useCartsStore = defineStore("carts", () => {
limitTimeDiscount: limitDiscountRes.value,
shopUserInfo: vipUser.value,
newUserDiscount: newUserDiscount.value
}));
}) as OrderExtraConfig);
// 营销活动列表
const activityList = computed<ActivityConfig[]>(() => {
@@ -405,8 +458,6 @@ export const useCartsStore = defineStore("carts", () => {
// 订单费用汇总(调用内部的 getAllGoodsList
const orderCostSummary = computed(() => {
allGoods.value = getAllGoodsList();
console.log('allGoods.value+++++++++++++++++++++++++', allGoods.value);
console.log('orderExtraConfig.value', orderExtraConfig.value);
const costSummary = OrderPriceCalculator.calculateOrderCostSummary(
allGoods.value,
dinnerType.value,
@@ -417,7 +468,6 @@ export const useCartsStore = defineStore("carts", () => {
new Date()
);
console.log('costSummary----------------------------', costSummary);
return costSummary;
});
@@ -565,7 +615,7 @@ export const useCartsStore = defineStore("carts", () => {
function add(data: any) {
goods.value.map(item => {
if (item.id == data.product_id) {
data.is_time_discount = item.isLimitDiscount ? 1 : 0
data.is_time_discount = item.is_time_discount ? 1 : 0
}
})
@@ -667,8 +717,10 @@ export const useCartsStore = defineStore("carts", () => {
}
}
// 获取历史订单信息
async function getOldOrder(table_code: string | number) {
const res = await orderApi.getHistoryList({ tableCode: table_code });
console.log('获取历史订单信息===', res);
if (res) setOldOrder(res);
}
@@ -684,9 +736,11 @@ export const useCartsStore = defineStore("carts", () => {
skuData = { ...SnapSku, salePrice: SnapSku ? SnapSku.price : 0 };
}
console.log('skuData===', skuData);
if (skuData) {
return {
limitDiscountPrice: goods.limitDiscountPrice,
salePrice: skuData.salePrice || 0,
memberPrice: skuData.memberPrice || 0,
coverImg: goods.coverImg,
@@ -694,12 +748,21 @@ export const useCartsStore = defineStore("carts", () => {
specInfo: skuData.specInfo,
packFee: goods.packFee || 0,
type: goods.type,
skuData
skuData,
is_time_discount: limitUtils.canUseLimitTimeDiscount(goods, limitDiscountRes.value, shopInfo, vipUser.value),
time_discount_price: limitUtils.returnPrice({
goods: skuData,
shopInfo: shopInfoTyped,
limitTimeDiscountRes: limitDiscountRes.value,
shopUserInfo: vipUser.value,
idKey: 'productId'
}),
};
}
return undefined;
}
// 历史订单信息补全
function returnDetailMap(data: any) {
const newData: { [key: string]: any } = {};
for (let i in data) {
@@ -722,20 +785,34 @@ export const useCartsStore = defineStore("carts", () => {
sku_name: v.skuName,
sku_id: v.skuId,
product_type: v.productType,
is_time_discount: v.isTimeDiscount
is_time_discount: limitUtils.canUseLimitTimeDiscount(v, limitDiscountRes.value, shopInfoTyped, vipUser.value, 'productId'),
time_discount_price: limitUtils.returnPrice({
goods: { ...v, ...skuData },
shopInfo: shopInfoTyped,
limitTimeDiscountRes: limitDiscountRes.value,
shopUserInfo: vipUser.value,
idKey: 'productId'
})
};
});
}
console.log('returnDetailMap.newData================', newData);
console.log('newData2025年11月13日', newData);
return newData;
}
function setOldOrder(data: any) {
// 初始话订单信息/补全历史订单信息
async function setOldOrder(data: any) {
console.log('补全订单信息', data);
oldOrder.value = {
...data,
detailMap: returnDetailMap(data.detailMap)
};
console.log('oldOrder.value.detailMap==', oldOrder.value.detailMap);
if (!vipUser.value.id && data.userId) {
const res = await shopUserApi.get({ userId: data.userId })
changeUser(res)
}
}
let $initParams = {} as ApifoxModel;
@@ -750,6 +827,9 @@ export const useCartsStore = defineStore("carts", () => {
table_code.value = initParams.table_code;
$initParams = initParams;
}
concocatSocket($initParams);
}
@@ -875,6 +955,11 @@ export const useCartsStore = defineStore("carts", () => {
msg.operate_type = 'manage_' + msg.operate_type;
concocatSocket(initParams);
}
if (msg.operate_type === "bulk_edit") {
msg.operate_type = 'manage_' + msg.operate_type;
concocatSocket({ ...$initParams, table_code: table_code.value });
}
});
}
@@ -955,7 +1040,8 @@ export const useCartsStore = defineStore("carts", () => {
setCoupons,
payParamsInit,
limitDiscountRes,
getAllGoodsList
getAllGoodsList,
vipUser
};
});