优化会员,商品,下单
This commit is contained in:
@@ -31,11 +31,13 @@ export default (params) => {
|
||||
})
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const timeoutDuration = params.timeout || 10000; // 可以通过 params 传入超时时间,默认 10 秒
|
||||
uni.request({
|
||||
url: uni.conf.baseUrl + url,
|
||||
method: method,
|
||||
header: header,
|
||||
data: data,
|
||||
timeout: timeoutDuration,
|
||||
success(response) {
|
||||
const res = response.data
|
||||
// 根据返回的状态码做出对应的操作
|
||||
@@ -43,7 +45,7 @@ export default (params) => {
|
||||
if (res.code == 200) {
|
||||
uni.hideLoading();
|
||||
uni.hideToast();
|
||||
resolve(res.data);
|
||||
resolve(res.data ?res.data: true);
|
||||
} else {
|
||||
switch (res.code) {
|
||||
case '501':
|
||||
@@ -68,15 +70,14 @@ export default (params) => {
|
||||
break;
|
||||
default:
|
||||
// 是否提示
|
||||
console.log(res)
|
||||
if (toast) {
|
||||
uni.showToast({
|
||||
title: res.message || res.msg || res.error,
|
||||
icon: "none",
|
||||
success: () => {
|
||||
setTimeout(res => {
|
||||
|
||||
}, 2000)
|
||||
reject(false);
|
||||
}, 1000)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -85,21 +86,18 @@ export default (params) => {
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
console.log(err)
|
||||
if (err.errMsg.indexOf('request:fail') !== -1) {
|
||||
uni.showToast({
|
||||
title: '网络异常',
|
||||
icon: "error",
|
||||
duration: 2000
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '未知异常',
|
||||
duration: 2000
|
||||
})
|
||||
if (err.errMsg.indexOf('timeout') !== -1) {
|
||||
if (toast) {
|
||||
uni.showToast({
|
||||
title: `请求超时,请稍后重试`,
|
||||
icon: "error",
|
||||
duration: 2000
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
reject(err);
|
||||
|
||||
},
|
||||
complete() {
|
||||
// 不管成功还是失败都会执行
|
||||
|
||||
@@ -24,10 +24,10 @@ const useWebSocket = (options = {}) => {
|
||||
const reconnectAttempts = ref(0); //重连的尝试次数
|
||||
const isNetworkConnected = ref(true); //监听当前网络连接状态
|
||||
const isManuallyClosed = ref(false); //是否是被手动关闭的
|
||||
const receivedMessages = ref([]); //储从 WebSocket 服务器接收到的消息
|
||||
const receivedMessages = ref(); //储从 WebSocket 服务器接收到的消息
|
||||
const initMessageSendAttempts = ref(0); //初始化连接多少次
|
||||
const reconnectStartTime = ref(0); //新增:记录重连开始时间
|
||||
|
||||
const isPongReceived = ref(false)
|
||||
// 关闭现有连接并清理资源
|
||||
const closeExistingConnection = () => {
|
||||
if (socketTask.value) {
|
||||
@@ -75,9 +75,8 @@ const useWebSocket = (options = {}) => {
|
||||
url: uni.conf.baseUrlwws,
|
||||
success: (res) => {
|
||||
isConnected.value = true;
|
||||
reconnectAttempts.value = 0;
|
||||
// 监听初始化成功在开启心跳
|
||||
// startHeartbeat();
|
||||
startHeartbeat();
|
||||
},
|
||||
fail: () => {
|
||||
console.error('WebSocket 连接失败,尝试重连');
|
||||
@@ -91,12 +90,12 @@ const useWebSocket = (options = {}) => {
|
||||
socketTask.value.onOpen(() => {
|
||||
// 初始化 初始购物车
|
||||
sendMessage(initMessage)
|
||||
uni.hideLoading();
|
||||
});
|
||||
socketTask.value.onMessage((res) => {
|
||||
let list = JSON.parse(res.data)
|
||||
console.log(list, 'receivedMessages.value')
|
||||
receivedMessages.value.push(list);
|
||||
if (res.msg == 'ok') {
|
||||
receivedMessages.value = JSON.parse(res.data)
|
||||
// receivedMessages.value.push(list);
|
||||
if (receivedMessages.value == 'ok') {
|
||||
console.log('心跳响应正常');
|
||||
// 心跳正常,重置重连尝试次数
|
||||
reconnectAttempts.value = 0;
|
||||
@@ -110,8 +109,15 @@ const useWebSocket = (options = {}) => {
|
||||
clearInterval(heartbeatTimer.value); // 停止心跳定时器
|
||||
clearTimeout(reconnectTimer.value); // 清除重连定时器
|
||||
if (res.code == '1006') {
|
||||
uni.showToast({
|
||||
title: '操作异常,请重新扫码',
|
||||
icon: 'none'
|
||||
});
|
||||
console.log('服务器正常关闭,停止重连');
|
||||
autoReconnect.value = false;
|
||||
setTimeout(() => {
|
||||
uni.pro.switchTab('index/index');
|
||||
}, 1000)
|
||||
return false;
|
||||
}
|
||||
if (autoReconnect.value && !isManuallyClosed.value) {
|
||||
@@ -139,14 +145,16 @@ const useWebSocket = (options = {}) => {
|
||||
uni.showToast({
|
||||
title: '网络未连接...',
|
||||
icon: 'none'
|
||||
})
|
||||
});
|
||||
setTimeout(() => {
|
||||
uni.pro.switchTab('index/index')
|
||||
}, 1000)
|
||||
uni.pro.switchTab('index/index');
|
||||
}, 1000);
|
||||
return;
|
||||
}
|
||||
heartbeatTimer.value = setInterval(() => {
|
||||
if (isConnected.value) {
|
||||
console.log('发送心跳消息');
|
||||
isPongReceived.value = false; // 每次发送心跳消息前重置标记
|
||||
socketTask.value.send({
|
||||
data: JSON.stringify({
|
||||
type: 'ping_interval'
|
||||
@@ -154,24 +162,47 @@ const useWebSocket = (options = {}) => {
|
||||
success: () => {
|
||||
console.log('心跳消息发送成功');
|
||||
const pongTimer = setTimeout(() => {
|
||||
console.error('心跳超时,未收到响应,尝试重连');
|
||||
clearInterval(heartbeatTimer.value);
|
||||
if (autoReconnect) {
|
||||
handleReconnect();
|
||||
if (!isPongReceived.value) {
|
||||
console.error('心跳超时,未收到响应,尝试重连');
|
||||
clearInterval(heartbeatTimer.value);
|
||||
if (autoReconnect.value && reconnectAttempts.value <
|
||||
maxReconnectAttempts) {
|
||||
handleReconnect();
|
||||
} else {
|
||||
console.error('重连次数达到上限,停止重连和心跳');
|
||||
clearInterval(heartbeatTimer.value);
|
||||
autoReconnect.value = false;
|
||||
uni.pro.switchTab('index/index');
|
||||
}
|
||||
}
|
||||
}, heartbeatInterval * 1.2);
|
||||
|
||||
socketTask.value.onMessage((res) => {
|
||||
if (res.msg == 'ok') {
|
||||
clearTimeout(pongTimer);
|
||||
const handlePong = (res) => {
|
||||
try {
|
||||
let data = JSON.parse(res.data);
|
||||
if (data.msg === 'ok' && data.msg_id ==
|
||||
'ping_interval') {
|
||||
isPongReceived.value = true;
|
||||
console.log('收到心跳响应,清除超时定时器');
|
||||
clearTimeout(pongTimer);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('解析心跳响应数据时出错:', error);
|
||||
}
|
||||
});
|
||||
};
|
||||
socketTask.value.onMessage(handlePong);
|
||||
},
|
||||
fail: () => {
|
||||
console.error('心跳消息发送失败,尝试重连');
|
||||
clearInterval(heartbeatTimer.value);
|
||||
if (autoReconnect) {
|
||||
if (autoReconnect.value && reconnectAttempts.value <
|
||||
maxReconnectAttempts) {
|
||||
handleReconnect();
|
||||
} else {
|
||||
console.error('重连次数达到上限,停止重连和心跳');
|
||||
clearInterval(heartbeatTimer.value);
|
||||
autoReconnect.value = false;
|
||||
uni.pro.switchTab('index/index');
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -179,6 +210,7 @@ const useWebSocket = (options = {}) => {
|
||||
}, heartbeatInterval);
|
||||
};
|
||||
|
||||
|
||||
// 手动关闭连接
|
||||
const closeSocket = () => {
|
||||
isManuallyClosed.value = true;
|
||||
@@ -217,6 +249,10 @@ const useWebSocket = (options = {}) => {
|
||||
reconnectAttempts.value++;
|
||||
const reconnectInterval = initialReconnectInterval * Math.pow(2, reconnectAttempts.value - 1);
|
||||
const randomizedInterval = reconnectInterval + Math.floor(Math.random() * 1000);
|
||||
uni.showLoading({
|
||||
title: `尝试第 ${reconnectAttempts.value} 次重连`,
|
||||
mask: true
|
||||
})
|
||||
console.log(`尝试第 ${reconnectAttempts.value} 次重连,重连间隔: ${randomizedInterval}ms...`);
|
||||
reconnectTimer.value = setTimeout(() => {
|
||||
connect();
|
||||
@@ -227,36 +263,14 @@ const useWebSocket = (options = {}) => {
|
||||
title: '重连次数达到上限,停止重连',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 发送初始化消息
|
||||
const sendInitMessage = async () => {
|
||||
if (initMessageSendAttempts.value < initMessageRetryCount) {
|
||||
initMessageSendAttempts.value++;
|
||||
await socketTask.value.send({
|
||||
data: JSON.stringify(initMessage),
|
||||
success: () => {
|
||||
console.log('初始化消息发送成功');
|
||||
initMessageSendAttempts.value = 0; // 重置尝试次数
|
||||
},
|
||||
fail: () => {
|
||||
console.log(
|
||||
`初始化消息发送失败,第 ${initMessageSendAttempts.value} 次尝试,将在 ${initMessageRetryInterval} 后重试`
|
||||
);
|
||||
setTimeout(() => {
|
||||
sendInitMessage();
|
||||
}, initMessageRetryInterval);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.error('初始化消息发送失败,已达到最大重试次数');
|
||||
// initMessageSendAttempts.value = 0; // 重置尝试次数
|
||||
clearInterval(heartbeatTimer.value);
|
||||
autoReconnect.value = false;
|
||||
uni.pro.switchTab('index/index');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// 网络状态监听
|
||||
// / 网络状态监听
|
||||
const initNetworkListener = () => {
|
||||
uni.getSystemInfo({
|
||||
success: (res) => {
|
||||
@@ -295,10 +309,12 @@ const useWebSocket = (options = {}) => {
|
||||
});
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
initNetworkListener();
|
||||
connect();
|
||||
});
|
||||
// 页面显示,尝试连接 WebSocket
|
||||
const onShowconnect = () => {
|
||||
if (autoReconnect.value) {
|
||||
connect();
|
||||
}
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
closeSocket();
|
||||
@@ -308,7 +324,10 @@ const useWebSocket = (options = {}) => {
|
||||
isConnected,
|
||||
sendMessage,
|
||||
closeSocket,
|
||||
receivedMessages
|
||||
receivedMessages,
|
||||
closeExistingConnection,
|
||||
onShowconnect,
|
||||
initNetworkListener
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user