首次提交
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
/**
|
||||
* 工具类导出文件
|
||||
* 统一导出所有工具类,方便在项目中使用
|
||||
*/
|
||||
|
||||
// 网络管理
|
||||
export { default as networkManager } from './network-manager.js';
|
||||
export { NetworkManager } from './network-manager.js';
|
||||
|
||||
// WebSocket客户端
|
||||
export { default as wsClient } from './websocket-client.js';
|
||||
export { WebSocketClient } from './websocket-client.js';
|
||||
|
||||
// 设备连接管理
|
||||
export { default as deviceConnectionManager } from './device-connection-manager.js';
|
||||
export { DeviceConnectionManager } from './device-connection-manager.js';
|
||||
|
||||
// 通知管理
|
||||
export { default as notificationManager } from './notification-manager.js';
|
||||
export { NotificationManager } from './notification-manager.js';
|
||||
|
||||
// 日志管理
|
||||
export { default as logger, log } from './logger.js';
|
||||
export { Logger } from './logger.js';
|
||||
|
||||
// 系统检测
|
||||
export {
|
||||
checkNetworkConnectionState,
|
||||
checkSystemHostName,
|
||||
checkSystemFirewallStatus,
|
||||
checkCoreProgramFiles,
|
||||
checkSystemCertificate,
|
||||
checkSystemTestMode,
|
||||
checkSystemDriver,
|
||||
checkSystemAdminRights,
|
||||
checkPortService,
|
||||
performSystemCheck
|
||||
} from './system-check.js';
|
||||
|
||||
// 重新导出现有的工具
|
||||
export { default as request } from './request.js';
|
||||
export { validateForm } from './validation.js';
|
||||
export { encrypt, decrypt } from './crypto.js';
|
||||
export { getDeviceMacAddress } from './device.js';
|
||||
|
||||
// 工具函数集合
|
||||
export const utils = {
|
||||
// 延迟函数
|
||||
delay: (ms) => new Promise(resolve => setTimeout(resolve, ms)),
|
||||
|
||||
// 防抖函数
|
||||
debounce: (func, wait) => {
|
||||
let timeout;
|
||||
return function executedFunction(...args) {
|
||||
const later = () => {
|
||||
clearTimeout(timeout);
|
||||
func(...args);
|
||||
};
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
};
|
||||
},
|
||||
|
||||
// 节流函数
|
||||
throttle: (func, limit) => {
|
||||
let inThrottle;
|
||||
return function() {
|
||||
const args = arguments;
|
||||
const context = this;
|
||||
if (!inThrottle) {
|
||||
func.apply(context, args);
|
||||
inThrottle = true;
|
||||
setTimeout(() => inThrottle = false, limit);
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
// 深拷贝
|
||||
deepClone: (obj) => {
|
||||
if (obj === null || typeof obj !== 'object') return obj;
|
||||
if (obj instanceof Date) return new Date(obj.getTime());
|
||||
if (obj instanceof Array) return obj.map(item => utils.deepClone(item));
|
||||
if (typeof obj === 'object') {
|
||||
const clonedObj = {};
|
||||
for (const key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
clonedObj[key] = utils.deepClone(obj[key]);
|
||||
}
|
||||
}
|
||||
return clonedObj;
|
||||
}
|
||||
},
|
||||
|
||||
// 生成唯一ID
|
||||
generateId: (prefix = '') => {
|
||||
const timestamp = Date.now().toString(36);
|
||||
const random = Math.random().toString(36).substr(2, 9);
|
||||
return prefix ? `${prefix}_${timestamp}_${random}` : `${timestamp}_${random}`;
|
||||
},
|
||||
|
||||
// 格式化文件大小
|
||||
formatFileSize: (bytes) => {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
const k = 1024;
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||
},
|
||||
|
||||
// 格式化时间
|
||||
formatDuration: (ms) => {
|
||||
const seconds = Math.floor(ms / 1000);
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
const hours = Math.floor(minutes / 60);
|
||||
|
||||
if (hours > 0) {
|
||||
return `${hours}小时${minutes % 60}分钟`;
|
||||
} else if (minutes > 0) {
|
||||
return `${minutes}分钟${seconds % 60}秒`;
|
||||
} else {
|
||||
return `${seconds}秒`;
|
||||
}
|
||||
},
|
||||
|
||||
// 判断是否为移动设备
|
||||
isMobile: () => {
|
||||
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
||||
navigator.userAgent
|
||||
);
|
||||
},
|
||||
|
||||
// 判断是否为Electron环境
|
||||
isElectron: () => {
|
||||
return typeof window !== 'undefined' &&
|
||||
window.process &&
|
||||
window.process.type === 'renderer';
|
||||
},
|
||||
|
||||
// 安全地解析JSON
|
||||
safeParseJSON: (str, defaultValue = {}) => {
|
||||
try {
|
||||
return JSON.parse(str);
|
||||
} catch {
|
||||
return defaultValue;
|
||||
}
|
||||
},
|
||||
|
||||
// 获取URL参数
|
||||
getQueryParam: (name) => {
|
||||
const url = new URL(window.location.href);
|
||||
return url.searchParams.get(name);
|
||||
},
|
||||
|
||||
// 设置URL参数
|
||||
setQueryParam: (name, value) => {
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.set(name, value);
|
||||
window.history.replaceState({}, '', url);
|
||||
},
|
||||
|
||||
// 下载文件
|
||||
downloadFile: (content, filename, type = 'text/plain') => {
|
||||
const blob = new Blob([content], { type });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
};
|
||||
|
||||
// 默认导出所有工具
|
||||
export default {
|
||||
networkManager,
|
||||
wsClient,
|
||||
deviceConnectionManager,
|
||||
notificationManager,
|
||||
logger,
|
||||
log,
|
||||
...utils
|
||||
};
|
||||
Reference in New Issue
Block a user