修改websocket为全局只有一个
This commit is contained in:
@@ -63,7 +63,8 @@
|
||||
|
||||
<view class="u-m-r-30 u-flex u-flex-y-center">
|
||||
<template v-if="ordershopUserInfo.registerType=='before'">
|
||||
<up-button shape="circle" @click="toJiacai" plain :custom-style="customStyle">
|
||||
<up-button shape="circle" v-if="!listinfo.id" @click="toJiacai" plain
|
||||
:custom-style="customStyle">
|
||||
<view class="u-flex u-flex-y-center">
|
||||
<image style="width: 40rpx;height: 40rpx;" src="@/static/icon/add-goods.png"
|
||||
mode=""></image>
|
||||
@@ -106,6 +107,9 @@
|
||||
import {
|
||||
back
|
||||
} from '@/utils/uniapp.js'
|
||||
import {
|
||||
onLoad
|
||||
} from '@dcloudio/uni-app'
|
||||
|
||||
function onback() {
|
||||
closeSocket()
|
||||
@@ -148,12 +152,11 @@
|
||||
import {
|
||||
useCartsStore
|
||||
} from '@/stores/carts.js';
|
||||
import useWebSocket from '@/common/js/carts-websocket.js';
|
||||
import {
|
||||
useWebSocket
|
||||
} from '@/stores/carts-websocket.js';
|
||||
const cartStore = useCartsStore()
|
||||
console.log(cartStore.goodsIsloading);
|
||||
console.log(cartStore.goodsMap);
|
||||
async function onMessage(Message) {
|
||||
console.log('onMessage');
|
||||
cartStore.onMessage(Message, cartsSocket)
|
||||
listinfo.totalPrices = historyTotalPrices * 1 + cartStore.totalPrice * 1
|
||||
if (Message.operate_type == "init") {
|
||||
@@ -176,7 +179,6 @@
|
||||
}, 1000);
|
||||
return;
|
||||
}
|
||||
console.log(res);
|
||||
}
|
||||
}
|
||||
if (Message.operate_type == "del") {
|
||||
@@ -192,34 +194,21 @@
|
||||
.operate_type == 'product_update') {
|
||||
youhuiReset()
|
||||
}
|
||||
return Message
|
||||
}
|
||||
let cartsSocket = null
|
||||
const socketInitOptions = {
|
||||
initMessage: {
|
||||
type: 'shopping',
|
||||
operate_type: 'init',
|
||||
table_code: uni.cache.get('tableCode'),
|
||||
shop_id: uni.cache.get('shopId'),
|
||||
},
|
||||
onMessage
|
||||
cartStore.goodsInit()
|
||||
const socketInitPar = {
|
||||
type: 'shopping',
|
||||
operate_type: 'init',
|
||||
table_code: uni.cache.get('tableCode'),
|
||||
shop_id: uni.cache.get('shopId'),
|
||||
}
|
||||
watch(() => cartStore.goodsIsloading, (newValue) => {
|
||||
console.log('cartStore.goodsIsloading', cartStore.goodsIsloading);
|
||||
if (!newValue) {
|
||||
cartsSocket = useWebSocket(socketInitOptions);
|
||||
cartsSocket.allowReconnect = true;
|
||||
cartsSocket.connect();
|
||||
}
|
||||
})
|
||||
if (cartStore.goodsIsloading) {
|
||||
cartStore.goodsInit()
|
||||
} else {
|
||||
cartsSocket = useWebSocket(socketInitOptions);
|
||||
cartsSocket.allowReconnect = true;
|
||||
cartsSocket.connect();
|
||||
}
|
||||
|
||||
|
||||
function socketInit() {
|
||||
cartsSocket = useWebSocket();
|
||||
cartsSocket.connect(socketInitPar, onMessage);
|
||||
}
|
||||
const noPayStatus = {
|
||||
cancelled: "订单已取消",
|
||||
done: "订单已关闭",
|
||||
@@ -233,7 +222,11 @@
|
||||
listinfo.pointsDiscountAmount = 0
|
||||
listinfo.Productroll = 0
|
||||
uniqueIds.value = []
|
||||
orderInfoAfterRef.value.IntegralInputclose();
|
||||
try {
|
||||
orderInfoAfterRef.value?.IntegralInputclose();
|
||||
} catch {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -308,18 +301,30 @@
|
||||
let historyTotalPrices = 0;
|
||||
// * 获取订单详情接口
|
||||
const orderorderInfo = async () => {
|
||||
console.log('listinfo.id', listinfo.id);
|
||||
let res = listinfo.id ? await APIgetOrderById({
|
||||
orderId: listinfo.id
|
||||
}) : await APIhistoryOrder({
|
||||
tableCode: options.tableCode
|
||||
})
|
||||
if (options.tableCode) {
|
||||
if (res && res.tableCode) {
|
||||
socketInitPar.table_code = res.tableCode
|
||||
let tableRes = await getTableInfo({
|
||||
tableCode: options.tableCode
|
||||
})
|
||||
console.log(tableRes);
|
||||
listinfo.tableName = tableRes.name;
|
||||
} else {
|
||||
if (options.tableCode) {
|
||||
socketInitPar.table_code = options.tableCode
|
||||
let tableRes = await getTableInfo({
|
||||
tableCode: options.tableCode
|
||||
})
|
||||
console.log(tableRes);
|
||||
listinfo.tableName = tableRes.name;
|
||||
}
|
||||
}
|
||||
socketInit()
|
||||
console.log(res);
|
||||
getOrderInfoAfterCalcInit(res)
|
||||
}
|
||||
@@ -393,12 +398,9 @@
|
||||
listinfo.originAmount = Math.round(sum * 100) / 100;
|
||||
|
||||
// 打包费packFeess 计算购物车商品费用totalPrices 餐位费Seatcharge 商品卷Productroll 优惠卷coupondiscountAmount 积分listinfo.pointsDiscountAmount
|
||||
console.log('listinfo');
|
||||
console.log(listinfo);
|
||||
let sums = nowCartPackFee + historyOrderPackFee + listinfo.totalPrices + seatFee - (listinfo
|
||||
.Productroll || 0) - (listinfo.coupondiscountAmount || 0) - (listinfo
|
||||
.pointsDiscountAmount || 0);
|
||||
console.log('listinfo', listinfo);
|
||||
listinfo.totalCost = Math.round(sums * 100) / 100;
|
||||
|
||||
// 总价格
|
||||
@@ -571,51 +573,92 @@
|
||||
|
||||
}
|
||||
|
||||
function isPayBefor() {
|
||||
return ordershopUserInfo.value.registerType == 'before' ? true : false
|
||||
}
|
||||
|
||||
const istoricalorders = async () => {
|
||||
|
||||
// 购物车有数据
|
||||
if (!cartStore.isEmpty) {
|
||||
await createOrder()
|
||||
goToPay()
|
||||
}
|
||||
|
||||
// 先调用历史订单
|
||||
let APIhistoryOrderres = await APIhistoryOrder({
|
||||
tableCode: listinfo.tableCode
|
||||
function pay_lock() {
|
||||
cartsSocket.sendMessage({
|
||||
...socketInitPar,
|
||||
operate_type: 'pay_lock'
|
||||
})
|
||||
// 判断是否有历史订单
|
||||
if (APIhistoryOrderres.detailMap) {
|
||||
let combinedArray = [];
|
||||
for (const key in APIhistoryOrderres.detailMap) {
|
||||
if (APIhistoryOrderres.detailMap.hasOwnProperty(key)) {
|
||||
let subArray = APIhistoryOrderres.detailMap[key];
|
||||
combinedArray = [...combinedArray, ...subArray]
|
||||
}
|
||||
}
|
||||
// 判断支付之前是是否还有新加订单
|
||||
if (listinfo.combinedArray.length === combinedArray.length) {
|
||||
}
|
||||
|
||||
function pay_unlock() {
|
||||
clearInterval(payStatusTimer)
|
||||
cartsSocket.sendMessage({
|
||||
...socketInitPar,
|
||||
operate_type: 'pay_unlock'
|
||||
})
|
||||
}
|
||||
async function search_pay_lock() {
|
||||
cartsSocket.sendMessage({
|
||||
...socketInitPar,
|
||||
operate_type: 'search_pay_lock'
|
||||
})
|
||||
return new Promise((relove, reject) => {
|
||||
cartsSocket.socketTask.onMessage(res => {
|
||||
relove(JSON.parse(res.data))
|
||||
})
|
||||
})
|
||||
}
|
||||
let payStatusTimer = null
|
||||
let orderIsLock = true;
|
||||
const istoricalorders = async () => {
|
||||
//先付
|
||||
if (isPayBefor()) {
|
||||
console.log('isPayBefor');
|
||||
// 购物车有数据
|
||||
if (!cartStore.isEmpty) {
|
||||
const res = await createOrder()
|
||||
goToPay()
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
//后付
|
||||
|
||||
// 先调用历史订单
|
||||
let APIhistoryOrderres = await APIhistoryOrder({
|
||||
tableCode: listinfo.tableCode
|
||||
})
|
||||
// 判断是否有历史订单
|
||||
if (APIhistoryOrderres.detailMap) {
|
||||
let combinedArray = [];
|
||||
for (const key in APIhistoryOrderres.detailMap) {
|
||||
if (APIhistoryOrderres.detailMap.hasOwnProperty(key)) {
|
||||
let subArray = APIhistoryOrderres.detailMap[key];
|
||||
combinedArray = [...combinedArray, ...subArray]
|
||||
}
|
||||
}
|
||||
// 判断支付之前是是否还有新加订单
|
||||
if (listinfo.combinedArray.length === combinedArray.length) {
|
||||
goToPay()
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '你的订单已更新!',
|
||||
icon: 'none'
|
||||
})
|
||||
orderorderInfo()
|
||||
}
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '你的订单已更新!',
|
||||
title: '你的小伙伴已提交订单~',
|
||||
icon: 'none'
|
||||
})
|
||||
orderorderInfo()
|
||||
setTimeout(() => {
|
||||
orderorderInfo()
|
||||
}, 1000)
|
||||
}
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '你的小伙伴已提交订单~',
|
||||
icon: 'none'
|
||||
})
|
||||
setTimeout(() => {
|
||||
orderorderInfo()
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// * 去支付
|
||||
const goToPay = async () => {
|
||||
const goToPay = async (isCreateOrder = false) => {
|
||||
// 余额支付
|
||||
if (paymentmethod.payType == 'accountPay') {
|
||||
if (orderVIP.value.isVip == 0) {
|
||||
@@ -659,6 +702,23 @@
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const canPayRes = await search_pay_lock()
|
||||
console.log('canPayRes', canPayRes);
|
||||
const canPay = canPayRes.status == 1 ? true : false;
|
||||
console.log('canPay', canPay);
|
||||
if (!canPay) {
|
||||
uni.showToast({
|
||||
title: '有人正在付款中!',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
pay_lock()
|
||||
payStatusTimer = setInterval(() => {
|
||||
pay_lock()
|
||||
}, 1000)
|
||||
|
||||
if (orderVIP.value.freeDineConfig.enable && isBwc.value) {
|
||||
await storeMemberpay.actionspayltPayVip({
|
||||
shopId: orderVIP.value.shopId,
|
||||
@@ -670,6 +730,7 @@
|
||||
buyerRemark: '',
|
||||
seatNum: is_type.value == 0 ? listinfo.seatNum : 0, //用餐人数
|
||||
})
|
||||
|
||||
orderorderInfo()
|
||||
return false;
|
||||
} else {
|
||||
@@ -700,9 +761,11 @@
|
||||
returnUrl: ''
|
||||
})
|
||||
} catch (error) {
|
||||
pay_unlock()
|
||||
//TODO handle the exception
|
||||
}
|
||||
}
|
||||
pay_unlock()
|
||||
orderorderInfo()
|
||||
}
|
||||
|
||||
@@ -712,7 +775,7 @@
|
||||
shopUserId: orderVIP.value.id,
|
||||
orderAmount: listinfo.totalCost,
|
||||
})
|
||||
orderInfoAfterRef.value.getCalcUsablePoints(res)
|
||||
orderInfoAfterRef.value?.getCalcUsablePoints(res)
|
||||
}
|
||||
//
|
||||
const clickPointsamount = (Pointsamount) => {
|
||||
@@ -760,13 +823,16 @@
|
||||
onBeforeUnmount(() => {
|
||||
uni.$off('returnData', handleReturnData);
|
||||
clearTimeout(backtimer)
|
||||
clearInterval(payStatusTimer)
|
||||
closeSocket()
|
||||
});
|
||||
onHide(() => {
|
||||
closeSocket()
|
||||
clearInterval(payStatusTimer)
|
||||
})
|
||||
|
||||
function closeSocket() {
|
||||
|
||||
// cartsSocket.closeExistingConnection()
|
||||
}
|
||||
|
||||
@@ -777,10 +843,9 @@
|
||||
try {
|
||||
// 更新数据
|
||||
if (orderVIP.value) {
|
||||
console.log(uni.cache.get('orderVIP'))
|
||||
orderVIP.value = uni.cache.get('orderVIP')
|
||||
paymentMethodref.value.orderVIPfun(uni.cache.get('orderVIP'))
|
||||
if (listinfo.id) {
|
||||
if (listinfo.id && ordershopUserInfo.value.registerType != 'before') {
|
||||
orderorderInfo()
|
||||
}
|
||||
}
|
||||
@@ -789,28 +854,7 @@
|
||||
}
|
||||
});
|
||||
let options = {}
|
||||
// 监听页面返回事件
|
||||
onMounted(async () => {
|
||||
// 获取当前页面栈
|
||||
const pages = getCurrentPages();
|
||||
// 获取当前页面实例
|
||||
const currentPage = pages[pages.length - 1];
|
||||
// 获取页面参数
|
||||
options = currentPage.options;
|
||||
listinfo.id = options.orderId;
|
||||
if (options.shopId) {
|
||||
// 每次进来全局更新shopId
|
||||
uni.cache.set('shopId', options.shopId, 30)
|
||||
uni.$on('returnData', handleReturnData);
|
||||
}
|
||||
// * 获取会员信息
|
||||
await storeuser.actionsproductqueryProduct()
|
||||
await nextTick()
|
||||
orderVIP.value = uni.cache.get('orderVIP')
|
||||
// 积分信息
|
||||
|
||||
orderorderInfo()
|
||||
})
|
||||
// 跳转到加菜页面
|
||||
function toJiacai() {
|
||||
back()
|
||||
@@ -845,12 +889,41 @@
|
||||
'cancelled': '已取消'
|
||||
}
|
||||
const navTitle = computed(() => {
|
||||
if (!listinfo.id && !cartStore.isEmpty) {
|
||||
return '待确认'
|
||||
}
|
||||
if (cartStore.isEmpty && listinfo.id) {
|
||||
return payStatus[listinfo.status] || '待确认'
|
||||
} else {
|
||||
return '待确认'
|
||||
}
|
||||
})
|
||||
|
||||
async function init(opt) {
|
||||
Object.assign(options, opt)
|
||||
listinfo.id = options.orderId;
|
||||
console.log('init', listinfo);
|
||||
if (options.shopId) {
|
||||
// 每次进来全局更新shopId
|
||||
uni.cache.set('shopId', options.shopId, 30)
|
||||
uni.$on('returnData', handleReturnData);
|
||||
}
|
||||
// * 获取会员信息
|
||||
await storeuser.actionsproductqueryProduct()
|
||||
await nextTick()
|
||||
|
||||
orderVIP.value = uni.cache.get('orderVIP')
|
||||
if (options.tableCode) {
|
||||
socketInitPar.table_code = options.tableCode
|
||||
socketInit()
|
||||
} else {
|
||||
orderorderInfo()
|
||||
}
|
||||
|
||||
}
|
||||
onLoad((opt) => {
|
||||
init(opt)
|
||||
})
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
@@ -1270,6 +1343,7 @@
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
z-index: 999;
|
||||
|
||||
// height: 80rpx;
|
||||
.flex-between {
|
||||
|
||||
@@ -218,6 +218,14 @@
|
||||
}
|
||||
|
||||
const orderinfo = (e) => {
|
||||
if(e.status=='unpaid'){
|
||||
uni.pro.navigateTo('order/confirm-order', {
|
||||
orderId: e.id,
|
||||
shopId: e.shopId,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
uni.pro.navigateTo('order/detail', {
|
||||
orderId: e.id,
|
||||
shopId: e.shopId
|
||||
|
||||
2632
pages/product/index - 副本.vue
Normal file
2632
pages/product/index - 副本.vue
Normal file
File diff suppressed because it is too large
Load Diff
@@ -240,7 +240,7 @@
|
||||
</view>
|
||||
</view>
|
||||
<view class="Controls" v-else>
|
||||
<view class="btn" v-if="item1.cartNumber != '0'">
|
||||
<view class="btn" v-if="item1.cartNumber*1 != '0'">
|
||||
<up-icon name="minus-circle-fill" color="#E9AB7A" size="25"></up-icon>
|
||||
<view class="btnClick"
|
||||
@tap.stop="$u.throttle(() => singleclick(item1, '-'), 500)">
|
||||
@@ -418,7 +418,7 @@
|
||||
<image class="img" src="@/static/history.png" mode=""></image>
|
||||
<text>已下单菜品</text>
|
||||
</view>
|
||||
<Loading :isLoading="isLoading" />
|
||||
<Loading :isLoading="!useSocket.isConnected" />
|
||||
</view>
|
||||
</template>
|
||||
|
||||
@@ -472,7 +472,9 @@
|
||||
|
||||
// websocket
|
||||
// import useWebSocket from '@/common/js/websocket.js';
|
||||
import useWebSocket from '@/common/js/carts-websocket.js';
|
||||
import {
|
||||
useWebSocket
|
||||
} from '@/stores/carts-websocket.js';
|
||||
|
||||
// pinia管理
|
||||
import {
|
||||
@@ -1135,16 +1137,8 @@
|
||||
shop_id: uni.cache.get('shopId')
|
||||
}
|
||||
}
|
||||
const {
|
||||
isConnected,
|
||||
sendMessage,
|
||||
closeSocket: manualClose,
|
||||
receivedMessages,
|
||||
closeExistingConnection,
|
||||
onShowconnect,
|
||||
initNetworkListener
|
||||
} = useWebSocket(options);
|
||||
|
||||
const useSocket = useWebSocket();
|
||||
useSocket.connect(options.initMessage, onMessage)
|
||||
//购物车显示
|
||||
const showCart = ref(false)
|
||||
|
||||
@@ -1154,6 +1148,7 @@
|
||||
|
||||
// 更新商品数量的方法
|
||||
const updateProductQuantities = () => {
|
||||
console.log('updateProductQuantities');
|
||||
// 先将所有商品的 cartNumber 初始化为 0
|
||||
shopProductList.hots.forEach((i) => {
|
||||
i.cartNumber = 0
|
||||
@@ -1171,7 +1166,7 @@
|
||||
group.productList.forEach((product) => {
|
||||
if (product.id == cartItem.product_id && product.skuId == cartItem
|
||||
.sku_id) {
|
||||
product.cartNumber = cartItem.number
|
||||
product.cartNumber = cartItem.number || 0
|
||||
product.cartListId = cartItem.id
|
||||
}
|
||||
});
|
||||
@@ -1185,7 +1180,7 @@
|
||||
if (group.id == cartItem.product_id) {
|
||||
// 更新商品的数量
|
||||
group.cartListId = cartItem.id
|
||||
group.cartNumber = cartItem.number
|
||||
group.cartNumber = cartItem.number || 0
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1193,115 +1188,141 @@
|
||||
|
||||
//websocket产值
|
||||
const websocketsendMessage = (data) => {
|
||||
uni.$u.debounce(sendMessage(data), 500)
|
||||
console.log(data);
|
||||
uni.$u.debounce(useSocket.sendMessage(data), 500)
|
||||
}
|
||||
|
||||
// 用于记录已经处理过的消息的 msg_id
|
||||
const processedMessageIds = new Set();
|
||||
|
||||
// 监听接收到的消息变化
|
||||
watchEffect(async () => {
|
||||
if (isDataLoaded.value && receivedMessages.value) {
|
||||
const Message = receivedMessages.value
|
||||
if (Message) {
|
||||
console.log(Message.data);
|
||||
// 心跳返回 过滤
|
||||
if (Message.type == "ping_interval" || Message.msg_id == "ping_interval") {
|
||||
isLoading.value = false;
|
||||
return false
|
||||
}
|
||||
// 检查消息是否已经处理过
|
||||
if (processedMessageIds.has(Message.msg_id)) {
|
||||
return;
|
||||
}
|
||||
processedMessageIds.add(Message.msg_id);
|
||||
|
||||
// 初始化
|
||||
if (Message.operate_type == "init") {
|
||||
cartStore.carts = Message.data
|
||||
uni.hideLoading();
|
||||
isLoading.value = false;
|
||||
}
|
||||
|
||||
// 清空购物车
|
||||
if (Message.operate_type == 'cleanup') {
|
||||
cartStore.carts = []
|
||||
setTimeout(() => {
|
||||
Historicalorders()
|
||||
}, 400)
|
||||
showCart.value = false
|
||||
}
|
||||
|
||||
// 删除除购物车
|
||||
if (Message.operate_type == 'del' && Message.status == 1) {
|
||||
// 优化:使用可选链操作符避免报错
|
||||
cartStore.carts = cartStore.carts.filter(item => item.id !== Message.data?.id);
|
||||
// cartStore.carts = cartStore.carts.filter(item => item.id != Message.data.id);
|
||||
}
|
||||
|
||||
// 添加或者减少购物后返回
|
||||
if (Message.operate_type == 'add' || Message.operate_type == 'edit') {
|
||||
[Message.data].forEach((objA) => {
|
||||
const index = cartStore.carts.findIndex((objB) => objB.id == objA.id);
|
||||
if (index !== -1) {
|
||||
cartStore.carts[index] = objA;
|
||||
} else {
|
||||
cartStore.carts.push(objA);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 历史订单
|
||||
if (Message.operate_type == 'clearOrder') {
|
||||
Historicalorders()
|
||||
}
|
||||
|
||||
// 购物车数据更新从新请求
|
||||
if (Message.type == 'product' && Message.data_type == 'product_update' && Message
|
||||
.operate_type == 'product_update') {
|
||||
isDataLoaded.value = false;
|
||||
productqueryProduct()
|
||||
}
|
||||
|
||||
// 提示
|
||||
if (Message.status == 0 && Message.type != 'no_suit_num') {
|
||||
uni.showToast({
|
||||
title: Message.msg,
|
||||
icon: "none"
|
||||
})
|
||||
}
|
||||
|
||||
if (Message.type == 'no_suit_num') {
|
||||
// console.log(specifications)
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
showCancel: false,
|
||||
content: '此商品库存不足起售数量!',
|
||||
success: async (data) => {
|
||||
await websocketsendMessage({
|
||||
id: Message.id,
|
||||
type: 'shopping',
|
||||
table_code: uni.cache.get('tableCode'),
|
||||
shop_id: uni.cache.get('shopId'),
|
||||
operate_type: 'del',
|
||||
is_print: 1,
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//除去p 每次返回都回执消息
|
||||
await websocketsendMessage({
|
||||
type: 'receipt',
|
||||
msg_id: Message.msg_id
|
||||
})
|
||||
|
||||
// 初始化商品数量
|
||||
await updateProductQuantities()
|
||||
|
||||
}
|
||||
//设置商品初始选中
|
||||
function setGoodsInitSel(cartsArr) {
|
||||
console.log('setGoodsInitSel');
|
||||
const arr = (cartsArr && cartsArr.length) ? cartsArr : cartStore.carts
|
||||
if (arr.length <= 0) {
|
||||
return
|
||||
}
|
||||
})
|
||||
shopProductList.hots.map(v => {
|
||||
const item = arr.find(cart => cart.product_id == v.id)
|
||||
if (item) {
|
||||
v.cartNumber = `${item.number}`;
|
||||
}
|
||||
})
|
||||
shopProductList.productInfo.map(info => {
|
||||
info.productList.map(v => {
|
||||
const item = arr.find(cart => cart.product_id == v.id)
|
||||
if (item) {
|
||||
v.cartNumber = `${item.number}`;
|
||||
console.log(v.cartNumber);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 收到的消息变化
|
||||
async function onMessage(Message) {
|
||||
if (Message) {
|
||||
console.log('product index 收到消息',Message);
|
||||
// 心跳返回 过滤
|
||||
if (Message.type == "ping_interval" || Message.msg_id == "ping_interval") {
|
||||
isLoading.value = false;
|
||||
return false
|
||||
}
|
||||
// 检查消息是否已经处理过
|
||||
if (processedMessageIds.has(Message.msg_id)) {
|
||||
return;
|
||||
}
|
||||
processedMessageIds.add(Message.msg_id);
|
||||
|
||||
// 初始化
|
||||
if (Message.operate_type == "init") {
|
||||
cartStore.carts = Message.data
|
||||
uni.hideLoading();
|
||||
isLoading.value = false;
|
||||
// 初始化商品数量
|
||||
// setGoodsInitSel(Message.data)
|
||||
}
|
||||
|
||||
// 清空购物车
|
||||
if (Message.operate_type == 'cleanup') {
|
||||
cartStore.carts = []
|
||||
setTimeout(() => {
|
||||
Historicalorders()
|
||||
}, 400)
|
||||
showCart.value = false
|
||||
}
|
||||
|
||||
// 删除除购物车
|
||||
if (Message.operate_type == 'del' && Message.status == 1) {
|
||||
// 优化:使用可选链操作符避免报错
|
||||
cartStore.carts = cartStore.carts.filter(item => item.id !== Message.data?.id);
|
||||
// cartStore.carts = cartStore.carts.filter(item => item.id != Message.data.id);
|
||||
}
|
||||
|
||||
// 添加或者减少购物后返回
|
||||
if (Message.operate_type == 'add' || Message.operate_type == 'edit') {
|
||||
[Message.data].forEach((objA) => {
|
||||
const index = cartStore.carts.findIndex((objB) => objB.id == objA.id);
|
||||
if (index !== -1) {
|
||||
cartStore.carts[index] = objA;
|
||||
} else {
|
||||
cartStore.carts.push(objA);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 历史订单
|
||||
if (Message.operate_type == 'clearOrder') {
|
||||
Historicalorders()
|
||||
}
|
||||
|
||||
// 购物车数据更新从新请求
|
||||
if (Message.type == 'product' && Message.data_type == 'product_update' && Message
|
||||
.operate_type == 'product_update') {
|
||||
isDataLoaded.value = false;
|
||||
productqueryProduct()
|
||||
}
|
||||
|
||||
// 提示
|
||||
if (Message.status == 0 && Message.type != 'no_suit_num') {
|
||||
uni.showToast({
|
||||
title: Message.msg,
|
||||
icon: "none"
|
||||
})
|
||||
}
|
||||
|
||||
if (Message.type == 'no_suit_num') {
|
||||
// console.log(specifications)
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
showCancel: false,
|
||||
content: '此商品库存不足起售数量!',
|
||||
success: async (data) => {
|
||||
await websocketsendMessage({
|
||||
id: Message.id,
|
||||
type: 'shopping',
|
||||
table_code: uni.cache.get('tableCode'),
|
||||
shop_id: uni.cache.get('shopId'),
|
||||
operate_type: 'del',
|
||||
is_print: 1,
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//除去p 每次返回都回执消息
|
||||
await websocketsendMessage({
|
||||
type: 'receipt',
|
||||
msg_id: Message.msg_id
|
||||
})
|
||||
|
||||
// 初始化商品数量
|
||||
await updateProductQuantities()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 更新购物车数据shopProductList.hots
|
||||
const matchedProducts = computed(() => {
|
||||
@@ -1322,6 +1343,7 @@
|
||||
for (const group of Specialstop) {
|
||||
for (const product of group.productList) {
|
||||
if (product.id == cartItem.product_id) {
|
||||
console.log(cartItem);
|
||||
return {
|
||||
...product,
|
||||
cartListinfo: cartItem,
|
||||
@@ -1490,29 +1512,50 @@
|
||||
});
|
||||
|
||||
// 定义 ifcartNumber 计算属性方法 展示数量
|
||||
const ifcartNumber = computed(() => {
|
||||
return (item) => {
|
||||
// 如果 item 为空或者 cartNumber 不是字符串类型,返回 0
|
||||
if (!item || typeof item.cartNumber !== 'string') {
|
||||
return 0;
|
||||
}
|
||||
let numValue = parseFloat(item.cartNumber);
|
||||
if (isNaN(numValue)) {
|
||||
// 如果转换结果是 NaN,说明 cartNumber 不是有效的数字字符串,返回 0
|
||||
return 0;
|
||||
}
|
||||
// type string 商品类型 single-单规格商品 sku-多规格商品 package-套餐商品 weight-称重商品 coupon-团购券
|
||||
if (item.type === 'weight') {
|
||||
// 如果类型是称重重量,将值保留两位小数
|
||||
return parseFloat(numValue.toFixed(2));
|
||||
} else {
|
||||
// 如果类型是整数,将值转换为整数
|
||||
return Math.round(numValue);
|
||||
}
|
||||
// 如果类型不匹配,返回原始值
|
||||
return item.cartNumber;
|
||||
};
|
||||
})
|
||||
function ifcartNumber(item) {
|
||||
// 如果 item 为空或者 cartNumber 不是字符串类型,返回 0
|
||||
if (!item || item.cartNumber * 1 == 0) {
|
||||
return '';
|
||||
}
|
||||
let numValue = parseFloat(item.cartNumber);
|
||||
if (isNaN(numValue)) {
|
||||
// 如果转换结果是 NaN,说明 cartNumber 不是有效的数字字符串,返回 0
|
||||
return '';
|
||||
}
|
||||
// type string 商品类型 single-单规格商品 sku-多规格商品 package-套餐商品 weight-称重商品 coupon-团购券
|
||||
if (item.type === 'weight') {
|
||||
// 如果类型是称重重量,将值保留两位小数
|
||||
return parseFloat(numValue.toFixed(2));
|
||||
} else {
|
||||
// 如果类型是整数,将值转换为整数
|
||||
return Math.round(numValue);
|
||||
}
|
||||
// 如果类型不匹配,返回原始值
|
||||
return item.cartNumber;
|
||||
}
|
||||
// const ifcartNumber = computed(() => {
|
||||
// return (item) => {
|
||||
// // 如果 item 为空或者 cartNumber 不是字符串类型,返回 0
|
||||
// if (!item || typeof item.cartNumber !== 'string') {
|
||||
// return 0;
|
||||
// }
|
||||
// let numValue = parseFloat(item.cartNumber);
|
||||
// if (isNaN(numValue)) {
|
||||
// // 如果转换结果是 NaN,说明 cartNumber 不是有效的数字字符串,返回 0
|
||||
// return 0;
|
||||
// }
|
||||
// // type string 商品类型 single-单规格商品 sku-多规格商品 package-套餐商品 weight-称重商品 coupon-团购券
|
||||
// if (item.type === 'weight') {
|
||||
// // 如果类型是称重重量,将值保留两位小数
|
||||
// return parseFloat(numValue.toFixed(2));
|
||||
// } else {
|
||||
// // 如果类型是整数,将值转换为整数
|
||||
// return Math.round(numValue);
|
||||
// }
|
||||
// // 如果类型不匹配,返回原始值
|
||||
// return item.cartNumber;
|
||||
// };
|
||||
// })
|
||||
|
||||
// 计算处理后的购物车列表 // 用于筛选后的购物车数组
|
||||
const cartListFilter = computed(() => {
|
||||
@@ -1609,6 +1652,7 @@
|
||||
isDataLoaded.value = true;
|
||||
// 历史订单
|
||||
Historicalorders()
|
||||
updateProductQuantities()
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '暂无列表数据,请重新扫码',
|
||||
@@ -1628,27 +1672,28 @@
|
||||
await proxy.$onLaunched;
|
||||
})
|
||||
|
||||
onShow(async() => {
|
||||
onShow(async () => {
|
||||
// 监听页面显示和隐藏
|
||||
onShowconnect()
|
||||
useSocket.setOnMessage(onMessage)
|
||||
useSocket.onShowconnect()
|
||||
let res = await APIhistoryOrder({
|
||||
tableCode: uni.cache.get('tableCode'),
|
||||
})
|
||||
if(res){
|
||||
if (res) {
|
||||
orderinfo.value = {
|
||||
id: res.id,
|
||||
detailMap: res.detailMap,
|
||||
placeNum: res.placeNum
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
orderinfo.value = {
|
||||
id:''
|
||||
id: ''
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
onHide(() => {
|
||||
closeExistingConnection()
|
||||
|
||||
})
|
||||
|
||||
onMounted(async () => {
|
||||
@@ -1670,7 +1715,7 @@
|
||||
}
|
||||
setTimeout(() => {
|
||||
// 启动网络监听
|
||||
initNetworkListener()
|
||||
useSocket.initNetworkListener()
|
||||
getElementTop()
|
||||
}, 500)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user