首次提交
This commit is contained in:
@@ -0,0 +1,451 @@
|
||||
// Vuex状态管理
|
||||
import { createStore } from 'vuex';
|
||||
import { authAPI, userAPI, deviceAPI } from '../services/api.js';
|
||||
// src/store/index.js
|
||||
import { defineStore } from 'pinia';
|
||||
export const useStore = defineStore('main', { /* 配置 */ });
|
||||
|
||||
|
||||
// 用户模块
|
||||
const user = {
|
||||
namespaced: true,
|
||||
state: () => ({
|
||||
userInfo: null,
|
||||
companyList: [],
|
||||
currentCompany: null,
|
||||
token: localStorage.getItem('token') || null,
|
||||
loginInfo: null
|
||||
}),
|
||||
|
||||
mutations: {
|
||||
SET_USER_INFO(state, userInfo) {
|
||||
state.userInfo = userInfo;
|
||||
},
|
||||
|
||||
SET_COMPANY_LIST(state, companyList) {
|
||||
state.companyList = companyList;
|
||||
},
|
||||
|
||||
SET_CURRENT_COMPANY(state, company) {
|
||||
state.currentCompany = company;
|
||||
},
|
||||
|
||||
SET_TOKEN(state, token) {
|
||||
state.token = token;
|
||||
if (token) {
|
||||
localStorage.setItem('token', token);
|
||||
} else {
|
||||
localStorage.removeItem('token');
|
||||
}
|
||||
},
|
||||
|
||||
SET_LOGIN_INFO(state, loginInfo) {
|
||||
state.loginInfo = loginInfo;
|
||||
},
|
||||
|
||||
CLEAR_USER_DATA(state) {
|
||||
state.userInfo = null;
|
||||
state.companyList = [];
|
||||
state.currentCompany = null;
|
||||
state.token = null;
|
||||
state.loginInfo = null;
|
||||
localStorage.removeItem('token');
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
// 获取用户基本信息
|
||||
async fetchUserInfo({ commit }) {
|
||||
try {
|
||||
const response = await userAPI.getUserInfo();
|
||||
const result = response.data;
|
||||
|
||||
if (result.code === 200) {
|
||||
commit('SET_USER_INFO', result.data);
|
||||
return { success: true, data: result.data };
|
||||
} else {
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败:', error);
|
||||
return { success: false, message: '获取用户信息失败' };
|
||||
}
|
||||
},
|
||||
|
||||
// 获取用户公司列表
|
||||
async fetchCompanyList({ commit }) {
|
||||
try {
|
||||
const response = await userAPI.getCompanyList();
|
||||
const result = response.data;
|
||||
|
||||
if (result.code === 200) {
|
||||
commit('SET_COMPANY_LIST', result.data || []);
|
||||
return { success: true, data: result.data || [] };
|
||||
} else {
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取公司列表失败:', error);
|
||||
return { success: false, message: '获取公司列表失败' };
|
||||
}
|
||||
},
|
||||
|
||||
// 设置当前公司
|
||||
setCurrentCompany({ commit }, company) {
|
||||
commit('SET_CURRENT_COMPANY', company);
|
||||
},
|
||||
|
||||
// 设置token
|
||||
setToken({ commit }, token) {
|
||||
commit('SET_TOKEN', token);
|
||||
},
|
||||
|
||||
// 设置登录信息
|
||||
setLoginInfo({ commit }, loginInfo) {
|
||||
commit('SET_LOGIN_INFO', loginInfo);
|
||||
},
|
||||
|
||||
// 用户登出
|
||||
async userLogout({ commit }) {
|
||||
commit('CLEAR_USER_DATA');
|
||||
}
|
||||
},
|
||||
|
||||
getters: {
|
||||
isLoggedIn: state => !!state.token,
|
||||
userName: state => state.userInfo?.name || '',
|
||||
companyName: state => state.currentCompany?.name || ''
|
||||
}
|
||||
};
|
||||
|
||||
// VPN状态模块
|
||||
const vpn = {
|
||||
namespaced: true,
|
||||
state: () => ({
|
||||
isConnectVPN: false,
|
||||
vpnStatus: 'disconnected', // disconnected, connecting, connected, error
|
||||
vpnConfig: null,
|
||||
connectionHistory: []
|
||||
}),
|
||||
|
||||
mutations: {
|
||||
SET_VPN_STATUS(state, status) {
|
||||
state.isConnectVPN = status
|
||||
state.vpnStatus = status ? 'connected' : 'disconnected'
|
||||
},
|
||||
|
||||
SET_VPN_CONFIG(state, config) {
|
||||
state.vpnConfig = config
|
||||
},
|
||||
|
||||
ADD_CONNECTION_HISTORY(state, record) {
|
||||
state.connectionHistory.push({
|
||||
timestamp: new Date().toISOString(),
|
||||
...record
|
||||
})
|
||||
},
|
||||
|
||||
CLEAR_CONNECTION_HISTORY(state) {
|
||||
state.connectionHistory = []
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
// 设置VPN连接状态
|
||||
setVPNStatus({ commit }, status) {
|
||||
commit('SET_VPN_STATUS', status)
|
||||
|
||||
commit('ADD_CONNECTION_HISTORY', {
|
||||
action: status ? 'connected' : 'disconnected'
|
||||
})
|
||||
},
|
||||
|
||||
// 设置VPN配置
|
||||
setVPNConfig({ commit }, config) {
|
||||
commit('SET_VPN_CONFIG', config)
|
||||
},
|
||||
|
||||
// 清除连接历史
|
||||
clearConnectionHistory({ commit }) {
|
||||
commit('CLEAR_CONNECTION_HISTORY')
|
||||
}
|
||||
},
|
||||
|
||||
getters: {
|
||||
vpnStatusText: (state) => {
|
||||
const statusMap = {
|
||||
disconnected: '未连接',
|
||||
connecting: '连接中...',
|
||||
connected: '已连接',
|
||||
error: '连接失败'
|
||||
}
|
||||
return statusMap[state.vpnStatus] || '未知状态'
|
||||
},
|
||||
|
||||
isVPNConnected: (state) => state.isConnectVPN
|
||||
}
|
||||
};
|
||||
|
||||
// 设备模块
|
||||
const device = {
|
||||
namespaced: true,
|
||||
state: () => ({
|
||||
deviceList: [],
|
||||
deviceTypes: [],
|
||||
currentDevice: null,
|
||||
deviceLogs: [],
|
||||
purposeList: [],
|
||||
deviceTimeList: [],
|
||||
connectedDevices: new Map(),
|
||||
deviceStatus: {}
|
||||
}),
|
||||
|
||||
mutations: {
|
||||
SET_DEVICE_LIST(state, devices) {
|
||||
state.deviceList = devices;
|
||||
},
|
||||
|
||||
SET_DEVICE_TYPES(state, types) {
|
||||
state.deviceTypes = types;
|
||||
},
|
||||
|
||||
SET_CURRENT_DEVICE(state, device) {
|
||||
state.currentDevice = device;
|
||||
},
|
||||
|
||||
SET_DEVICE_LOGS(state, logs) {
|
||||
state.deviceLogs = logs;
|
||||
},
|
||||
|
||||
SET_PURPOSE_LIST(state, purposes) {
|
||||
state.purposeList = purposes;
|
||||
},
|
||||
|
||||
SET_DEVICE_TIME_LIST(state, timeList) {
|
||||
state.deviceTimeList = timeList;
|
||||
},
|
||||
|
||||
SET_DEVICE_STATUS(state, { deviceId, status }) {
|
||||
state.deviceStatus[deviceId] = status;
|
||||
},
|
||||
|
||||
ADD_CONNECTED_DEVICE(state, { deviceId, connectionInfo }) {
|
||||
state.connectedDevices.set(deviceId, connectionInfo);
|
||||
},
|
||||
|
||||
REMOVE_CONNECTED_DEVICE(state, deviceId) {
|
||||
state.connectedDevices.delete(deviceId);
|
||||
},
|
||||
|
||||
UPDATE_DEVICE_STATUS(state, { deviceId, status }) {
|
||||
const device = state.deviceList.find(d => d.id === deviceId);
|
||||
if (device) {
|
||||
device.status = status;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
// 获取设备列表数据
|
||||
async fetchDeviceList({ commit }, params = {}) {
|
||||
try {
|
||||
const response = await deviceAPI.getDeviceList(params);
|
||||
const result = response.data;
|
||||
|
||||
if (result.code === 200) {
|
||||
commit('SET_DEVICE_LIST', result.data || []);
|
||||
return { success: true, data: result.data || [] };
|
||||
} else {
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取设备列表失败:', error);
|
||||
return { success: false, message: '获取设备列表失败' };
|
||||
}
|
||||
},
|
||||
|
||||
// 获取设备类型列表
|
||||
async fetchDeviceTypes({ commit }, params = {}) {
|
||||
try {
|
||||
const response = await deviceAPI.getDeviceTypes(params);
|
||||
const result = response.data;
|
||||
|
||||
if (result.code === 200) {
|
||||
commit('SET_DEVICE_TYPES', result.data || []);
|
||||
return { success: true, data: result.data || [] };
|
||||
} else {
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取设备类型失败:', error);
|
||||
return { success: false, message: '获取设备类型失败' };
|
||||
}
|
||||
},
|
||||
|
||||
// 连接指定设备
|
||||
async connectToDevice({ commit }, params) {
|
||||
try {
|
||||
const response = await deviceAPI.connectDevice(params);
|
||||
const result = response.data;
|
||||
|
||||
if (result.code === 200) {
|
||||
commit('SET_DEVICE_STATUS', {
|
||||
deviceId: params.device_id,
|
||||
status: 'connected'
|
||||
});
|
||||
commit('ADD_CONNECTED_DEVICE', {
|
||||
deviceId: params.device_id,
|
||||
connectionInfo: result.data
|
||||
});
|
||||
return { success: true, data: result.data };
|
||||
} else {
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('连接设备失败:', error);
|
||||
return { success: false, message: '连接设备失败' };
|
||||
}
|
||||
},
|
||||
|
||||
// 获取设备使用日志
|
||||
async fetchDeviceLogs({ commit }, params) {
|
||||
try {
|
||||
const response = await deviceAPI.getDeviceLogs(params);
|
||||
const result = response.data;
|
||||
|
||||
if (result.code === 200) {
|
||||
commit('SET_DEVICE_LOGS', result.data || []);
|
||||
return { success: true, data: result.data || [] };
|
||||
} else {
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取设备日志失败:', error);
|
||||
return { success: false, message: '获取设备日志失败' };
|
||||
}
|
||||
},
|
||||
|
||||
// 获取设备使用用途列表
|
||||
async fetchPurposeList({ commit }, params) {
|
||||
try {
|
||||
const response = await deviceAPI.getPurposeList(params);
|
||||
const result = response.data;
|
||||
|
||||
if (result.code === 200) {
|
||||
commit('SET_PURPOSE_LIST', result.data || []);
|
||||
return { success: true, data: result.data || [] };
|
||||
} else {
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用途列表失败:', error);
|
||||
return { success: false, message: '获取用途列表失败' };
|
||||
}
|
||||
},
|
||||
|
||||
// 获取设备使用时间列表
|
||||
async fetchDeviceTimeList({ commit }, params) {
|
||||
try {
|
||||
const response = await deviceAPI.getDeviceTimeList(params);
|
||||
const result = response.data;
|
||||
|
||||
if (result.code === 200) {
|
||||
commit('SET_DEVICE_TIME_LIST', result.data || []);
|
||||
return { success: true, data: result.data || [] };
|
||||
} else {
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取设备使用时间列表失败:', error);
|
||||
return { success: false, message: '获取设备使用时间列表失败' };
|
||||
}
|
||||
},
|
||||
|
||||
// 设置当前设备
|
||||
setCurrentDevice({ commit }, device) {
|
||||
commit('SET_CURRENT_DEVICE', device);
|
||||
},
|
||||
|
||||
// 更新设备状态
|
||||
updateDeviceStatus({ commit }, { deviceId, status }) {
|
||||
commit('UPDATE_DEVICE_STATUS', { deviceId, status });
|
||||
}
|
||||
},
|
||||
|
||||
getters: {
|
||||
availableDevices: state => state.deviceList.filter(d => d.status === 'available'),
|
||||
connectedDeviceCount: state => state.connectedDevices.size,
|
||||
getDeviceById: state => id => state.deviceList.find(d => d.id === id)
|
||||
}
|
||||
};
|
||||
|
||||
// 端口模块
|
||||
const port = {
|
||||
namespaced: true,
|
||||
state: () => ({
|
||||
portList: [],
|
||||
currentPort: null,
|
||||
portStatus: {},
|
||||
isLoading: false
|
||||
}),
|
||||
|
||||
mutations: {
|
||||
SET_PORT_LIST(state, ports) {
|
||||
state.portList = ports;
|
||||
},
|
||||
|
||||
SET_CURRENT_PORT(state, port) {
|
||||
state.currentPort = port;
|
||||
},
|
||||
|
||||
SET_PORT_STATUS(state, { portId, status }) {
|
||||
state.portStatus[portId] = status;
|
||||
},
|
||||
|
||||
SET_LOADING(state, loading) {
|
||||
state.isLoading = loading;
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
// 获取端口列表
|
||||
async fetchPortList({ commit }, params = {}) {
|
||||
try {
|
||||
const response = await deviceAPI.getPortList(params);
|
||||
const result = response.data;
|
||||
|
||||
if (result.code === 200) {
|
||||
commit('SET_PORT_LIST', result.data || []);
|
||||
return { success: true, data: result.data || [] };
|
||||
} else {
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取端口列表失败:', error);
|
||||
return { success: false, message: '获取端口列表失败' };
|
||||
}
|
||||
},
|
||||
|
||||
// 设置当前端口
|
||||
setCurrentPort({ commit }, port) {
|
||||
commit('SET_CURRENT_PORT', port);
|
||||
}
|
||||
},
|
||||
|
||||
getters: {
|
||||
availablePorts: state => state.portList.filter(p => p.status === 'available'),
|
||||
occupiedPorts: state => state.portList.filter(p => p.status === 'occupied'),
|
||||
totalPorts: state => state.portList.length
|
||||
}
|
||||
};
|
||||
|
||||
// 创建store实例
|
||||
export default createStore({
|
||||
modules: {
|
||||
user,
|
||||
vpn,
|
||||
device,
|
||||
port
|
||||
},
|
||||
strict: process.env.NODE_ENV !== 'production'
|
||||
});
|
||||
Reference in New Issue
Block a user