293 lines
7.6 KiB
JavaScript
293 lines
7.6 KiB
JavaScript
// stores/counter.js
|
|
import { defineStore } from "pinia";
|
|
import yskUtils from "ysk-utils";
|
|
import { getNowCart } from "@/pagesCreateOrder/util.js";
|
|
import * as orderApi from "@/http/api/order.js";
|
|
export function formatCartGoods(goods) {
|
|
return {
|
|
...goods,
|
|
// 商品单价(会员价或普通价)
|
|
salePrice: goods.salePrice || goods.price,
|
|
number: goods.number || goods.num,
|
|
isGift: goods.isGift || goods.is_gift || 0,
|
|
is_gift: goods.is_gift || goods.isGift || 0,
|
|
packFee: goods.packFee || goods.packAmount,
|
|
};
|
|
}
|
|
|
|
export const useCartStore = defineStore("cart", {
|
|
state: () => {
|
|
return {
|
|
WebsocketUtil: null,
|
|
//商品列表
|
|
goodsList: [],
|
|
//购物车列表
|
|
cartList: [],
|
|
//历史订单购物车数据
|
|
oldCartList: [],
|
|
//商品map
|
|
goodsMap: {},
|
|
//订单数据
|
|
order: null,
|
|
// 台桌
|
|
table: null,
|
|
//选择的优惠券
|
|
selCoupon: [],
|
|
//活动列表
|
|
activityList: [],
|
|
// 商家减免,初始配置:默认无减免(固定金额 0 元)
|
|
merchantReductionConfig: {
|
|
type: "fixed_amount",
|
|
fixedAmount: 0,
|
|
},
|
|
//积分规则
|
|
pointDeductionRule: {
|
|
pointsPerYuan: 0,
|
|
maxDeductionAmount: Infinity,
|
|
},
|
|
//使用积分数量
|
|
userPoints: 0,
|
|
//新客立减
|
|
newUserDiscount: 0,
|
|
//满减活动
|
|
fullReductionActivities: [],
|
|
// 商家霸王餐配置
|
|
freeDineConfig: null,
|
|
//店铺信息
|
|
shopInfo: {
|
|
tableFee: 0,
|
|
isTableFee: 0,
|
|
isMemberPrice: 0,
|
|
},
|
|
//店铺用户
|
|
shopUserInfo: {},
|
|
//限时折扣
|
|
limitTimeDiscount: null,
|
|
//当前点餐类型
|
|
currentDinnerType: "dine-in",
|
|
//就餐人数
|
|
personCount: 0, //就餐人数
|
|
};
|
|
},
|
|
getters: {
|
|
//餐费费
|
|
seatFeeConfig: (state) => {
|
|
console.log(state.shopInfo);
|
|
return {
|
|
pricePerPerson: state.shopInfo.tableFee,
|
|
personCount: state.personCount, //就餐人数
|
|
isEnabled: !state.shopInfo.isTableFee,
|
|
};
|
|
},
|
|
// 用户是否是会员
|
|
userIsVip: (state) => {
|
|
return state.shopUserInfo && state.shopUserInfo.isVip == 1;
|
|
},
|
|
//是否使用会员价
|
|
useVipPrice: (state) => {
|
|
if (state.shopInfo.isMemberPrice == 1 && state.userIsVip) {
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
orderExtraConfig: (state) => {
|
|
return {
|
|
// 引用扩展后的商家减免配置
|
|
merchantReduction: state.merchantReductionConfig,
|
|
additionalFee: 0,
|
|
pointDeductionRule: state.pointDeductionRule,
|
|
seatFeeConfig: state.seatFeeConfig,
|
|
currentStoreId: "",
|
|
userPoints: state.userPoints,
|
|
isMember: state.useVipPrice,
|
|
memberDiscountRate: 1,
|
|
newUserDiscount: state.newUserDiscount,
|
|
fullReductionActivities: state.fullReductionActivities,
|
|
currentDinnerType: state.currentDinnerType,
|
|
isFreeDine: false, //霸王餐
|
|
freeDineConfig: state.freeDineConfig,
|
|
limitTimeDiscount: state.limitTimeDiscount,
|
|
shopUserInfo: state.shopUserInfo,
|
|
};
|
|
},
|
|
//全部购物车数据
|
|
allCartList: (state) => {
|
|
// return [...state.oldCartList, ...state.cartList];
|
|
return state.oldCartList;
|
|
},
|
|
// 订单费用汇总
|
|
orderCostSummary: (state) => {
|
|
const costSummary =
|
|
yskUtils.OrderPriceCalculator.calculateOrderCostSummary(
|
|
state.allCartList,
|
|
state.currentDinnerType,
|
|
state.selCoupon,
|
|
state.activityList,
|
|
state.orderExtraConfig,
|
|
{},
|
|
new Date()
|
|
);
|
|
console.log(" 订单费用汇总", costSummary);
|
|
return costSummary;
|
|
},
|
|
},
|
|
actions: {
|
|
setSelCoupon(coupon) {
|
|
this.selCoupon = coupon;
|
|
},
|
|
/**
|
|
* 发送消息
|
|
*/
|
|
sendMessage(message, isAutoAppendDefaultValue = true) {
|
|
if (!message) {
|
|
return;
|
|
}
|
|
if (!this.WebsocketUtil) {
|
|
return uni.showToast({
|
|
title: "socket未连接",
|
|
icon: "none",
|
|
});
|
|
}
|
|
if (!this.table || !this.table.tableCode) {
|
|
return uni.showToast({
|
|
title: "无台桌码",
|
|
icon: "none",
|
|
});
|
|
}
|
|
if (isAutoAppendDefaultValue) {
|
|
this.WebsocketUtil.send(
|
|
JSON.stringify({
|
|
table_code: this.table.tableCode,
|
|
shop_id: this.shopInfo.id || uni.getStorageSync("shopInfo").id,
|
|
account: uni.getStorageSync("tokenInfo").loginId,
|
|
...message,
|
|
type: "pad",
|
|
})
|
|
);
|
|
} else {
|
|
this.WebsocketUtil.send(
|
|
JSON.stringify({
|
|
...message,
|
|
})
|
|
);
|
|
}
|
|
},
|
|
//socket收到消息处理
|
|
onMessage(message) {
|
|
if (!message) {
|
|
return;
|
|
}
|
|
let msg = JSON.parse(message);
|
|
// console.log("socket收到消息", msg);
|
|
let cartItem;
|
|
if (msg.msg_id) {
|
|
this.WebsocketUtil.send(
|
|
JSON.stringify({
|
|
type: "receipt",
|
|
msg_id: msg.msg_id,
|
|
})
|
|
);
|
|
}
|
|
if (msg.status == 0&&msg.type!="time_discount") {
|
|
uni.showToast({
|
|
title: "添加失败",
|
|
icon: "none",
|
|
});
|
|
return false;
|
|
}
|
|
|
|
console.log("socket收到消息", msg);
|
|
|
|
switch (msg.operate_type) {
|
|
case "pad_init":
|
|
this.cartList = msg.data;
|
|
this.limitTimeDiscount = msg.time_dis_info;
|
|
this.getOrder();
|
|
break;
|
|
case "pad_add":
|
|
case "add":
|
|
break;
|
|
case "pad_edit":
|
|
break;
|
|
|
|
case "bulk_edit":
|
|
this.sendMessage({
|
|
operate_type: "init",
|
|
});
|
|
this.getOrder();
|
|
break;
|
|
case "edit":
|
|
break;
|
|
case "pad_del":
|
|
break;
|
|
case "del":
|
|
break;
|
|
case "pad_cleanup":
|
|
case "cleanup":
|
|
this.goodsList=[]
|
|
break;
|
|
case "product_update":
|
|
break;
|
|
case "batch":
|
|
break;
|
|
}
|
|
},
|
|
/**
|
|
* socket通知购物车商品数量修改处理
|
|
*/
|
|
cartControls(cartItem, type) {
|
|
if (!this.table) {
|
|
this.table = { tableCode: cartItem.table_code };
|
|
}
|
|
let cartIndex = 0;
|
|
|
|
this.cartList.map((item, index) => {
|
|
if (item.id == cartItem.id) {
|
|
cartIndex = index;
|
|
}
|
|
});
|
|
|
|
if (type == "del") {
|
|
this.cartList.splice(cartIndex, 1);
|
|
return;
|
|
}
|
|
if (type == "add") {
|
|
this.cartList.push({ ...cartItem, packNumber: cartItem.pack_number });
|
|
}
|
|
if (type == "edit") {
|
|
this.cartList[cartIndex].number = cartItem.number;
|
|
this.cartList[cartIndex].pack_number = cartItem.pack_number;
|
|
this.cartList[cartIndex].packNumber = cartItem.pack_number;
|
|
}
|
|
},
|
|
/**
|
|
*
|
|
* @returns 历史订单数据
|
|
*/
|
|
async getOrder() {
|
|
console.log("获取历史订单数据");
|
|
const res = await orderApi.getOrderById({ orderId: this.order.id });
|
|
if (res) {
|
|
this.setOrder(res);
|
|
return res;
|
|
}
|
|
},
|
|
/**
|
|
* 设置历史订单数据,给商品列表赋值
|
|
*/
|
|
setOrder(order) {
|
|
this.order = order;
|
|
this.oldCartList = Object.values(order.detailMap).reduce((pre, cur) => {
|
|
pre.push(
|
|
...cur.map((v) => {
|
|
const goods = formatCartGoods(v);
|
|
return { ...goods, isHistory: true };
|
|
})
|
|
);
|
|
return pre;
|
|
}, []);
|
|
},
|
|
},
|
|
unistorage: true, // 开启后对 state 的数据读写都将持久化
|
|
});
|