This commit is contained in:
24kycj
2024-11-29 10:35:43 +08:00
parent f125b5cace
commit af7380352b
3 changed files with 74 additions and 52 deletions
+35 -40
View File
@@ -223,6 +223,7 @@ const getDefaultState = () => {
return {
isConnect: false,
serviceStatus: false,
workStatus: true,
reconnections: 0,
PrinterStatus,
CDName,
@@ -247,7 +248,7 @@ const mutations = {
const actions = {
// 初始化websocket
initWebSocket({ state, dispatch, rootState }) {
initWebSocket({ state, commit, dispatch, rootState }) {
if (typeof WebSocket === 'undefined') return console.log('您的浏览器不支持websocket')
if ((websock && state.isConnect) || !rootState.user.token) {
return
@@ -342,54 +343,40 @@ const actions = {
// 打印报错处理
} else if (data.resp_name === 'printer_error') {
let error = data.resp_info
let info = {
error1: '',
error2: '',
error3: ''
}
if (error.error1) {
let errors = error.error1.split('')
let errors = error.error1.toString(2).split('').reverse()
console.log(errors)
for (let i = 0; i < errors.length; i++) {
if (errors[i] == 1) info.error1 = i
if (errors[i] == 1) {
setTimeout(() => {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: Error1[i] } })
}, 500);
}
}
}
if (error.error2) {
let errors = error.error2.split('')
let errors = error.error2.toString(2).split('').reverse()
for (let i = 0; i < errors.length; i++) {
if (errors[i] == 1) info.error2 = i
if (errors[i] == 1) {
setTimeout(() => {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: Error2[i] } })
}, 500);
}
}
}
if (error.error3) {
let errors = error.error3.split('')
let errors = error.error3.toString(2).split('').reverse()
for (let i = 0; i < errors.length; i++) {
if (errors[i] == 1) info.error3 = i
if (errors[i] == 1) {
setTimeout(() => {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: Error3[i] } })
}, 500);
}
}
}
if (error.printer_error) {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: printer_errors[error.printer_error], message: '' } })
dispatch('websocketsend', {
req_name: 'next_task',
req_type: 1,
req_info: {}
}).then(() => {
}).catch(() => {
})
commit('setData', { name: 'printer_error', data: {type: 'error', confirm: printer_errors[error.printer_error], req: 'continue_task' } })
}
setTimeout(() => {
if (info.error1 || info.error1 === 0) {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: Error1[info.error1], message: '' } })
}
if (info.error2 || info.error2 === 0) {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: Error2[info.error2], message: '' } })
}
if (info.error3 || info.error3 === 0) {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: Error3[info.error3], message: '' } })
}
}, 100)
}
} else {
// 未知消息
@@ -410,10 +397,12 @@ const actions = {
// 发送消息
websocketsend({ commit, dispatch }, res) {
return new Promise((resolve, reject) => {
if (!websock || !state.isConnect || !state.serviceStatus) {
commit('setData', { name: 'printer_error', data: {type: 'error', message: '服务连接失败,正在尝试重新连接', notify: '' } })
dispatch('initWebSocket')
reject()
if (!state.serviceStatus) {
commit('setData', { name: 'printer_error', data: {type: 'warning', message: '服务连接中,请耐心等待', notify: '' } })
return
}
if (!state.isConnect) {
commit('setData', { name: 'printer_error', data: {type: 'warning', message: '请先打开服务', notify: '' } })
return
}
websock.send(JSON.stringify(res))
@@ -427,13 +416,19 @@ const actions = {
commit('setData', { name: 'serviceStatus', data: true })
},
// 链接关闭之后执行的方法
websocketonclose: ({ commit, dispatch }, res) => {
websocketonclose: ({ state, commit, dispatch }, res) => {
console.log('断开链接', res)
// 关闭
commit('setData', { name: 'isConnect', data: false })
for (let key in defaultData) {
commit('setData', { name: key, data: defaultData[key] })
}
if (state.workStatus) {
const refreshTime = parseInt(state.refreshTime)*1000
setTimeout(() => {
dispatch('initWebSocket')
}, refreshTime)
}
},
// 链接错误之后执行的方法
websocketonerror({ state, commit, dispatch }, res) {