Compare commits
2 Commits
05edaccebb
...
6a81060e77
Author | SHA1 | Date | |
---|---|---|---|
|
6a81060e77 | ||
|
7e8c417c48 |
@ -12,11 +12,12 @@ export async function migrateDatabases() {
|
||||
}
|
||||
|
||||
try {
|
||||
// 确保数据目录存在
|
||||
// 获取数据库路径
|
||||
const systemDbPath = getSystemDbPath();
|
||||
const userDbPath = getUserDbPath();
|
||||
const dataDir = path.dirname(systemDbPath);
|
||||
|
||||
// 确保数据目录存在
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
console.log(`创建数据目录: ${dataDir}`);
|
||||
@ -27,7 +28,7 @@ export async function migrateDatabases() {
|
||||
const installedSystemDbPath = path.join(appPath, 'data', 'system.db');
|
||||
const installedUserDbPath = path.join(appPath, 'data', 'user.db');
|
||||
|
||||
// 检查并复制系统数据库文件
|
||||
// 检查并复制系统数据库文件(仅当目标文件不存在时)
|
||||
if (fs.existsSync(installedSystemDbPath) && !fs.existsSync(systemDbPath)) {
|
||||
fs.copyFileSync(installedSystemDbPath, systemDbPath);
|
||||
console.log(`复制系统数据库: ${installedSystemDbPath} -> ${systemDbPath}`);
|
||||
@ -37,7 +38,7 @@ export async function migrateDatabases() {
|
||||
console.log(`系统数据库不存在: ${systemDbPath}`);
|
||||
}
|
||||
|
||||
// 检查并复制用户数据库文件
|
||||
// 检查并复制用户数据库文件(仅当目标文件不存在时)
|
||||
if (fs.existsSync(installedUserDbPath) && !fs.existsSync(userDbPath)) {
|
||||
fs.copyFileSync(installedUserDbPath, userDbPath);
|
||||
console.log(`复制用户数据库: ${installedUserDbPath} -> ${userDbPath}`);
|
||||
|
@ -1,9 +1,35 @@
|
||||
import { app } from 'electron';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
// 获取用户数据目录
|
||||
const userDataPath = app.getPath('userData');
|
||||
const dataDir = path.join(userDataPath, 'data');
|
||||
// 检测是否为便携模式
|
||||
let isPortable = false;
|
||||
const exePath = app.getPath('exe');
|
||||
const appDir = path.dirname(exePath);
|
||||
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||||
|
||||
// 如果应用目录中存在portable.txt文件,则启用便携模式
|
||||
if (fs.existsSync(portableFlagPath)) {
|
||||
isPortable = true;
|
||||
console.log('启用便携模式');
|
||||
}
|
||||
|
||||
// 根据模式选择数据目录
|
||||
let dataDir;
|
||||
if (isPortable) {
|
||||
// 便携模式:数据存储在应用目录下的data文件夹
|
||||
dataDir = path.join(appDir, 'data');
|
||||
} else {
|
||||
// 非便携模式:数据存储在用户数据目录
|
||||
const userDataPath = app.getPath('userData');
|
||||
dataDir = path.join(userDataPath, 'data');
|
||||
}
|
||||
|
||||
// 确保数据目录存在
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
console.log(`创建数据目录: ${dataDir}`);
|
||||
}
|
||||
|
||||
// 系统数据库路径
|
||||
export function getSystemDbPath() {
|
||||
|
@ -1,5 +1,6 @@
|
||||
// 确保所有导入都使用 ES 模块语法
|
||||
import { app, BrowserWindow, ipcMain, dialog } from "electron";
|
||||
import { fs } from "fs";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { migrateDatabases } from './db/migration.js';
|
||||
@ -82,6 +83,7 @@ function createWindow() {
|
||||
preload: path.join(__dirname, "../electron/preload.js"),
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true,
|
||||
openDevTools: true,
|
||||
},
|
||||
});
|
||||
|
||||
@ -89,63 +91,62 @@ function createWindow() {
|
||||
mainWindow.maximize();
|
||||
|
||||
if (process.env.NODE_ENV === "development") {
|
||||
mainWindow.loadURL("http://localhost:5173/");
|
||||
mainWindow.webContents.openDevTools();
|
||||
} else {
|
||||
// 使用应用路径来构建更可靠的加载路径
|
||||
const appPath = app.getAppPath();
|
||||
console.log(`应用路径: ${appPath}`);
|
||||
mainWindow.loadURL("http://localhost:5173/");
|
||||
mainWindow.webContents.openDevTools();
|
||||
} else {
|
||||
// 使用应用路径来构建更可靠的加载路径
|
||||
const appPath = app.getAppPath();
|
||||
console.log(`应用路径: ${appPath}`);
|
||||
|
||||
// 尝试多种可能的路径
|
||||
const pathsToTry = [
|
||||
// 尝试1: 相对路径 (原有逻辑)
|
||||
path.join(__dirname, "../dist/index.html"),
|
||||
// 尝试2: 直接使用应用路径 + dist
|
||||
path.join(appPath, "dist/index.html"),
|
||||
// 尝试3: 应用路径的上一级 + dist
|
||||
path.join(path.dirname(appPath), "dist/index.html")
|
||||
];
|
||||
// 尝试多种可能的路径
|
||||
const pathsToTry = [
|
||||
// 尝试1: 相对路径 (原有逻辑)
|
||||
path.join(__dirname, "../dist/index.html"),
|
||||
// 尝试2: 直接使用应用路径 + dist
|
||||
path.join(appPath, "dist/index.html"),
|
||||
// 尝试3: 应用路径的上一级 + dist
|
||||
path.join(path.dirname(appPath), "dist/index.html")
|
||||
];
|
||||
|
||||
let indexPath = null;
|
||||
// 检查哪个路径存在
|
||||
for (const pathOption of pathsToTry) {
|
||||
console.log(`尝试检查路径: ${pathOption}`);
|
||||
let indexPath = null;
|
||||
// 检查哪个路径存在
|
||||
for (const pathOption of pathsToTry) {
|
||||
console.log(`尝试检查路径: ${pathOption}`);
|
||||
try {
|
||||
if (fs.existsSync(pathOption)) {
|
||||
indexPath = pathOption;
|
||||
console.log(`找到文件: ${indexPath}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (indexPath) {
|
||||
try {
|
||||
mainWindow.loadFile(indexPath);
|
||||
// 添加加载完成事件监听
|
||||
mainWindow.webContents.on('did-finish-load', () => {
|
||||
console.log('文件加载完成');
|
||||
// 显示开发者工具便于调试
|
||||
mainWindow.webContents.openDevTools();
|
||||
});
|
||||
// 添加加载失败事件监听
|
||||
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
|
||||
console.error(`文件加载失败: ${errorCode} - ${errorDescription}`);
|
||||
dialog.showErrorBox('加载失败', `无法加载应用界面: ${errorDescription}`);
|
||||
});
|
||||
// 添加页面崩溃事件监听
|
||||
mainWindow.webContents.on('crashed', (event, killed) => {
|
||||
console.error(`页面崩溃: ${killed ? '被杀死' : '意外崩溃'}`);
|
||||
dialog.showErrorBox('页面崩溃', `应用界面崩溃: ${killed ? '被杀死' : '意外崩溃'}`);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`加载文件时出错: ${error}`);
|
||||
dialog.showErrorBox('加载失败', `无法加载应用界面: ${error.message}`);
|
||||
}
|
||||
} else {
|
||||
console.error(`所有路径都不存在: ${pathsToTry.join(', ')}`);
|
||||
dialog.showErrorBox('文件不存在', `无法找到应用界面文件,请检查打包配置`);
|
||||
} catch (error) {
|
||||
console.error(`检查路径时出错: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (indexPath) {
|
||||
try {
|
||||
// 使用file://协议加载本地文件,避免路径问题
|
||||
mainWindow.loadFile(indexPath);
|
||||
// 添加加载完成事件监听
|
||||
mainWindow.webContents.on('did-finish-load', () => {
|
||||
console.log('文件加载完成');
|
||||
});
|
||||
// 添加加载失败事件监听
|
||||
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
|
||||
console.error(`文件加载失败: ${errorCode} - ${errorDescription}`);
|
||||
dialog.showErrorBox('加载失败', `无法加载应用界面: ${errorDescription}`);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`加载文件时出错: ${error}`);
|
||||
dialog.showErrorBox('加载失败', `无法加载应用界面: ${error.message}`);
|
||||
}
|
||||
} else {
|
||||
console.error(`所有路径都不存在: ${pathsToTry.join(', ')}`);
|
||||
dialog.showErrorBox('文件不存在', `无法找到应用界面文件,请检查打包配置`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 添加窗口关闭前的确认对话框
|
||||
mainWindow.on('close', (event) => {
|
||||
// 阻止默认关闭行为
|
||||
|
@ -360,14 +360,38 @@ function getAppSaveDir() {
|
||||
// 开发环境:使用项目根目录
|
||||
return path.join(process.cwd(), 'output');
|
||||
} else {
|
||||
// 生产环境:根据操作系统选择不同路径
|
||||
if (process.platform === 'darwin') { // macOS
|
||||
// 使用用户文档目录
|
||||
return path.join(app.getPath('documents'), 'ExamApp');
|
||||
} else if (process.platform === 'win32') { // Windows
|
||||
return path.dirname(app.getPath('exe'));
|
||||
} else { // Linux 等其他系统
|
||||
return app.getPath('userData');
|
||||
// 检测是否为便携模式
|
||||
const exePath = app.getPath('exe');
|
||||
const appDir = path.dirname(exePath);
|
||||
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||||
const isPortable = fs.existsSync(portableFlagPath);
|
||||
|
||||
if (isPortable) {
|
||||
// 便携模式:使用应用目录下的output文件夹
|
||||
const outputDir = path.join(appDir, 'output');
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
}
|
||||
return outputDir;
|
||||
} else {
|
||||
// 非便携模式:根据操作系统选择不同路径
|
||||
if (process.platform === 'darwin') { // macOS
|
||||
// 使用用户文档目录
|
||||
const docDir = path.join(app.getPath('documents'), 'ExamApp');
|
||||
if (!fs.existsSync(docDir)) {
|
||||
fs.mkdirSync(docDir, { recursive: true });
|
||||
}
|
||||
return docDir;
|
||||
} else if (process.platform === 'win32') { // Windows
|
||||
// 使用用户文档目录
|
||||
const docDir = path.join(app.getPath('documents'), 'ExamApp');
|
||||
if (!fs.existsSync(docDir)) {
|
||||
fs.mkdirSync(docDir, { recursive: true });
|
||||
}
|
||||
return docDir;
|
||||
} else { // Linux 等其他系统
|
||||
return app.getPath('userData');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// 将导入createWebHistory改为createWebHashHistory
|
||||
// 确保使用createWebHashHistory
|
||||
import { createRouter, createWebHashHistory } from 'vue-router'
|
||||
import WelcomeView from '@/views/WelcomeView.vue'
|
||||
import ExamingView from '@/views/user/ExamingView.vue'
|
||||
|
@ -2,10 +2,11 @@ import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import vueDevTools from 'vite-plugin-vue-devtools'
|
||||
import electron from 'vite-plugin-electron'
|
||||
// 只保留一个 fileURLToPath 导入
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
export default defineConfig({
|
||||
// 添加base配置,确保静态资源使用相对路径
|
||||
base: './',
|
||||
plugins: [
|
||||
vue(),
|
||||
vueDevTools(),
|
||||
@ -22,4 +23,14 @@ export default defineConfig({
|
||||
'@': fileURLToPath(new URL('./src', import.meta.url))
|
||||
},
|
||||
},
|
||||
build: {
|
||||
// 确保打包后的文件使用相对路径
|
||||
rollupOptions: {
|
||||
output: {
|
||||
assetFileNames: 'assets/[name]-[hash].[ext]',
|
||||
chunkFileNames: 'chunks/[name]-[hash].js',
|
||||
entryFileNames: 'entry-[name]-[hash].js',
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user