购物车打包费和餐位费封装
This commit is contained in:
parent
b40af3ea23
commit
56799f41f6
|
|
@ -0,0 +1,70 @@
|
||||||
|
import {
|
||||||
|
defineStore
|
||||||
|
} from 'pinia';
|
||||||
|
import {
|
||||||
|
computed,
|
||||||
|
watchEffect
|
||||||
|
} from 'vue';
|
||||||
|
import {
|
||||||
|
ref
|
||||||
|
} from 'vue';
|
||||||
|
|
||||||
|
export const useCartStore = defineStore('cart', () => {
|
||||||
|
const shopInfo = uni.cache.get('shopInfo')
|
||||||
|
const dinersNum = uni.cache.get('dinersNum')
|
||||||
|
|
||||||
|
// 计算单个商品的打包费用(向下取整并保留两位小数)
|
||||||
|
const itemSinglePackFee = (item) => {
|
||||||
|
const fee = item.packFee * item.cartNumber;
|
||||||
|
// 先将结果乘以 100,向下取整,再除以 100 以保留两位小数
|
||||||
|
return Math.floor(fee * 100) / 100
|
||||||
|
};
|
||||||
|
|
||||||
|
// 计算购物车商品总价格
|
||||||
|
const getTotalTotalPrices = (matchedProducts) => computed(() => {
|
||||||
|
if (!matchedProducts || !Array.isArray(matchedProducts)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// 购物车总数价格
|
||||||
|
let cart = matchedProducts.reduce((total, item) => {
|
||||||
|
// 是否启用会员价 0否1是
|
||||||
|
if (shopInfo.isVip == 1 && shopInfo.isMemberPrice == 1) {
|
||||||
|
// memberPrice会员价
|
||||||
|
return total + parseFloat(item.memberPrice) * parseFloat(item.cartNumber);
|
||||||
|
} else {
|
||||||
|
// salePrice销售价
|
||||||
|
return total + parseFloat(item.salePrice) * parseFloat(item.cartNumber);
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
|
// 向上取整并保留两位小数
|
||||||
|
return cart = Math.ceil(cart * 100) / 100;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 桌位置
|
||||||
|
const getTotalSeatcharge = () => computed(() => {
|
||||||
|
// 是否免除桌位费 0 否 1 是
|
||||||
|
let tableFeeTotals = 0
|
||||||
|
if (shopInfo.isTableFee == 0 && dinersNum) {
|
||||||
|
const tableFeeTotals = Math.ceil(parseFloat(dinersNum) * parseFloat(shopInfo
|
||||||
|
.tableFee) * 100) / 100;
|
||||||
|
}
|
||||||
|
return Math.floor(tableFeeTotals * 100) / 100 ? Math.floor(tableFeeTotals * 100) / 100 : 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 计算购物车总打包费用(向下取整并保留两位小数)
|
||||||
|
const getTotalPackFee = (cartList) => computed(() => {
|
||||||
|
let total = 0;
|
||||||
|
for (const item of cartList) {
|
||||||
|
total += itemSinglePackFee(item);
|
||||||
|
}
|
||||||
|
return Math.floor(total * 100) / 100 ? Math.floor(total * 100) / 100 : 0;
|
||||||
|
// 同样对总费用进行向下取整并保留两位小数处理
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
itemSinglePackFee,
|
||||||
|
getTotalPackFee,
|
||||||
|
getTotalSeatcharge,
|
||||||
|
getTotalTotalPrices
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
@ -112,12 +112,12 @@ export const Memberpay = defineStore('memberpay', {
|
||||||
payType: 'aliPay',
|
payType: 'aliPay',
|
||||||
openId: uni.cache.get('alipayOpenId').wechatOpenId,
|
openId: uni.cache.get('alipayOpenId').wechatOpenId,
|
||||||
// #endif
|
// #endif
|
||||||
returnUrl: '',
|
returnUrl: data.returnUrl,
|
||||||
buyerRemark: data.buyerRemark,
|
buyerRemark: data.buyerRemark,
|
||||||
})
|
})
|
||||||
if (res) {
|
if (res) {
|
||||||
uni.showLoading({
|
uni.showLoading({
|
||||||
title: '加载中',
|
title: '支付吊起中...',
|
||||||
mask: true
|
mask: true
|
||||||
})
|
})
|
||||||
uni.requestPayment({
|
uni.requestPayment({
|
||||||
|
|
|
||||||
|
|
@ -182,7 +182,7 @@ export const productStore = defineStore('product', {
|
||||||
uni.cache.set('shopTable', res.shopTable)
|
uni.cache.set('shopTable', res.shopTable)
|
||||||
// 台桌信息
|
// 台桌信息
|
||||||
uni.cache.set('shopInfo', res.shopInfo)
|
uni.cache.set('shopInfo', res.shopInfo)
|
||||||
uni.cache.set('shopId', res.shopTable.shopId,30)
|
uni.cache.set('shopId', res.shopTable.shopId, 30)
|
||||||
// 当前用户距离店铺的米数
|
// 当前用户距离店铺的米数
|
||||||
uni.cache.set('distance', res.distance)
|
uni.cache.set('distance', res.distance)
|
||||||
|
|
||||||
|
|
@ -199,7 +199,7 @@ export const productStore = defineStore('product', {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
let res = await APIshopUserInfo()
|
let res = await APIshopUserInfo()
|
||||||
uni.cache.set('userInfo', res);
|
uni.cache.set('shopUserInfo', res);
|
||||||
resolve(true)
|
resolve(true)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(false)
|
reject(false)
|
||||||
|
|
@ -212,12 +212,13 @@ export const productStore = defineStore('product', {
|
||||||
actionsAPIuser() {
|
actionsAPIuser() {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
|
// 获取店铺用户会员信息
|
||||||
if (uni.cache.get('shopId')) {
|
if (uni.cache.get('shopId')) {
|
||||||
this.actionsproductqueryProduct()
|
this.actionsproductqueryProduct()
|
||||||
} else {
|
}
|
||||||
let res = await APIuser()
|
let res = await APIuser()
|
||||||
uni.cache.set('userInfo', res);
|
uni.cache.set('userInfo', res);
|
||||||
}
|
// }
|
||||||
resolve(true)
|
resolve(true)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(false)
|
reject(false)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue