first commit

This commit is contained in:
PC-202306242200\Administrator
2026-03-28 23:27:25 +08:00
commit 85b89ccea7
1694 changed files with 168292 additions and 0 deletions

208
store/modules/ws.js Normal file
View File

@@ -0,0 +1,208 @@
import {
WS_URL
} from '@/env'
import store from '../../store'
const getDefaultState = () => {
return {
socketTask: null,
interval: null,
//断线重连定时器
timer: null,
//心跳间隔
timeout: 20000,
// 当前重连次数
connectNum: 1,
//链接是否打开了
isOpen: false,
network: true,
orderPreInterval: null,
orderRunInterval: null,
// 存放websocket中onMessage数据
orderPre: {
device_id: '',
pay_type: '',
is_connect: 0,
fd: '',
},
orderRunItems: [],
orderRunItem: {
order: null,
report: null
}
}
}
export default {
namespaced: true,
state: getDefaultState(),
mutations: {
setWsData(state, data) {
state.wsData = data
},
setOrderPreInterval(state, data) {
clearTimeout(state.orderPreInterval);
state.orderPreInterval = setInterval(() => {
this.dispatch('ws/WEBSOCKET_SEND', JSON.stringify(data));
}, 10000);
},
setOrderRunInterval(state, data) {
clearTimeout(state.orderRunInterval);
state.orderRunInterval = setInterval(() => {
this.dispatch('ws/WEBSOCKET_SEND', JSON.stringify(data));
}, 15000);
},
WEBSOCKET_SEND(state, data) {
state.socketTask.send({
data,
async success() {
console.log('send', data)
},
})
},
},
actions: {
reConnect({
commit,
state
}) {
if (state.connectNum < 20) {
state.timer = setTimeout(() => {
this.dispatch('ws/initWs')
}, 3000)
state.connectNum += 1;
} else if (state.connectNum < 50) {
state.timer = setTimeout(() => {
this.dispatch('ws/initWs')
}, 10000)
state.connectNum += 1;
} else {
state.timer = setTimeout(() => {
this.dispatch('ws/initWs')
}, 450000)
state.connectNum += 1;
}
},
initWs({
commit,
state
}) {
console.log(state.isOpen)
// 防止重复连接
if (state.isOpen) {
return;
};
const _this = this;
//检查网络是否可用
uni.getNetworkType({
success(result) {
console.log(result)
if (result.networkType == 'none') {
state.netWork = false;
// 网络断开后显示model
// uni.showModal({
// title: '网络错误',
// content: '请检查网络',
// showCancel: false,
// success: function(res) {
// }
// })
return;
}
let url = wsUrl();
const token = store.state.auth.token.access_token;
state.socketTask = uni.connectSocket({
url,
header: {
Authorization: token.token_type + ' ' + token.access_token,
},
complete: (e) => {
},
});
if (!state.socketTask) {
return;
};
// 监听开启
state.socketTask.onOpen(() => {
console.log('onOpen')
// 将连接状态设为已连接
state.isOpen = true;
//开启心跳
state.interval = setInterval(() => {
state.socketTask.send({
data: 'ping',
success(res) {
console.log(res)
},
fail(err) {
console.log('正在尝试重新链接第' + state
.connectNum + '次');
uni.showToast({
title: '正在尝试重新链接第' + state
.connectNum + '次',
icon: "none",
});
state.isOpen = false;
//执行重连
_this.dispatch('ws/reConnect');
},
})
}, state.timeout);
});
// 监听信息
state.socketTask.onMessage(res => {
console.log('onMessage' + res.data)
if (res.data && res.data == 'pong') {
return;
}
let data = JSON.parse(res.data);
switch (data.action) {
case 'order_pre':
state.orderPre = data.data;
break;
case 'order_run_items':
state.orderRunItems = data.data;
break;
case 'order_run_item':
state.orderRunItem = data.data;
break;
default:
break;
}
});
// 监听关闭
state.socketTask.onClose(() => {
state.isOpen = false;
state.socketTask = false;
//清除定时器
clearTimeout(state.interval);
state.interval = null;
_this.dispatch('ws/reConnect');
});
// 监听错误
state.socketTask.onError((e) => {
state.isOpen = false;
state.socketTask = false;
});
}
})
},
WEBSOCKET_SEND({
commit
}, data) {
commit('WEBSOCKET_SEND', data)
},
}
}