增加代客下单页面

This commit is contained in:
2025-02-21 14:42:38 +08:00
parent e08a2eb4b7
commit f961bf7d92
24 changed files with 2780 additions and 199 deletions

228
src/store/modules/carts.ts Normal file
View File

@@ -0,0 +1,228 @@
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", () => {
//当前购物车数据
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 selCart = computed(() => {
if (isSelGift.value) {
return giftList.value[selListIndex.value] || defaultCart
}
return list.value[selListIndex.value] || defaultCart
})
//赠菜总价
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) => {
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
})
const table_code = ref('');
function changeNumber(step: number, item: CartsState) {
if (item.number * 1 + step <= 0) {
del(item);
return;
}
update({ ...item, number: step * 1 });
}
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);
}
}
function add(data: any) {
sendMessage('add', data);
}
function del(data: any) {
sendMessage('del', data);
}
function update(data: any) {
console.log(data);
sendMessage('edit', data);
}
function updateTag(key: string, val: any) {
sendMessage('edit', { ...selCart.value, number: 0, [key]: val });
}
function clear() {
sendMessage('cleanup', {});
}
function getProductDetails(v: { product_id: string, sku_id: string }) {
const goods = goodsMap[v.product_id]
const 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.operate_type === "manage_init") {
// 设置单价
list.value = msg.data.filter((v: { is_gift: any; }) => !v.is_gift).map((v: Record<string, any>) => {
const skuData = getProductDetails({ product_id: v.product_id, sku_id: v.sku_id })
return {
...skuData,
...v,
}
});
giftList.value = msg.data.filter((v: { is_gift: any; }) => v.is_gift).map((v: Record<string, any>) => {
const skuData = getProductDetails({ product_id: v.product_id, sku_id: v.sku_id })
return {
...skuData,
...v,
}
});
console.log(giftList.value)
}
if (msg.operate_type === "manage_add") {
const skuData = getProductDetails({ product_id: msg.data[0].product_id, sku_id: msg.data[0].sku_id })
list.value.push({ ...skuData, ...msg.data[0] })
return ElMessage.success(msg.message || '添加成功')
}
if (msg.operate_type === "manage_edit") {
const newCart = msg.data[0]
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];
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 }
}
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 }
}
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 || '删除成功')
}
}
}
console.log(list.value)
});
}
function sendMessage(operate_type: msgType, message: any) {
console.log({ ...message, operate_type: operate_type, table_code: table_code.value })
WebSocketManager.sendMessage({ ...message, operate_type: operate_type, table_code: table_code.value });
}
return {
updateTag,
list,
add,
del,
update,
init,
changeNumber,
selCart, totalNumber,
changeSelCart, payMoney,
clear, yiyouhui, giftList
};
});
export function useDictStoreHook() {
return useCartsStore(store);
}