修改确认订单逻辑
This commit is contained in:
232
stores/carts.js
232
stores/carts.js
@@ -1,11 +1,24 @@
|
||||
import {
|
||||
defineStore
|
||||
} from 'pinia';
|
||||
import yskUtils from 'ysk-utils'
|
||||
const {
|
||||
OrderPriceCalculator,
|
||||
BaseCartItem,
|
||||
BackendCoupon,
|
||||
ActivityConfig,
|
||||
OrderExtraConfig,
|
||||
MerchantReductionConfig,
|
||||
MerchantReductionType,
|
||||
GoodsType
|
||||
} = yskUtils
|
||||
|
||||
import {
|
||||
ref,
|
||||
computed,
|
||||
reactive,
|
||||
watchEffect
|
||||
watchEffect,
|
||||
watch
|
||||
} from 'vue';
|
||||
import {
|
||||
productminiApphotsquery,
|
||||
@@ -13,16 +26,156 @@ import {
|
||||
} from "@/common/api/product/product.js";
|
||||
export const useCartsStore = defineStore('cart',
|
||||
() => {
|
||||
// const dinersNum = uni.cache.get('dinersNum')
|
||||
// const isVip = uni.cache.get('orderVIP').isVip //此用户是否是会员
|
||||
// const isMemberPrice = uni.cache.get('ordershopUserInfo').isMemberPrice //此店是否可以用会员
|
||||
// const isTableFee = uni.cache.get('ordershopUserInfo').isTableFee //此店是否免桌位费
|
||||
// const tableFee = uni.cache.get('ordershopUserInfo').tableFee //一个餐位费多钱
|
||||
|
||||
// 商品订单会员
|
||||
const shopInfo = ref(uni.cache.get('shopInfo') || {
|
||||
isMemberPrice: 0,
|
||||
isTableFee: 1
|
||||
})
|
||||
|
||||
// 适配工具库 BaseCartItem 接口的商品数据转换函数
|
||||
const convertToBaseCartItem = (item) => {
|
||||
const skuData = item.skuData ? {
|
||||
id: item.skuData.id || item.sku_id,
|
||||
salePrice: item.skuData.salePrice || 0,
|
||||
memberPrice: item.skuData.memberPrice || 0
|
||||
} :
|
||||
undefined;
|
||||
|
||||
|
||||
|
||||
return {
|
||||
...item,
|
||||
id: item.id,
|
||||
product_id: item.product_id || item.productId,
|
||||
salePrice: item.salePrice || item.price,
|
||||
number: item.number || item.num || 0,
|
||||
product_type: item.productType,
|
||||
is_temporary: !!(item.is_temporary || item.isTemporary),
|
||||
is_gift: !!(item.is_gift || item.isGift),
|
||||
returnNum: item.returnNum || 0,
|
||||
memberPrice: item.memberPrice || 0,
|
||||
discountSaleAmount: item.discount_sale_amount || item.discountSaleAmount || 0,
|
||||
packFee: item.packFee || 0,
|
||||
packNumber: item.pack_number || item.packNumber || 0,
|
||||
activityInfo: item.activityInfo ? {
|
||||
type: item.activityInfo.type,
|
||||
discountRate: OrderPriceCalculator.formatDiscountRate(item.activityInfo.discountRate),
|
||||
vipPriceShare: !!item.activityInfo.vipPriceShare
|
||||
} : undefined,
|
||||
skuData
|
||||
};
|
||||
};
|
||||
|
||||
// 合并所有商品列表
|
||||
const allGoods = computed(() => {
|
||||
const currentGoods = (carts.value).map(convertToBaseCartItem);
|
||||
const giftGoods = [].map(convertToBaseCartItem);
|
||||
// 扁平化历史订单商品
|
||||
const oldOrderGoods = Object.values(oldOrder.value.detailMap || {})
|
||||
.flat()
|
||||
.map(convertToBaseCartItem);
|
||||
return [...currentGoods, ...giftGoods, ...oldOrderGoods];
|
||||
})
|
||||
|
||||
|
||||
// 就餐类型 'dine-in' | 'take-out'
|
||||
|
||||
let dinnerType = ref('dine-in');
|
||||
|
||||
function setDinnerType(str) {
|
||||
dinnerType.value = str
|
||||
}
|
||||
//餐位费配置
|
||||
|
||||
const dinersNum = uni.cache.set('dinersNum') || 1
|
||||
const seatFeeConfig = ref({
|
||||
pricePerPerson: shopInfo.value.tableFee || 1,
|
||||
personCount: dinersNum,
|
||||
isEnabled: !shopInfo.value.isTableFee
|
||||
})
|
||||
watch(() => shopInfo.value.isTableFee, (newval) => {
|
||||
seatFeeConfig.value.isEnabled = !shopInfo.value.isTableFee
|
||||
})
|
||||
|
||||
function setSeatFeeConfig(key, val) {
|
||||
seatFeeConfig.value[key] = val;
|
||||
}
|
||||
watch(() => seatFeeConfig.value, (newval) => {
|
||||
console.log('seatFeeConfig', seatFeeConfig.value);
|
||||
}, {
|
||||
deep: true
|
||||
})
|
||||
|
||||
//积分规则
|
||||
const pointDeductionRule = ref({
|
||||
pointsPerYuan: 100,
|
||||
maxDeductionAmount: Infinity
|
||||
})
|
||||
|
||||
function setPointDeductionRule(pointsPerYuan, maxDeductionAmount) {
|
||||
pointDeductionRule.value.pointsPerYuan = pointsPerYuan
|
||||
pointDeductionRule.value.maxDeductionAmount = maxDeductionAmount
|
||||
}
|
||||
|
||||
// 初始配置:默认无减免(固定金额 0 元)
|
||||
const merchantReductionConfig = ref({
|
||||
type: 'fixed_amount',
|
||||
fixedAmount: 0
|
||||
});
|
||||
//使用积分数量
|
||||
const userPoints = ref(0);
|
||||
|
||||
function setUserPoints(newval) {
|
||||
userPoints.value = newval
|
||||
}
|
||||
|
||||
|
||||
//新客立减
|
||||
const newUserDiscount = ref(0);
|
||||
// 订单额外配置
|
||||
const orderExtraConfig = computed(() => ({
|
||||
// 引用扩展后的商家减免配置
|
||||
merchantReduction: merchantReductionConfig.value,
|
||||
additionalFee: 0,
|
||||
pointDeductionRule: pointDeductionRule.value,
|
||||
seatFeeConfig: seatFeeConfig.value,
|
||||
currentStoreId: '',
|
||||
userPoints: userPoints.value,
|
||||
isMember: useVipPrice.value,
|
||||
memberDiscountRate: 1,
|
||||
newUserDiscount: newUserDiscount.value
|
||||
}));
|
||||
|
||||
// 营销活动列表
|
||||
const activityList = computed(() => {
|
||||
return [];
|
||||
});
|
||||
// 优惠券列表
|
||||
const backendCoupons = ref([])
|
||||
|
||||
function setCoupons(cps) {
|
||||
console.log('setCoupons', cps);
|
||||
backendCoupons.value = cps;
|
||||
}
|
||||
// 商品加入购物车顺序
|
||||
const cartOrder = ref({});
|
||||
// 订单费用汇总
|
||||
const orderCostSummary = computed(() => {
|
||||
const costSummary = OrderPriceCalculator.calculateOrderCostSummary(
|
||||
allGoods.value,
|
||||
dinnerType.value,
|
||||
backendCoupons.value,
|
||||
activityList.value,
|
||||
orderExtraConfig.value,
|
||||
cartOrder.value,
|
||||
new Date()
|
||||
);
|
||||
return costSummary;
|
||||
});
|
||||
|
||||
const goodsIsloading = ref(true);
|
||||
|
||||
|
||||
|
||||
//商品数据Map
|
||||
const goodsMap = reactive({})
|
||||
//获取商品数据
|
||||
@@ -62,13 +215,6 @@ export const useCartsStore = defineStore('cart',
|
||||
let skuData = undefined;
|
||||
skuData = goods?.skuList.find((sku) => sku.id == v.sku_id);
|
||||
|
||||
// if (goods.type == 'package') {
|
||||
// //套餐商品
|
||||
// const SnapSku = findInGroupSnapSku(goods.groupSnap, v.sku_id)
|
||||
// skuData = { ...SnapSku, salePrice: SnapSku ? SnapSku.price : 0 }
|
||||
// } else {
|
||||
// skuData = goods?.skuList.find((sku: { id: string, salePrice: number }) => sku.id == v.sku_id);
|
||||
// }
|
||||
skuData = goods?.skuList.find((sku) => sku.id == v.sku_id);
|
||||
|
||||
if (skuData) {
|
||||
@@ -135,7 +281,6 @@ export const useCartsStore = defineStore('cart',
|
||||
if (Message.operate_type == 'del' && Message.status == 1) {
|
||||
// 优化:使用可选链操作符避免报错
|
||||
carts.value = carts.value.filter(item => item.id !== msgData?.id);
|
||||
// carts.value = carts.value.filter(item => item.id != Message.data.id);
|
||||
}
|
||||
|
||||
// 添加或者减少购物后返回
|
||||
@@ -167,7 +312,6 @@ export const useCartsStore = defineStore('cart',
|
||||
}
|
||||
|
||||
if (Message.type == 'no_suit_num') {
|
||||
// console.log(specifications)
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
showCancel: false,
|
||||
@@ -177,9 +321,6 @@ export const useCartsStore = defineStore('cart',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,6 +334,10 @@ export const useCartsStore = defineStore('cart',
|
||||
originAmount: 0
|
||||
})
|
||||
|
||||
function setOldOrder(data) {
|
||||
oldOrder.value = data
|
||||
}
|
||||
|
||||
|
||||
// 会员信息
|
||||
const orderVIP = ref(uni.cache.get('orderVIP') || {
|
||||
@@ -210,10 +355,7 @@ export const useCartsStore = defineStore('cart',
|
||||
}
|
||||
}
|
||||
|
||||
// 商品订单会员
|
||||
const shopInfo = ref(uni.cache.get('shopInfo') || {
|
||||
isMemberPrice: 0
|
||||
})
|
||||
|
||||
|
||||
//是否使用会员价
|
||||
const useVipPrice = computed(() => {
|
||||
@@ -264,16 +406,6 @@ export const useCartsStore = defineStore('cart',
|
||||
return money
|
||||
})
|
||||
|
||||
//商品券抵扣金额
|
||||
// const productCouponDiscountAmount = computed(() => {
|
||||
// let index = -1;
|
||||
// return quansSelArr.value.reduce((pre, cur) => {
|
||||
// index++;
|
||||
// return pre + returnProDiscount(cur, index) * 1;
|
||||
// }, 0);
|
||||
// });
|
||||
|
||||
|
||||
//返回打包数量(称重商品打包数量最大为1)
|
||||
function returnCartPackNumber(cur) {
|
||||
const maxReturnNum = cur.number - (cur.returnNum || 0);
|
||||
@@ -343,8 +475,6 @@ export const useCartsStore = defineStore('cart',
|
||||
if (!matchedProducts || !Array.isArray(matchedProducts)) {
|
||||
return 0;
|
||||
}
|
||||
// console.log(uni.cache.get('orderVIP').isVip, uni.cache.get('ordershopUserInfo').isMemberPrice,
|
||||
// 111)
|
||||
// 购物车总数价格
|
||||
console.log('isBwc');
|
||||
console.log(isBwc);
|
||||
@@ -366,8 +496,6 @@ export const useCartsStore = defineStore('cart',
|
||||
|
||||
cart = cart.toFixed(2)
|
||||
console.log(parseFloat(cart))
|
||||
// 向上取整并保留两位小数
|
||||
// let result = roundUpToTwoDecimals(cart, 'upward')
|
||||
return parseFloat(cart);
|
||||
};
|
||||
|
||||
@@ -399,9 +527,6 @@ export const useCartsStore = defineStore('cart',
|
||||
|
||||
if (uni.cache.get('ordershopUserInfo').isTableFee == 0 && seatNum) {
|
||||
cart = parseFloat(seatNum) * parseFloat(uni.cache.get('ordershopUserInfo').tableFee)
|
||||
|
||||
// Math.ceil(parseFloat(seatNum) * parseFloat(
|
||||
// uni.cache.get('ordershopUserInfo').tableFee) * 100) / 100;
|
||||
}
|
||||
// 向下取整并保留两位小数
|
||||
let result = roundUpToTwoDecimals(cart, 'downward')
|
||||
@@ -417,18 +542,9 @@ export const useCartsStore = defineStore('cart',
|
||||
// 向下取整并保留两位小数
|
||||
let result = roundUpToTwoDecimals(total, 'downward')
|
||||
return result;
|
||||
// return Math.floor(total * 100) / 100;
|
||||
});
|
||||
|
||||
//获取热门商品
|
||||
async function getHotProduct() {
|
||||
|
||||
}
|
||||
|
||||
//获取分组商品
|
||||
async function getGroupProduct() {
|
||||
|
||||
}
|
||||
|
||||
return {
|
||||
getTotalPackFee,
|
||||
@@ -446,7 +562,21 @@ export const useCartsStore = defineStore('cart',
|
||||
totalPackFee,
|
||||
updateData,
|
||||
useVipPrice,
|
||||
totalOriginPrice
|
||||
totalOriginPrice,
|
||||
orderCostSummary,
|
||||
setCoupons,
|
||||
setUserPoints,
|
||||
setPointDeductionRule,
|
||||
setOldOrder,
|
||||
//优惠券列表
|
||||
backendCoupons,
|
||||
allGoods,
|
||||
setDinnerType,
|
||||
setSeatFeeConfig,
|
||||
seatFeeConfig,
|
||||
shopInfo,
|
||||
//新客立减金额
|
||||
newUserDiscount
|
||||
};
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user