订单相关修改提测

This commit is contained in:
GaoHao
2024-09-09 11:08:04 +08:00
parent c2ff506543
commit 2dbcdf4300
45 changed files with 1377 additions and 3506 deletions

View File

@@ -1,6 +1,6 @@
class webSocketUtils {
constructor(url, time, params) {
this.socketTask = null;
this.socketTask = false;
this.is_open_socket = false; //避免重复连接
this.url = url;
this.params = params ? params : null; ////是否初始化请求
@@ -13,31 +13,41 @@ class webSocketUtils {
this.heartbeatInterval = null; //检测服务器端是否还活着
this.reconnectTimeOut = null; //重连之后多久再次重连
try {
return this.connectSocketInit({
data: this.params,
type: 'connectSocketInit',
});
} catch (e) {
console.log('catch');
// console.log('catch');
this.reconnect();
}
}
// 进入这个页面的时候创建websocket连接【整个页面随时使用】
connectSocketInit(data) {
let _this = this;
this.data = data;
this.socketTask = uni.connectSocket({
uni.connectSocket({
url: this.url,
success: () => {
// console.log('正准备建立websocket中...');
header: {
'content-type': 'application/json'
},
success: (res) => {
console.log('建立websocketc成功...');
// uni.hideLoading();
// 返回实例
return this.socketTask;
this.socketTask = true;
},
fail: (res) => {
console.log(res)
}
});
this.socketTask.onOpen((res) => {
uni.onSocketOpen((res) => {
uni.hideLoading()
this.connectNum = 1;
// console.log('WebSocket连接正常');
console.log('WebSocket连接正常==',res);
if (this.params) { //是否初始化请求
this.send(this.params);
}
@@ -47,17 +57,21 @@ class webSocketUtils {
this.canReconnect = true;
this.start();
// 注:只有连接正常打开中 ,才能正常收到消息
this.socketTask.onMessage((e) => {
uni.onSocketMessage((e)=>{
// 字符串转json
let res = JSON.parse(e.data);
uni.$emit('message', res)
// 普通socket信息处理 TODO
});
},(res)=>{
console.log(res)
});
// 监听连接失败这里代码我注释掉的原因是因为如果服务器关闭后和下面的onclose方法一起发起重连操作这样会导致重复连接
uni.onSocketError((res) => {
console.log('网络断开,请检查!');
this.socketTask = null;
this.socketTask = false;
this.is_open_socket = false;
// this.Close()
this.canReconnect = true;
@@ -97,11 +111,18 @@ class webSocketUtils {
}
});
// 这里仅是事件监听【如果socket关闭了会执行】
this.socketTask.onClose(() => {
this.socketTask = null;
uni.onSocketClose((res) => {
console.log("socket关闭了")
this.socketTask = false;
clearInterval(this.heartbeatInterval);
clearInterval(this.reconnectTimeOut);
// #ifdef MP-ALIPAY
// 支付宝小程序的ws连接问题关闭连接时需关闭对于接受防止关闭失败
uni.offSocketMessage();
uni.offSocketError();
uni.offSocketOpen();
uni.offSocketClose();
// #endif
this.is_open_socket = false;
if (this.canReconnect) {
this.reconnect();
@@ -113,26 +134,41 @@ class webSocketUtils {
Close() {
this.is_open_socket = true;
this.canReconnect = false;
if (this.socketTask) {
this.socketTask.close({
// if (this.socketTask) {
uni.closeSocket({
success(res) {
console.log('手动关闭成功');
this.socketTask = false;
clearInterval(this.heartbeatInterval);
clearInterval(this.reconnectTimeOut);
// #ifdef MP-ALIPAY
// 支付宝小程序的ws连接问题关闭连接时需关闭对于接受防止关闭失败
uni.offSocketMessage();
uni.offSocketError();
uni.offSocketOpen();
uni.offSocketClose();
// #endif
},
fail: (res) => {
console.log('手动关闭失败==',res);
}
});
}
// }
}
//发送消息
send(data) {
// console.log("发送消息---------->", data);
// 注:只有连接正常打开中 ,才能正常成功发送消息
if (this.socketTask) {
this.socketTask.send({
// if (this.socketTask) {
uni.sendSocketMessage({
data: JSON.stringify(data),
async success() {
success(res) {
// console.log("消息发送成功");
},
});
}
// }
}
//开启心跳检测
start(data) {
@@ -149,6 +185,7 @@ class webSocketUtils {
//停止发送心跳
clearInterval(this.heartbeatInterval);
//如果不是人为关闭的话,进行重连
console.log(!this.is_open_socket)
if (!this.is_open_socket) {
console.log('进行重连');
this.canReconnect = true;