358 lines
10 KiB
TypeScript
358 lines
10 KiB
TypeScript
import { store } from "@/store";
|
|
import WebSocketManager, { type ApifoxModel, msgType } from "@/utils/websocket";
|
|
|
|
export interface CartsState {
|
|
id: string | number;
|
|
[property: string]: any;
|
|
}
|
|
|
|
export const useCartsStore = defineStore("carts", () => {
|
|
//台桌id
|
|
const table_code = ref('');
|
|
|
|
//当前购物车数据
|
|
const list = useStorage<any[]>("carts", []);
|
|
//赠菜
|
|
const giftList = useStorage<any[]>("giftList", []);
|
|
let goodsMap: { [key: string]: any } = {};
|
|
//当前选中cart
|
|
let selListIndex = ref(-1);
|
|
//当前选中商品是否是赠菜
|
|
const isSelGift = ref(false);
|
|
const defaultCart = {
|
|
id: '',
|
|
number: 0,
|
|
}
|
|
//购物车是否为空
|
|
const isEmpty = computed(() => {
|
|
return list.value.length === 0 && giftList.value.length === 0
|
|
})
|
|
//当前购物车选中数据
|
|
const selCart = computed(() => {
|
|
if (isSelGift.value) {
|
|
return giftList.value[selListIndex.value] || defaultCart
|
|
}
|
|
return list.value[selListIndex.value] || defaultCart
|
|
})
|
|
//当前购物车选中对应商品数据
|
|
const selGoods = computed(() => {
|
|
return goodsMap[selCart.value.product_id]
|
|
})
|
|
//当前选中购物车数据是否是可选套餐商品
|
|
const isCanSelectGroup = computed(() => {
|
|
if (!selGoods.value) {
|
|
return false
|
|
}
|
|
return selGoods.value.type == 'package' && selGoods.value.groupType == 1
|
|
})
|
|
//赠菜总价
|
|
const giftMoney = computed(() => {
|
|
return giftList.value.reduce((acc: number, cur: any) => {
|
|
return acc + cur.number * cur.salePrice
|
|
}, 0)
|
|
})
|
|
const yiyouhui = computed(() => {
|
|
const youhui = giftMoney.value
|
|
if (youhui > 0) {
|
|
return '已优惠¥' + youhui.toFixed(2)
|
|
}
|
|
return ''
|
|
})
|
|
|
|
//支付总价
|
|
const payMoney = computed(() => {
|
|
const money = list.value.reduce((acc: number, cur: any) => {
|
|
if (cur.is_temporary) {
|
|
//临时菜
|
|
return acc + cur.number * (cur.discount_sale_amount)
|
|
} else {
|
|
return acc + cur.number * (cur.salePrice)
|
|
}
|
|
}, 0)
|
|
return money.toFixed(2)
|
|
})
|
|
//总计数量
|
|
const totalNumber = computed(() => {
|
|
const cartNumber = list.value.reduce((acc: number, cur: any) => {
|
|
return acc + cur.number * 1
|
|
}, 0)
|
|
const giftNumber = list.value.reduce((acc: number, cur: any) => {
|
|
return acc + cur.number * 1
|
|
}, 0)
|
|
return cartNumber + giftNumber
|
|
})
|
|
|
|
|
|
function changeNumber(step: number, item: CartsState) {
|
|
if (item.number * 1 + step <= 0) {
|
|
del(item);
|
|
return;
|
|
}
|
|
const newNumber = item.number * 1 + step * 1;
|
|
update({ ...item, number: item.number * 1 + step * 1, pack_number: newNumber < item.pack_number ? (item.pack_number * 1 + step * 1) : item.pack_number });
|
|
}
|
|
|
|
function changeSelCart(cart: CartsState) {
|
|
console.log(cart)
|
|
if (!cart.id) {
|
|
return
|
|
}
|
|
if (cart.is_gift) {
|
|
isSelGift.value = true
|
|
selListIndex.value = giftList.value.findIndex((item: CartsState) => item.id === cart.id);
|
|
console.log(selListIndex.value)
|
|
|
|
} else {
|
|
isSelGift.value = false
|
|
selListIndex.value = list.value.findIndex((item: CartsState) => item.id === cart.id);
|
|
}
|
|
}
|
|
|
|
const basic_msg = {
|
|
number: 1,
|
|
is_pack: 0,
|
|
is_gift: 0,
|
|
is_temporary: 0,
|
|
discount_sale_amount: 0,
|
|
discount_sale_note: "",
|
|
is_print: 0,
|
|
is_wait_call: 0,
|
|
product_name: "",
|
|
remark: "",
|
|
sku_id: '',
|
|
}
|
|
//当前购物车直接添加
|
|
function cartsPush(data: any) {
|
|
sendMessage('add', {
|
|
...basic_msg,
|
|
...data
|
|
});
|
|
}
|
|
|
|
function add(data: any) {
|
|
const msg = {
|
|
...basic_msg,
|
|
...data
|
|
}
|
|
const hasCart = list.value.find((cartItem) => {
|
|
return cartItem.product_id == msg.product_id && cartItem.sku_id == msg.sku_id;
|
|
});
|
|
if (hasCart) {
|
|
update({ ...hasCart, ...msg, number: hasCart.number * 1 + msg.number * 1 })
|
|
} else {
|
|
console.log(msg);
|
|
sendMessage('add', msg);
|
|
}
|
|
}
|
|
|
|
// 换桌
|
|
function changeTable(newVal: string | number) {
|
|
if (table_code.value) {
|
|
sendMessage('rottable', {
|
|
new_table_code: newVal,
|
|
id: list.value[0].id
|
|
});
|
|
}
|
|
}
|
|
|
|
function del(data: any) {
|
|
sendMessage('del', data);
|
|
}
|
|
|
|
function update(data: any) {
|
|
console.log(data);
|
|
sendMessage('edit', data);
|
|
}
|
|
function updateTag(key: string, val: any, cart: CartsState) {
|
|
sendMessage('edit', { ...cart || selCart.value, [key]: val });
|
|
}
|
|
function clear() {
|
|
sendMessage('cleanup', {});
|
|
}
|
|
|
|
// 寻找套餐商品sku
|
|
interface GroupSnap {
|
|
goods: { [key: string]: any }[];
|
|
}
|
|
|
|
function findInGroupSnapSku(groupSnap: GroupSnap[], sku_id: string | number) {
|
|
for (let i in groupSnap) {
|
|
const sku = groupSnap[i].goods.find(v => v.sku_id == sku_id)
|
|
if (sku) {
|
|
return sku
|
|
}
|
|
}
|
|
}
|
|
|
|
function getProductDetails(v: { product_id: string, sku_id: string }) {
|
|
const goods = goodsMap[v.product_id]
|
|
if (!goods) {
|
|
return undefined
|
|
}
|
|
let skuData = undefined;
|
|
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);
|
|
}
|
|
if (skuData) {
|
|
return {
|
|
salePrice: skuData ? skuData.salePrice : 0,
|
|
coverImg: goods.coverImg,
|
|
name: goods.name
|
|
}
|
|
} else {
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
function init(initParams: ApifoxModel, $goodsMap: any) {
|
|
// 商品id对应的数据map
|
|
goodsMap = $goodsMap
|
|
table_code.value = initParams && initParams.table_code ? initParams.table_code : '';
|
|
WebSocketManager.subscribeToTopic(initParams, (msg) => {
|
|
console.log("收到消息:", msg);
|
|
if (msg.hasOwnProperty('status') && msg.status != 1) {
|
|
return ElMessage.error(msg.message || '操作失败')
|
|
}
|
|
if (msg && msg.data) {
|
|
if (Array.isArray(msg.data) && msg.data.length) {
|
|
table_code.value = msg.data[0].table_code
|
|
}
|
|
if (msg.data.table_code) {
|
|
table_code.value = table_code.value ? table_code.value : msg.data.table_code
|
|
}
|
|
}
|
|
// 初始化
|
|
if (msg.operate_type === "manage_init") {
|
|
// 设置单价
|
|
list.value = msg.data.filter((v: Record<string, any>) => {
|
|
const skuData = getProductDetails({ product_id: v.product_id, sku_id: v.sku_id })
|
|
if (skuData) {
|
|
(Object.keys(skuData) as (keyof typeof skuData)[]).forEach((key) => {
|
|
v[key] = skuData[key];
|
|
});
|
|
} else {
|
|
del({ id: v.id })
|
|
return false
|
|
}
|
|
return !v.is_gift
|
|
})
|
|
giftList.value = msg.data.filter((v: Record<string, any>) => {
|
|
const skuData = getProductDetails({ product_id: v.product_id, sku_id: v.sku_id })
|
|
if (skuData) {
|
|
(Object.keys(skuData) as (keyof typeof skuData)[]).forEach((key) => {
|
|
v[key] = skuData[key];
|
|
});
|
|
} else {
|
|
del({ id: v.id })
|
|
return false
|
|
}
|
|
return v.is_gift
|
|
})
|
|
console.log(giftList.value)
|
|
}
|
|
//广播
|
|
if (msg.type === "bc") {
|
|
msg.operate_type = 'manage_' + msg.operate_type
|
|
}
|
|
if (msg.operate_type === "manage_add") {
|
|
if (list.value.find(v => v.id == msg.data.id)) {
|
|
return ElMessage.warning(msg.message || '该商品已存在')
|
|
}
|
|
const skuData = getProductDetails({ product_id: msg.data.product_id, sku_id: msg.data.sku_id })
|
|
list.value.push({ ...skuData, ...msg.data })
|
|
return ElMessage.success(msg.message || '添加成功')
|
|
|
|
}
|
|
if (msg.operate_type === "manage_edit") {
|
|
const newCart = msg.data
|
|
const index = list.value.findIndex((item) => item.id === newCart.id)
|
|
const giftIndex = giftList.value.findIndex((item) => item.id === newCart.id)
|
|
const cartItem = list.value[index] || { is_gift: false };
|
|
const giftItem = giftList.value[giftIndex];
|
|
if (isSelGift.value) {
|
|
//操作赠菜
|
|
if (newCart.is_gift != giftItem.is_gift) {
|
|
//修改了赠菜状态
|
|
giftList.value.splice(giftIndex, 1)
|
|
list.value.push({ ...giftItem, ...newCart })
|
|
selListIndex.value = -1
|
|
} else {
|
|
giftList.value[giftIndex] = { ...giftItem, ...newCart }
|
|
}
|
|
ElMessage.success(msg.message || '修改成功')
|
|
return
|
|
}
|
|
if (!isSelGift.value) {
|
|
//操作非赠菜
|
|
if (newCart.is_gift != cartItem.is_gift) {
|
|
list.value.splice(index, 1)
|
|
giftList.value.push({ ...cartItem, ...newCart })
|
|
selListIndex.value = -1
|
|
} else {
|
|
list.value[index] = { ...cartItem, ...newCart }
|
|
}
|
|
ElMessage.success(msg.message || '修改成功')
|
|
return
|
|
}
|
|
|
|
}
|
|
if (msg.operate_type === "manage_del") {
|
|
if (!isSelGift.value) {
|
|
const index = list.value.findIndex((item) => item.id === msg.data.id)
|
|
if (index > -1) {
|
|
list.value.splice(index, 1)
|
|
if (list.value.length >= 1) {
|
|
selListIndex.value = index - 1;
|
|
}
|
|
return ElMessage.success(msg.message || '删除成功')
|
|
}
|
|
} else {
|
|
const index = giftList.value.findIndex((item) => item.id === msg.data.id)
|
|
if (index > -1) {
|
|
giftList.value.splice(index, 1)
|
|
if (giftList.value.length >= 1) {
|
|
selListIndex.value = index - 1;
|
|
}
|
|
return ElMessage.success(msg.message || '删除成功')
|
|
}
|
|
}
|
|
}
|
|
if (msg.operate_type === "manage_cleanup") {
|
|
list.value = []
|
|
giftList.value = []
|
|
table_code.value = ''
|
|
}
|
|
console.log(list.value)
|
|
});
|
|
}
|
|
function sendMessage(operate_type: msgType, message: any) {
|
|
const msg = { ...message, operate_type: operate_type, table_code: table_code.value }
|
|
WebSocketManager.sendMessage(msg);
|
|
}
|
|
return {
|
|
isCanSelectGroup,
|
|
selGoods,
|
|
cartsPush,
|
|
table_code,
|
|
updateTag,
|
|
list,
|
|
add,
|
|
del,
|
|
update,
|
|
init,
|
|
changeNumber, isEmpty,
|
|
selCart, totalNumber,
|
|
changeSelCart, payMoney,
|
|
clear, yiyouhui, giftList,
|
|
changeTable
|
|
};
|
|
});
|
|
|
|
export function useDictStoreHook() {
|
|
return useCartsStore(store);
|
|
}
|