306 lines
8.6 KiB
JavaScript
306 lines
8.6 KiB
JavaScript
"use strict";
|
||
const electron = require("electron");
|
||
const path = require("path");
|
||
const fs = require("fs");
|
||
const Store = require("electron-store");
|
||
let store = null;
|
||
function initStore() {
|
||
if (store) {
|
||
console.warn("Store 已经初始化过了");
|
||
return;
|
||
}
|
||
store = new Store({
|
||
// 存储文件名
|
||
name: "asset-pro-config",
|
||
// 默认值
|
||
defaults: {
|
||
settings: {
|
||
count: 0,
|
||
theme: "system"
|
||
},
|
||
windowBounds: {
|
||
width: 1600,
|
||
height: 900
|
||
}
|
||
},
|
||
// Schema 验证(可选但推荐)
|
||
schema: {
|
||
settings: {
|
||
type: "object",
|
||
properties: {
|
||
count: { type: "number", default: 0 },
|
||
theme: {
|
||
type: "string",
|
||
enum: ["light", "dark", "system"],
|
||
default: "system"
|
||
}
|
||
},
|
||
default: {}
|
||
},
|
||
windowBounds: {
|
||
type: "object",
|
||
properties: {
|
||
width: { type: "number", default: 1600 },
|
||
height: { type: "number", default: 900 },
|
||
x: { type: "number" },
|
||
y: { type: "number" }
|
||
},
|
||
default: {}
|
||
}
|
||
}
|
||
});
|
||
console.log("Store 初始化完成,存储路径:", store.path);
|
||
}
|
||
function getStore() {
|
||
if (!store) {
|
||
throw new Error("Store 未初始化,请先调用 initStore()");
|
||
}
|
||
return store;
|
||
}
|
||
function getSettings(key) {
|
||
const s = getStore();
|
||
if (key) {
|
||
return s.get(`settings.${key}`);
|
||
}
|
||
return s.get("settings");
|
||
}
|
||
function saveSettings(key, value) {
|
||
const s = getStore();
|
||
s.set(`settings.${key}`, value);
|
||
}
|
||
function getWindowBounds() {
|
||
const s = getStore();
|
||
const bounds = s.get("windowBounds");
|
||
const newBounds = {
|
||
width: 1600,
|
||
height: 900
|
||
};
|
||
if (bounds.x !== void 0 && bounds.y !== void 0) {
|
||
newBounds.x = bounds.x;
|
||
newBounds.y = bounds.y;
|
||
}
|
||
if (bounds.width !== 1600 || bounds.height !== 900) {
|
||
s.set("windowBounds", newBounds);
|
||
}
|
||
return newBounds;
|
||
}
|
||
function saveWindowBounds(bounds) {
|
||
const s = getStore();
|
||
s.set("windowBounds", bounds);
|
||
}
|
||
const IPC_CHANNELS = {
|
||
// 文件操作
|
||
SAVE_FILE: "file:save",
|
||
READ_FILE: "file:read",
|
||
// 设置操作
|
||
GET_SETTINGS: "settings:get",
|
||
SAVE_SETTINGS: "settings:save"
|
||
};
|
||
function setupIpcHandlers() {
|
||
electron.ipcMain.handle(
|
||
IPC_CHANNELS.SAVE_FILE,
|
||
async (_event, content, filename = "demo-note.txt") => {
|
||
try {
|
||
const desktopPath = electron.app.getPath("desktop");
|
||
const filePath = path.join(desktopPath, filename);
|
||
await fs.promises.writeFile(filePath, content, "utf-8");
|
||
return {
|
||
success: true,
|
||
message: `文件已保存到: ${filePath}`,
|
||
path: filePath
|
||
};
|
||
} catch (error) {
|
||
const errorMessage = error instanceof Error ? error.message : "未知错误";
|
||
console.error("保存文件失败:", errorMessage);
|
||
return {
|
||
success: false,
|
||
message: `保存失败: ${errorMessage}`,
|
||
path: null
|
||
};
|
||
}
|
||
}
|
||
);
|
||
electron.ipcMain.handle(
|
||
IPC_CHANNELS.READ_FILE,
|
||
async (_event, filename = "demo-note.txt") => {
|
||
try {
|
||
const desktopPath = electron.app.getPath("desktop");
|
||
const filePath = path.join(desktopPath, filename);
|
||
try {
|
||
await fs.promises.access(filePath);
|
||
} catch {
|
||
return {
|
||
success: false,
|
||
message: "文件不存在,请先保存一个文件",
|
||
content: null
|
||
};
|
||
}
|
||
const content = await fs.promises.readFile(filePath, "utf-8");
|
||
return {
|
||
success: true,
|
||
message: "文件读取成功",
|
||
content
|
||
};
|
||
} catch (error) {
|
||
const errorMessage = error instanceof Error ? error.message : "未知错误";
|
||
console.error("读取文件失败:", errorMessage);
|
||
return {
|
||
success: false,
|
||
message: `读取失败: ${errorMessage}`,
|
||
content: null
|
||
};
|
||
}
|
||
}
|
||
);
|
||
electron.ipcMain.handle(IPC_CHANNELS.GET_SETTINGS, async (_event, key) => {
|
||
try {
|
||
const settings = getSettings(key);
|
||
return {
|
||
success: true,
|
||
data: settings
|
||
};
|
||
} catch (error) {
|
||
const errorMessage = error instanceof Error ? error.message : "未知错误";
|
||
console.error("获取设置失败:", errorMessage);
|
||
return {
|
||
success: false,
|
||
data: null,
|
||
message: errorMessage
|
||
};
|
||
}
|
||
});
|
||
electron.ipcMain.handle(
|
||
IPC_CHANNELS.SAVE_SETTINGS,
|
||
async (_event, key, value) => {
|
||
try {
|
||
saveSettings(key, value);
|
||
return {
|
||
success: true,
|
||
message: "设置已保存"
|
||
};
|
||
} catch (error) {
|
||
const errorMessage = error instanceof Error ? error.message : "未知错误";
|
||
console.error("保存设置失败:", errorMessage);
|
||
return {
|
||
success: false,
|
||
message: `保存失败: ${errorMessage}`
|
||
};
|
||
}
|
||
}
|
||
);
|
||
}
|
||
const VITE_DEV_SERVER_URL = process.env["VITE_DEV_SERVER_URL"];
|
||
let mainWindow = null;
|
||
function createWindow() {
|
||
const bounds = getWindowBounds();
|
||
const windowOptions = {
|
||
width: bounds.width,
|
||
height: bounds.height,
|
||
webPreferences: {
|
||
// 安全配置:启用上下文隔离
|
||
contextIsolation: true,
|
||
// 安全配置:禁用 Node.js 集成
|
||
nodeIntegration: false,
|
||
// 预加载脚本路径
|
||
preload: path.join(__dirname, "preload.js"),
|
||
// 启用沙盒模式
|
||
sandbox: false
|
||
// 需要设为 false 才能使用 preload
|
||
},
|
||
// 窗口样式
|
||
title: "AssetPro",
|
||
show: false,
|
||
// 先隐藏,等准备好再显示
|
||
backgroundColor: "#ffffff",
|
||
// 无边框窗口
|
||
frame: false
|
||
};
|
||
if (bounds.x !== void 0 && bounds.y !== void 0) {
|
||
windowOptions.x = bounds.x;
|
||
windowOptions.y = bounds.y;
|
||
} else {
|
||
windowOptions.center = true;
|
||
}
|
||
mainWindow = new electron.BrowserWindow(windowOptions);
|
||
mainWindow.once("ready-to-show", () => {
|
||
mainWindow == null ? void 0 : mainWindow.show();
|
||
});
|
||
setTimeout(() => {
|
||
if (mainWindow && !mainWindow.isVisible()) {
|
||
console.log("超时后强制显示窗口");
|
||
mainWindow.show();
|
||
}
|
||
}, 3e3);
|
||
mainWindow.on("close", () => {
|
||
if (mainWindow) {
|
||
const bounds2 = mainWindow.getBounds();
|
||
saveWindowBounds(bounds2);
|
||
}
|
||
});
|
||
if (VITE_DEV_SERVER_URL) {
|
||
mainWindow.loadURL(VITE_DEV_SERVER_URL);
|
||
} else {
|
||
const fs2 = require("fs");
|
||
const possiblePaths = [
|
||
path.join(electron.app.getAppPath(), "dist-renderer", "index.html"),
|
||
// asar 内部(主要路径)
|
||
path.join(process.resourcesPath, "dist-renderer", "index.html"),
|
||
// resources/dist-renderer(如果 extraResources 存在)
|
||
path.join(process.resourcesPath, "dist", "index.html")
|
||
// resources/dist(备用)
|
||
];
|
||
console.log("尝试加载页面,检查以下路径:");
|
||
possiblePaths.forEach((p) => {
|
||
const exists = fs2.existsSync(p);
|
||
console.log(` ${exists ? "✓" : "✗"} ${p}`);
|
||
});
|
||
console.log("process.resourcesPath:", process.resourcesPath);
|
||
console.log("app.getAppPath():", electron.app.getAppPath());
|
||
let indexPath = null;
|
||
for (const testPath of possiblePaths) {
|
||
if (fs2.existsSync(testPath)) {
|
||
indexPath = testPath;
|
||
console.log("找到页面文件:", indexPath);
|
||
break;
|
||
}
|
||
}
|
||
if (!indexPath) {
|
||
console.error("无法找到 index.html 文件!");
|
||
mainWindow.loadURL("data:text/html,<h1>无法找到页面文件</h1><p>请检查控制台输出</p>");
|
||
return;
|
||
}
|
||
mainWindow.loadFile(indexPath).catch((error) => {
|
||
console.error("loadFile 失败:", error);
|
||
let fileUrl = indexPath.replace(/\\/g, "/");
|
||
if (fileUrl.match(/^[A-Z]:/)) {
|
||
fileUrl = `file:///${fileUrl}`;
|
||
} else {
|
||
fileUrl = `file://${fileUrl}`;
|
||
}
|
||
console.log("尝试使用 loadURL:", fileUrl);
|
||
mainWindow.loadURL(fileUrl).catch((urlError) => {
|
||
console.error("loadURL 也失败:", urlError);
|
||
mainWindow.loadURL("data:text/html,<h1>页面加载失败</h1><p>请查看控制台错误信息</p>");
|
||
});
|
||
});
|
||
}
|
||
mainWindow.webContents.on("did-fail-load", (event, errorCode, errorDescription) => {
|
||
console.error("页面加载失败:", errorCode, errorDescription);
|
||
});
|
||
}
|
||
electron.app.whenReady().then(() => {
|
||
initStore();
|
||
setupIpcHandlers();
|
||
createWindow();
|
||
electron.app.on("activate", () => {
|
||
if (electron.BrowserWindow.getAllWindows().length === 0) {
|
||
createWindow();
|
||
}
|
||
});
|
||
});
|
||
electron.app.on("window-all-closed", () => {
|
||
if (process.platform !== "darwin") {
|
||
electron.app.quit();
|
||
}
|
||
});
|