修改websocket为全局只有一个
This commit is contained in:
368
stores/carts-websocket.js
Normal file
368
stores/carts-websocket.js
Normal file
@@ -0,0 +1,368 @@
|
||||
import {
|
||||
defineStore
|
||||
} from 'pinia';
|
||||
import {
|
||||
ref,
|
||||
computed,
|
||||
reactive,
|
||||
onMounted,
|
||||
onBeforeUnmount,
|
||||
watchEffect
|
||||
} from 'vue';
|
||||
|
||||
|
||||
|
||||
export const useWebSocket = defineStore('socketTask', () => {
|
||||
const options = {}
|
||||
let {
|
||||
heartbeatInterval = 10000, //心跳是10秒一次
|
||||
reconnectInterval = 3000, //重新连接间隔时间的一个参数
|
||||
maxReconnectAttempts = 3, //最大重连接次数
|
||||
initialReconnectInterval = 3000, // 初始重连间隔
|
||||
initMessage,
|
||||
initMessageRetryCount = 3, // 新增:初始化消息发送重试次数
|
||||
initMessageRetryInterval = 2000, // 新增:初始化消息重试间隔
|
||||
maxReconnectDuration = Infinity,
|
||||
} = options;
|
||||
const autoReconnect = ref(true); //是否自动重新连接
|
||||
const socketTask = ref(null);
|
||||
const isConnected = ref(false); //表示是否已连接上。
|
||||
const heartbeatTimer = ref(null); //心跳定时器
|
||||
const reconnectTimer = ref(null); //重连定时器
|
||||
const reconnectAttempts = ref(0); //重连的尝试次数
|
||||
const isNetworkConnected = ref(true); //监听当前网络连接状态
|
||||
const isManuallyClosed = ref(false); //是否是被手动关闭的
|
||||
const receivedMessages = ref(); //储从 WebSocket 服务器接收到的消息
|
||||
const initMessageSendAttempts = ref(0); //初始化连接多少次
|
||||
const reconnectStartTime = ref(0); //新增:记录重连开始时间
|
||||
const isPongReceived = ref(false)
|
||||
const allowReconnect = ref(true); // 新增:控制是否允许重连
|
||||
// 关闭现有连接并清理资源
|
||||
const closeExistingConnection = () => {
|
||||
try {
|
||||
socketTask.value.close({
|
||||
success: () => {
|
||||
console.log('WebSocket 连接已关闭');
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('关闭 WebSocket 连接失败:', err);
|
||||
}
|
||||
});
|
||||
|
||||
// 清除心跳定时器
|
||||
clearInterval(heartbeatTimer.value);
|
||||
heartbeatTimer.value = null;
|
||||
|
||||
// 清除重连定时器
|
||||
clearTimeout(reconnectTimer.value);
|
||||
reconnectTimer.value = null;
|
||||
|
||||
// 标记连接已断开
|
||||
isConnected.value = false;
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
//TODO handle the exception
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
const websocketsendMessage = (data) => {
|
||||
uni.$u.debounce(sendMessage(data), 500)
|
||||
}
|
||||
|
||||
|
||||
const handlePong = (res) => {
|
||||
try {
|
||||
let data = JSON.parse(res.data);
|
||||
if (data.operate_type == "init" || (data
|
||||
.msg === 'ok' &&
|
||||
data.msg_id ==
|
||||
'ping_interval')) {
|
||||
isPongReceived.value = true;
|
||||
console.log('收到心跳响应,清除超时定时器');
|
||||
clearTimeout(pongTimer);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('解析心跳响应数据时出错:', error);
|
||||
}
|
||||
};
|
||||
|
||||
let onMessage = () => {
|
||||
|
||||
}
|
||||
function setOnMessage(onMessageBallBack){
|
||||
onMessage=onMessageBallBack
|
||||
}
|
||||
// 连接 WebSocket
|
||||
const connect = async (connectMsg, onMessageBallBack) => {
|
||||
if (!isNetworkConnected.value) {
|
||||
uni.showToast({
|
||||
title: '网络未连接...',
|
||||
icon: 'none'
|
||||
})
|
||||
setTimeout(() => {
|
||||
uni.pro.switchTab('index/index')
|
||||
}, 1000)
|
||||
console.log('网络未连接,暂不尝试连接 WebSocket');
|
||||
return;
|
||||
}
|
||||
if (connectMsg) {
|
||||
initMessage = connectMsg
|
||||
}
|
||||
if (onMessageBallBack) {
|
||||
onMessage = onMessageBallBack
|
||||
}
|
||||
if (socketTask.value && isConnected.value) {
|
||||
try {
|
||||
sendMessage(connectMsg ? connectMsg : initMessage)
|
||||
} catch (error) {
|
||||
//TODO handle the exception
|
||||
}
|
||||
return
|
||||
}
|
||||
socketTask.value = uni.connectSocket({
|
||||
url: uni.conf.baseUrlwws + '?' + Date.now(),
|
||||
success: (res) => {
|
||||
console.log('连接成功');
|
||||
isConnected.value = true;
|
||||
// 监听初始化成功在开启心跳
|
||||
startHeartbeat();
|
||||
},
|
||||
fail: () => {
|
||||
console.error('WebSocket 连接失败,尝试重连');
|
||||
if (autoReconnect.value && allowReconnect.value) {
|
||||
handleReconnect();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (socketTask.value) {
|
||||
socketTask.value.onOpen(() => {
|
||||
// 初始化 初始购物车
|
||||
sendMessage(connectMsg ? connectMsg : initMessage)
|
||||
isConnected.value = true
|
||||
});
|
||||
socketTask.value.onMessage((res) => {
|
||||
receivedMessages.value = JSON.parse(res.data)
|
||||
console.log('收到消息',receivedMessages.value)
|
||||
sendMessage({
|
||||
type: 'receipt',
|
||||
msg_id: receivedMessages.value.msg_id
|
||||
})
|
||||
if (onMessage) {
|
||||
onMessage(receivedMessages.value)
|
||||
}
|
||||
// receivedMessages.value.push(list);
|
||||
if (receivedMessages.value == 'ok' || receivedMessages.value
|
||||
.operate_type == 'init') {
|
||||
console.log('初始化正常,心跳响应正常');
|
||||
// 清除重连定时器
|
||||
clearTimeout(reconnectTimer.value);
|
||||
allowReconnect.value = false
|
||||
reconnectTimer.value = null;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
socketTask.value.onClose((res) => {
|
||||
console.log(res, 'WebSocket 连接已关闭,尝试重连');
|
||||
isConnected.value = false;
|
||||
clearInterval(heartbeatTimer.value); // 停止心跳定时器
|
||||
clearTimeout(reconnectTimer.value); // 清除重连定时器
|
||||
if (res.code == '1006' && !allowReconnect.value) {
|
||||
uni.showToast({
|
||||
title: '网络异常,请重新扫码',
|
||||
icon: 'none'
|
||||
});
|
||||
autoReconnect.value = false;
|
||||
setTimeout(() => {
|
||||
uni.pro.switchTab('index/index');
|
||||
}, 1000)
|
||||
return false;
|
||||
}
|
||||
if (autoReconnect.value && !isManuallyClosed.value) {
|
||||
handleReconnect();
|
||||
}
|
||||
});
|
||||
|
||||
socketTask.value.onError((err) => {
|
||||
console.error('WebSocket 连接发生错误:', err);
|
||||
isConnected.value = false;
|
||||
clearInterval(heartbeatTimer.value);
|
||||
if (autoReconnect.value && !isManuallyClosed.value) {
|
||||
handleReconnect();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.error('socketTask 未正确初始化');
|
||||
}
|
||||
};
|
||||
|
||||
// 启动心跳机制
|
||||
const startHeartbeat = () => {
|
||||
if (!isNetworkConnected.value) {
|
||||
console.log('网络未连接,暂停心跳');
|
||||
uni.showToast({
|
||||
title: '网络未连接...',
|
||||
icon: 'none'
|
||||
});
|
||||
setTimeout(() => {
|
||||
uni.pro.switchTab('index/index');
|
||||
}, 1000);
|
||||
return;
|
||||
}
|
||||
clearInterval(heartbeatTimer.value)
|
||||
heartbeatTimer.value = setInterval(() => {
|
||||
sendMessage({
|
||||
type: 'ping_interval',
|
||||
set: 'shopping'
|
||||
})
|
||||
}, heartbeatInterval);
|
||||
};
|
||||
|
||||
|
||||
// 手动关闭连接
|
||||
const closeSocket = () => {
|
||||
console.log('手动关闭连接');
|
||||
isManuallyClosed.value = true;
|
||||
closeExistingConnection();
|
||||
};
|
||||
|
||||
// 发送消息
|
||||
const sendMessage = (data) => {
|
||||
if (isConnected.value && data) {
|
||||
// console.log('发送消息', data);
|
||||
socketTask.value.send({
|
||||
data: JSON.stringify(data),
|
||||
success: () => {
|
||||
// console.log('消息发送成功');
|
||||
},
|
||||
fail: () => {
|
||||
// console.error('消息发送失败');
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.error('WebSocket 未连接,无法发送消息');
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// 处理重连逻辑
|
||||
const handleReconnect = () => {
|
||||
if (!isNetworkConnected.value) {
|
||||
console.log('网络未连接,暂停重连');
|
||||
return;
|
||||
}
|
||||
if (isManuallyClosed.value) {
|
||||
console.log('手动关闭连接,不进行重连');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!allowReconnect.value) {
|
||||
console.log('重连功能已关闭,不进行重连');
|
||||
return;
|
||||
}
|
||||
|
||||
if (reconnectAttempts.value < maxReconnectAttempts) {
|
||||
reconnectAttempts.value++;
|
||||
const reconnectInterval = initialReconnectInterval * Math.pow(2, reconnectAttempts
|
||||
.value - 1);
|
||||
const randomizedInterval = reconnectInterval + Math.floor(Math.random() * 1000);
|
||||
uni.showLoading({
|
||||
title: `正在努力连接..`,
|
||||
mask: true
|
||||
})
|
||||
console.log(`尝试第 ${reconnectAttempts.value} 次重连,重连间隔: ${randomizedInterval}ms...`);
|
||||
|
||||
reconnectTimer.value = setTimeout(() => {
|
||||
connect();
|
||||
}, randomizedInterval);
|
||||
} else {
|
||||
console.error('重连次数达到上限,停止重连');
|
||||
uni.showToast({
|
||||
title: '重连次数达到上限,停止重连',
|
||||
icon: 'none'
|
||||
});
|
||||
clearInterval(heartbeatTimer.value);
|
||||
autoReconnect.value = false;
|
||||
setTimeout(() => {
|
||||
uni.hideLoading();
|
||||
uni.pro.switchTab('index/index');
|
||||
}, 1000)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// / 网络状态监听
|
||||
const initNetworkListener = () => {
|
||||
uni.getSystemInfo({
|
||||
success: (res) => {
|
||||
if (res.platform !== 'devtools') {
|
||||
uni.onNetworkStatusChange((statusRes) => {
|
||||
isNetworkConnected.value = statusRes.isConnected;
|
||||
if (statusRes.isConnected && !isManuallyClosed.value) {
|
||||
console.log('网络已连接,尝试重新连接 WebSocket');
|
||||
if (!isConnected.value && autoReconnect.value) {
|
||||
connect();
|
||||
}
|
||||
} else if (!statusRes.isConnected) {
|
||||
console.log('网络已断开,暂停 WebSocket 操作');
|
||||
clearInterval(heartbeatTimer.value);
|
||||
clearTimeout(reconnectTimer.value);
|
||||
if (socketTask.value) {
|
||||
socketTask.value.close();
|
||||
isConnected.value = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('获取系统信息失败:', err);
|
||||
}
|
||||
});
|
||||
|
||||
uni.getNetworkType({
|
||||
success: (res) => {
|
||||
isNetworkConnected.value = res.networkType !== 'none';
|
||||
if (!isNetworkConnected.value) {
|
||||
console.log('初始网络未连接,暂不尝试连接 WebSocket');
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 页面显示,尝试连接 WebSocket
|
||||
const onShowconnect = () => {
|
||||
if (autoReconnect.value) {
|
||||
uni.showLoading({
|
||||
title: `尝试再次连接`,
|
||||
mask: true
|
||||
})
|
||||
connect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
// closeSocket();
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
return {
|
||||
isConnected,
|
||||
sendMessage,
|
||||
closeSocket,
|
||||
receivedMessages,
|
||||
closeExistingConnection,
|
||||
connect,
|
||||
onShowconnect,
|
||||
initNetworkListener,
|
||||
connect,
|
||||
allowReconnect,
|
||||
socketTask,setOnMessage
|
||||
};
|
||||
})
|
||||
@@ -27,7 +27,7 @@ export const useCartsStore = defineStore('cart',
|
||||
const goodsMap = reactive({})
|
||||
//获取商品数据
|
||||
async function goodsInit() {
|
||||
goodsIsloading.value=true;
|
||||
goodsIsloading.value = true;
|
||||
//获取招牌菜商品
|
||||
const hotres = await productminiApphotsquery();
|
||||
for (let product of hotres) {
|
||||
@@ -102,12 +102,10 @@ export const useCartsStore = defineStore('cart',
|
||||
return getProductDetails(v)
|
||||
}).filter(v => v)
|
||||
}
|
||||
|
||||
|
||||
|
||||
//收到socket消息时对购物车进行处理
|
||||
async function onMessage(Message) {
|
||||
console.log('Message');
|
||||
console.log(Message);
|
||||
if (Message) {
|
||||
// 心跳返回 过滤
|
||||
if (Message.type == "ping_interval" || Message.msg_id == "ping_interval") {
|
||||
@@ -184,8 +182,8 @@ export const useCartsStore = defineStore('cart',
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//购物车数据
|
||||
const carts = ref([])
|
||||
|
||||
@@ -219,13 +217,13 @@ export const useCartsStore = defineStore('cart',
|
||||
|
||||
//是否使用会员价
|
||||
const useVipPrice = computed(() => {
|
||||
const isUse=(orderVIP.value.isVip && shopInfo.value.isMemberPrice)?true:false
|
||||
console.log('useVipPrice',isUse);
|
||||
const isUse = (orderVIP.value.isVip && shopInfo.value.isMemberPrice) ? true : false
|
||||
return isUse
|
||||
})
|
||||
function currentCalcMpneyNumber(item){
|
||||
const n=item.number-(item.returnNum||0)
|
||||
return n<=0?0:n
|
||||
|
||||
function currentCalcMpneyNumber(item) {
|
||||
const n = item.number - (item.returnNum || 0)
|
||||
return n <= 0 ? 0 : n
|
||||
}
|
||||
//历史订单商品价格总和
|
||||
const oldOrderMoney = computed(() => {
|
||||
@@ -239,7 +237,7 @@ export const useCartsStore = defineStore('cart',
|
||||
const memberPrice = cur.skuData ? (cur.skuData.memberPrice || cur.skuData
|
||||
.salePrice) : 0
|
||||
const price = (discount_sale_amount || cur.salePrice || 0)
|
||||
const number =currentCalcMpneyNumber(cur)
|
||||
const number = currentCalcMpneyNumber(cur)
|
||||
return prve + (number <= 0 ? 0 : number) * (discount_sale_amount || (useVipPrice
|
||||
.value ? memberPrice : price))
|
||||
}, 0)
|
||||
@@ -252,7 +250,7 @@ export const useCartsStore = defineStore('cart',
|
||||
const money = carts.value.reduce((prve, cur) => {
|
||||
const memberPrice = cur.memberPrice || cur.salePrice
|
||||
const price = useVipPrice.value ? memberPrice : cur.salePrice;
|
||||
const curMoney = price * currentCalcMpneyNumber(cur)
|
||||
const curMoney = price * currentCalcMpneyNumber(cur)
|
||||
return prve + curMoney
|
||||
}, 0)
|
||||
return money
|
||||
@@ -260,7 +258,7 @@ export const useCartsStore = defineStore('cart',
|
||||
// 霸王餐购物车原价,不享受任何优惠
|
||||
const totalOriginPrice = computed(() => {
|
||||
const money = carts.value.reduce((prve, cur) => {
|
||||
const curMoney = cur.salePrice * currentCalcMpneyNumber(cur)
|
||||
const curMoney = cur.salePrice * currentCalcMpneyNumber(cur)
|
||||
return prve + curMoney
|
||||
}, 0)
|
||||
return money
|
||||
@@ -274,7 +272,7 @@ export const useCartsStore = defineStore('cart',
|
||||
// return pre + returnProDiscount(cur, index) * 1;
|
||||
// }, 0);
|
||||
// });
|
||||
|
||||
|
||||
|
||||
//返回打包数量(称重商品打包数量最大为1)
|
||||
function returnCartPackNumber(cur) {
|
||||
@@ -285,17 +283,17 @@ export const useCartsStore = defineStore('cart',
|
||||
pack_number = pack_number <= 0 ? 0 : pack_number
|
||||
return pack_number * 1
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//当前购物车打包费
|
||||
const totalPackFee = computed(() => {
|
||||
const money = carts.value.reduce((prve, cur) => {
|
||||
const curMoney = (cur.packFee || 0) * currentCalcMpneyNumber(cur)
|
||||
const curMoney = (cur.packFee || 0) * currentCalcMpneyNumber(cur)
|
||||
return prve + curMoney
|
||||
}, 0)
|
||||
return money
|
||||
})
|
||||
|
||||
|
||||
//打包费
|
||||
const packFee = computed(() => {
|
||||
const nowPackFee = carts.value.reduce((acc, cur) => {
|
||||
@@ -351,7 +349,7 @@ export const useCartsStore = defineStore('cart',
|
||||
console.log('isBwc');
|
||||
console.log(isBwc);
|
||||
let cart = matchedProducts.reduce((total, item) => {
|
||||
if(isBwc===true){
|
||||
if (isBwc === true) {
|
||||
return total + (parseFloat(item.price) * parseFloat(item.num - item.returnNum));
|
||||
}
|
||||
// 是否启用会员价 0否1是
|
||||
@@ -447,7 +445,8 @@ export const useCartsStore = defineStore('cart',
|
||||
totalPrice,
|
||||
totalPackFee,
|
||||
updateData,
|
||||
useVipPrice,totalOriginPrice
|
||||
useVipPrice,
|
||||
totalOriginPrice
|
||||
};
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user