Files
AlgerMusicPlayer/src/main/modules/deviceInfo.ts
T
alger 9f5473e14d fix(main): 修复 config.json 并发读写导致的 EBUSY 主进程崩溃弹窗
- 主进程 6 个独立 Store 实例合并为共享单例(window-size/lyric/server/deviceInfo/window 复用 config.ts 实例)
- initializeConfig 幂等化,修复 set-store-value IPC 监听重复注册导致的双倍写入
- 窗口/歌词窗 move、resize 保存增加 500ms 防抖,拖动时不再每秒几十次写盘,close 时冲刷落盘
- store 读写 IPC handler 与窗口状态读写加 try-catch,撞锁丢弃单次写入而非崩溃
- 全局 uncaughtException 兜底:文件锁类错误(EBUSY/EPERM 等)仅记日志,其余异常保留报错弹窗

Closes #714
2026-07-05 14:06:01 +08:00

65 lines
1.7 KiB
TypeScript

import { app } from 'electron';
import { machineIdSync } from 'node-machine-id';
import os from 'os';
import { getSharedStore } from './config';
const store = getSharedStore();
/**
* 获取设备唯一标识符
* 优先使用存储的ID,如果没有则获取机器ID并存储
*/
export function getDeviceId(): string {
let deviceId = store.get('deviceId') as string | undefined;
if (!deviceId) {
try {
// 使用node-machine-id获取设备唯一标识
deviceId = machineIdSync(true);
} catch (error) {
console.error('获取机器ID失败:', error);
// 如果获取失败,使用主机名和MAC地址组合作为备选方案
const networkInterfaces = os.networkInterfaces();
let macAddress = '';
// 尝试获取第一个非内部网络接口的MAC地址
Object.values(networkInterfaces).forEach((interfaces) => {
if (interfaces) {
interfaces.forEach((iface) => {
if (!iface.internal && !macAddress && iface.mac !== '00:00:00:00:00:00') {
macAddress = iface.mac;
}
});
}
});
deviceId = `${os.hostname()}-${macAddress}`.replace(/:/g, '');
}
// 存储设备ID
if (deviceId) {
store.set('deviceId', deviceId);
} else {
// 如果所有方法都失败,使用随机ID
deviceId = Math.random().toString(36).substring(2, 15);
store.set('deviceId', deviceId);
}
}
return deviceId;
}
/**
* 获取系统信息
*/
export function getSystemInfo() {
return {
osType: os.type(),
osVersion: os.release(),
osArch: os.arch(),
platform: process.platform,
appVersion: app.getVersion()
};
}