// import { getAllList, getChatList, bindId, chatBegin } from '@/api/message' // import { Message, MessageBox } from 'element-ui' let timeouter = null let websock = null let isConnect = false // 状态JSON // 打印机状态 const PrinterStatus = { 'I': '空闲', 'B': '忙碌', 'P': '正在打印' } // 错误状态 const Error1 = { '0': '托盘移动错误', '1': '墨盒类型错误', '2': '抓取光盘时未检测到光盘光盘已用完', '3': '光盘掉落', '4': '墨水车空警告', '5': '光盘抓手错误', '6': '抓取多张光盘错误', '7': '机械臂挂钩错误或者机械臂归位错误', } const Error2 = { '0': '墨水车伺服系统错误', '1': '墨水车失速错误', '2': '墨盒数据读取错误', '3': '打印图像超出范围', '4': '环境温度超出正常使用范围', '5': '墨盒短路', '6': '型号错误', '7': '缓冲区溢出错误', } const getDefaultState = () => { return { reconnections: 0, PrinterStatus, task_list: [], printer_info: { blue_last: 33, //蓝色墨盒遗留 cover_flag: false, //是否盖盖 left_strong_cd_num: 99, //左盘仓剩余盘数量 printer_name: '', //打印机名称 printer_status: 73, //打印机状态 数字转成字符为I printer_tray: 73, //打印机托盘状态 数字转成字符为I red_last: 29, //红色墨盒遗留 rigth_strong_cd_num: 99, //右盘仓剩余盘数量 yellow_last: 27 //黄色墨盒遗留 } } } const state = getDefaultState() const mutations = { setData: (state, obj) => { state[obj.name] = obj.data } } const actions = { // 初始化websocket initWebSocket({ commit, dispatch, rootState }) { if (typeof WebSocket === 'undefined') return console.log('您的浏览器不支持websocket') if ((websock && isConnect) || !rootState.user.token) { return } console.log(process.env.VUE_APP_SOCKET_API) websock = new WebSocket(process.env.VUE_APP_SOCKET_API) websock.onmessage = function (res) { dispatch('websocketonmessage', res) } websock.onopen = function (res) { dispatch('websocketonopen', res) } websock.onerror = function (res) { dispatch('websocketonerror', res) } websock.onclose = function (res) { dispatch('websocketonclose', res) } }, // 接收消息 websocketonmessage({ commit, dispatch }, e) { const data = JSON.parse(e.data) // 转json对象 if (data.resp_name) { if (data.resp_name === 'task_list') { commit('chat/setData', { name: 'task_list', data: data.resp_info.task_list }) } else if (data.resp_name === 'printer_info') { let info = data.resp_info info.printer_status = String.fromCharCode(info.printer_status) info.printer_tray = String.fromCharCode(info.printer_tray) commit('chat/setData', { name: 'printer_info', data: info }) } } else { if (data.type === 'init') { state.pingNum = 0 clearTimeout(timeouter) dispatch('pingCheck') commit('set_client_id', data.client_id) // 初始化 bindId({ client_id: data.client_id }) .then(() => {}) .catch((err) => { alert(err.msg) // Message.error(err.msg) }) } else if (data.type === 'ping') { // 心跳回执 state.pingNum = 0 dispatch('websocketsend', { type: 'pong' }) } else { // 未知消息 console.log('未知消息: ' + JSON.stringify(data)) } } }, // 心跳检测重连 pingCheck({ state, commit, dispatch, rootState }) { if (state.pingNum < 4) { state.pingNum++ timeouter = setTimeout(() => { dispatch('pingCheck') }, 10000) } else { dispatch('closeWebsocket') } }, // 发送消息 websocketsend({ commit, dispatch }, res) { if (!websock) { // Message.info('webSocket未连接,正在尝试连接') dispatch('initWebSocket') return } websock.send(JSON.stringify(res)) }, // 链接建立之后执行的方法 websocketonopen: ({ commit, dispatch }, res) => { console.log('链接建立之后执行send方法发送数据', res) isConnect = true }, // 链接关闭之后执行的方法 websocketonclose: ({ commit, dispatch }, res) => { console.log('断开链接', res) // 关闭 if (isConnect) { isConnect = false dispatch('initWebSocket') } }, // 链接错误之后执行的方法 websocketonerror({ commit, dispatch }, res) { isConnect = false state.reconnections += 1 if (state.reconnections <= 3) { setTimeout(() => { dispatch('initWebSocket') }, 5000) } else { // state.reconnections = 0 // dispatch('initWebSocket') // MessageBox.alert('服务器启动异常,请刷新重试', '温馨提示', { // confirmButtonText: '刷新', // showCancelButton: true // }) // .then(() => { // }) // .catch(() => {}) } }, // 关闭 closeWebsocket: () => { // 关闭 if (websock) websock.close() } } export default { namespaced: true, state, mutations, actions }