修改websocket为全局只有一个
This commit is contained in:
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