/** * WebSocket状态管理模块 * * @author kxmall * @date 2025-01-08 */ import riderWebSocketManager from '@/utils/websocket.js'; const state = { // 连接状态 connectionStatus: 'disconnected', // disconnected, connecting, connected, error // 在线状态 isOnline: false, // 最后连接时间 lastConnectedTime: null, // 最后断开时间 lastDisconnectedTime: null, // 重连次数 reconnectAttempts: 0, // 接收到的消息数量 receivedMessageCount: 0, // 最后接收消息时间 lastMessageTime: null, // WebSocket配置 config: { enabled: true, autoReconnect: true, enableNotifications: true } }; const mutations = { // 设置连接状态 SET_CONNECTION_STATUS(state, status) { state.connectionStatus = status; state.isOnline = status === 'connected'; if (status === 'connected') { state.lastConnectedTime = new Date(); state.reconnectAttempts = 0; } else if (status === 'disconnected') { state.lastDisconnectedTime = new Date(); } }, // 增加重连次数 INCREMENT_RECONNECT_ATTEMPTS(state) { state.reconnectAttempts++; }, // 重置重连次数 RESET_RECONNECT_ATTEMPTS(state) { state.reconnectAttempts = 0; }, // 增加接收消息数量 INCREMENT_MESSAGE_COUNT(state) { state.receivedMessageCount++; state.lastMessageTime = new Date(); }, // 更新WebSocket配置 UPDATE_CONFIG(state, config) { state.config = { ...state.config, ...config }; }, // 重置状态 RESET_STATE(state) { state.connectionStatus = 'disconnected'; state.isOnline = false; state.reconnectAttempts = 0; state.receivedMessageCount = 0; state.lastConnectedTime = null; state.lastDisconnectedTime = null; state.lastMessageTime = null; } }; const actions = { // 连接WebSocket connect({ commit, rootState }) { if (!rootState.userInfo || !rootState.userInfo.state) { console.log('用户未通过审核,无法连接WebSocket'); return false; } commit('SET_CONNECTION_STATUS', 'connecting'); try { riderWebSocketManager.connect(rootState.userInfo, rootState.baseUrl); return true; } catch (error) { console.error('连接WebSocket失败:', error); commit('SET_CONNECTION_STATUS', 'error'); return false; } }, // 断开WebSocket disconnect({ commit }) { riderWebSocketManager.disconnect(); commit('SET_CONNECTION_STATUS', 'disconnected'); }, // 重连WebSocket reconnect({ dispatch, commit }) { commit('INCREMENT_RECONNECT_ATTEMPTS'); return dispatch('connect'); }, // 发送消息 sendMessage({ state }, message) { if (state.connectionStatus !== 'connected') { console.warn('WebSocket未连接,无法发送消息'); return false; } return riderWebSocketManager.send(message); }, // 更新配置 updateConfig({ commit }, config) { commit('UPDATE_CONFIG', config); }, // 处理连接状态变化 handleStatusChange({ commit }, status) { commit('SET_CONNECTION_STATUS', status); }, // 处理接收到消息 handleMessage({ commit }, message) { commit('INCREMENT_MESSAGE_COUNT'); }, // 重置所有状态 reset({ commit }) { commit('RESET_STATE'); } }; const getters = { // 是否已连接 isConnected: state => state.connectionStatus === 'connected', // 是否正在连接 isConnecting: state => state.connectionStatus === 'connecting', // 是否有错误 hasError: state => state.connectionStatus === 'error', // 连接状态文本 statusText: state => { switch (state.connectionStatus) { case 'connected': return '已连接'; case 'connecting': return '连接中'; case 'disconnected': return '已断开'; case 'error': return '连接错误'; default: return '未知状态'; } }, // 连接时长(毫秒) connectionDuration: state => { if (state.connectionStatus === 'connected' && state.lastConnectedTime) { return Date.now() - state.lastConnectedTime.getTime(); } return 0; }, // 是否需要重连 shouldReconnect: state => { return state.config.autoReconnect && state.connectionStatus === 'disconnected' && state.reconnectAttempts < 5; } }; export default { namespaced: true, state, mutations, actions, getters };