优化更新
This commit is contained in:
@@ -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 limitTimeDiscountApi from '@/api/market/limitTimeDiscount.js'
|
||||
import * as UTILS from "@/utils/coupon-utils.js";
|
||||
import { BigNumber } from "bignumber.js";
|
||||
import _ from "lodash";
|
||||
@@ -15,6 +16,11 @@ import {
|
||||
OrderExtraConfig, MerchantReductionConfig, MerchantReductionType,
|
||||
GoodsType, FullReductionActivity
|
||||
} from "@/utils/goods";
|
||||
// import yskUtils from 'ysk-utils';
|
||||
// const OrderPriceCalculator = yskUtils.OrderPriceCalculator
|
||||
|
||||
import { useUserStore } from "@/store/modules/user";
|
||||
import { canUseLimitTimeDiscount, returnPrice } from '@/utils/order-utils'
|
||||
|
||||
const shopUser = useUserStoreHook();
|
||||
export interface CartsState {
|
||||
@@ -72,7 +78,9 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
vipPriceShare: !!item.activityInfo.vipPriceShare
|
||||
}
|
||||
: undefined,
|
||||
skuData
|
||||
skuData,
|
||||
is_time_discount: item.isTimeDiscount,
|
||||
isTimeDiscount: item.isTimeDiscount
|
||||
};
|
||||
};
|
||||
|
||||
@@ -85,10 +93,16 @@ 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(null)
|
||||
|
||||
// ------------------------------ 2. Store 内部原有响应式变量 ------------------------------
|
||||
// 选择用户
|
||||
const vipUser = ref<{ id?: string | number, isVip?: boolean }>({});
|
||||
@@ -119,16 +133,78 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
originAmount: 0
|
||||
});
|
||||
|
||||
/**
|
||||
* 异步查询链接/任务是否完成,完成后设置 isLinkFinshed.value = true
|
||||
* @param {number} [interval=1000] - 查询间隔时间(毫秒),默认 1 秒
|
||||
* @returns {Promise<void>} - 完成后 resolve,可通过 await 等待后续操作
|
||||
*/
|
||||
async function checkLinkFinished(interval = 100) {
|
||||
return new Promise((resolve) => {
|
||||
let elapsedTime = 0; // 已消耗时间
|
||||
const checkTimer = setInterval(async () => {
|
||||
// 2. 实际查询逻辑(这里替换为你的真实查询逻辑,比如调用接口)
|
||||
try {
|
||||
if (isLinkFinshed.value == true) {
|
||||
clearInterval(checkTimer);
|
||||
resolve();
|
||||
} else {
|
||||
console.log('链接未完成,继续查询...');
|
||||
elapsedTime += interval;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('查询出错,继续重试:', error);
|
||||
elapsedTime += interval;
|
||||
}
|
||||
}, interval);
|
||||
});
|
||||
}
|
||||
|
||||
// 代客下单页面商品缓存
|
||||
const goods = useStorage<any[]>("Instead_goods", []);
|
||||
async function getGoods(query: any) {
|
||||
// 获取当前店铺可用的限时折扣
|
||||
limitDiscountRes.value = await limitTimeDiscountApi.getLimitTimeDiscount({
|
||||
shopId: localStorage.getItem("shopId")
|
||||
})
|
||||
|
||||
// 获取满减活动
|
||||
fullReductionActivities.value = await limitTimeDiscountApi.getDiscountActivity({
|
||||
shopId: localStorage.getItem("shopId")
|
||||
})
|
||||
|
||||
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,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
console.log('代客下单页面商品缓存.获取当前店铺可用的限时折扣', limitDiscountRes.value);
|
||||
|
||||
const res = await productApi.getPage({
|
||||
page: 1,
|
||||
size: 999,
|
||||
status: "on_sale",
|
||||
...query,
|
||||
});
|
||||
goods.value = res.records;
|
||||
|
||||
const shopInfo = JSON.parse(localStorage.getItem('userInfo'))
|
||||
goods.value = res.records.map(item => {
|
||||
return {
|
||||
...item,
|
||||
isLimitDiscount: limitDiscountRes.value !== null && canUseLimitTimeDiscount(item, limitDiscountRes.value, shopInfo, shopUser.userInfo),
|
||||
limitDiscountPrice: limitDiscountRes.value !== null && returnPrice({ goods: { ...item, memberPrice: item.lowMemberPrice, salePrice: item.lowPrice }, shopInfo: shopInfo, limitTimeDiscountRes: limitDiscountRes.value, shopUserInfo: shopUser.userInfo, idKey: 'id' })
|
||||
}
|
||||
});
|
||||
|
||||
console.log('代客下单页面商品缓存.goods.value', goods.value);
|
||||
|
||||
setGoodsMap(goods.value);
|
||||
}
|
||||
|
||||
@@ -215,35 +291,15 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
pointsPerYuan: 100,
|
||||
maxDeductionAmount: Infinity
|
||||
})
|
||||
|
||||
const fullReductionActivities = ref([])
|
||||
|
||||
//使用积分数量
|
||||
const userPoints = ref(0);
|
||||
const testFullReductionActivity: FullReductionActivity = {
|
||||
"id": 231,
|
||||
"shopId": 26,
|
||||
"status": 2, // 2=进行中
|
||||
"sort": 10,
|
||||
"createTime": "2025-10-14 13:56:07",
|
||||
"updateTime": "2025-10-14 14:41:02", // 最新修改
|
||||
"validStartTime": "2025-10-14",
|
||||
"validEndTime": "2025-12-14",
|
||||
"useType": "dine,pickup,deliv,express", // 支持所有就餐类型
|
||||
"useDays": "周一,周二,周三,周四,周五,周六,周日", // 全周期
|
||||
"useTimeType": "all", // 全时段
|
||||
"useStartTime": '',
|
||||
"useEndTime": '',
|
||||
"couponShare": 0, // 与优惠券不同享
|
||||
"discountShare": 0, // 与限时折扣不同享
|
||||
"vipPriceShare": 0, // 与会员价不同享
|
||||
"pointsShare": 0, // 与积分不同享
|
||||
"thresholds": [ // 多门槛(此处1个)
|
||||
{
|
||||
"activityId": 231,
|
||||
"fullAmount": 1, // 满1元
|
||||
"discountAmount": 10 // 减10元
|
||||
}
|
||||
],
|
||||
"isDel": false,
|
||||
};
|
||||
|
||||
// 新客立减金额
|
||||
const newUserDiscount = ref(0)
|
||||
|
||||
// 订单额外配置(现在依赖响应式的 merchantReduction)
|
||||
const orderExtraConfig = computed<OrderExtraConfig>(() => ({
|
||||
// 引用扩展后的商家减免配置
|
||||
@@ -255,9 +311,11 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
userPoints: userPoints.value,
|
||||
isMember: useVipPrice.value,
|
||||
memberDiscountRate: shopUser.userInfo.memberDiscountRate || 1,
|
||||
fullReductionActivities: [testFullReductionActivity],
|
||||
currentDinnerType: dinnerType.value
|
||||
|
||||
fullReductionActivities: fullReductionActivities.value,
|
||||
currentDinnerType: dinnerType.value,
|
||||
limitTimeDiscount: limitDiscountRes.value,
|
||||
shopUserInfo: shopUser.userInfo,
|
||||
newUserDiscount: newUserDiscount.value
|
||||
}));
|
||||
|
||||
// 营销活动列表
|
||||
@@ -305,7 +363,7 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
// 订单费用汇总(调用内部的 getAllGoodsList)
|
||||
const orderCostSummary = computed(() => {
|
||||
allGoods.value = getAllGoodsList();
|
||||
console.log(' allGoods.value', allGoods.value);
|
||||
console.log(' allGoods.value+++++++++++++++++++++++++', allGoods.value);
|
||||
console.log(' orderExtraConfig.value', orderExtraConfig.value);
|
||||
const costSummary = OrderPriceCalculator.calculateOrderCostSummary(
|
||||
allGoods.value,
|
||||
@@ -316,7 +374,7 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
cartOrder.value,
|
||||
new Date()
|
||||
);
|
||||
console.log('costSummary', costSummary);
|
||||
console.log('costSummary----------------------------', costSummary);
|
||||
|
||||
return costSummary;
|
||||
});
|
||||
@@ -464,10 +522,19 @@ 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
|
||||
}
|
||||
})
|
||||
|
||||
const msg = { ...basic_msg, ...data };
|
||||
const hasCart = list.value.find((cartItem) => {
|
||||
return cartItem.product_id == msg.product_id && cartItem.sku_id == msg.sku_id;
|
||||
});
|
||||
|
||||
console.log('carts.add===', data);
|
||||
|
||||
if (hasCart) {
|
||||
update({ ...hasCart, ...msg, number: hasCart.number * 1 + msg.number * 1 });
|
||||
} else {
|
||||
@@ -566,6 +633,7 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
if (res) setOldOrder(res);
|
||||
}
|
||||
|
||||
// 购物车信息补全
|
||||
function getProductDetails(v: { product_id: string, sku_id: string }) {
|
||||
const goods = goodsMap[v.product_id];
|
||||
console.log('getProductDetails', goods)
|
||||
@@ -579,6 +647,7 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
|
||||
if (skuData) {
|
||||
return {
|
||||
limitDiscountPrice: goods.limitDiscountPrice,
|
||||
salePrice: skuData.salePrice || 0,
|
||||
memberPrice: skuData.memberPrice || 0,
|
||||
coverImg: goods.coverImg,
|
||||
@@ -613,10 +682,13 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
product_name: v.productName,
|
||||
sku_name: v.skuName,
|
||||
sku_id: v.skuId,
|
||||
product_type: v.productType
|
||||
product_type: v.productType,
|
||||
is_time_discount: v.isTimeDiscount
|
||||
};
|
||||
});
|
||||
}
|
||||
console.log('returnDetailMap.newData================', newData);
|
||||
|
||||
return newData;
|
||||
}
|
||||
|
||||
@@ -645,8 +717,10 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
function concocatSocket(initParams = $initParams) {
|
||||
WebSocketManager.subscribeToTopic(initParams, (msg) => {
|
||||
console.log("收到消息:", msg);
|
||||
if (msg.hasOwnProperty('status') && msg.status !== 1) {
|
||||
return ElMessage.error(msg.message || '操作失败');
|
||||
if (!msg.status) {
|
||||
if (msg.hasOwnProperty('status') && msg.status !== 1) {
|
||||
return ElMessage.error(msg.message || '操作失败');
|
||||
}
|
||||
}
|
||||
if (msg?.data) {
|
||||
if (Array.isArray(msg.data) && msg.data.length && msg.data[0].table_code) {
|
||||
@@ -834,11 +908,14 @@ export const useCartsStore = defineStore("carts", () => {
|
||||
clearMerchantReduction,
|
||||
seatFeeConfig,
|
||||
pointDeductionRule,
|
||||
// 新客立减金额
|
||||
newUserDiscount,
|
||||
//使用积分数量
|
||||
userPoints,
|
||||
coupons,
|
||||
setCoupons,
|
||||
payParamsInit
|
||||
payParamsInit,
|
||||
limitDiscountRes
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user