ai处理打包后白屏的问题

This commit is contained in:
chenqiang 2025-08-12 18:17:07 +08:00
parent 7e8c417c48
commit 6a81060e77
3 changed files with 61 additions and 49 deletions

View File

@ -1,5 +1,6 @@
// 确保所有导入都使用 ES 模块语法 // 确保所有导入都使用 ES 模块语法
import { app, BrowserWindow, ipcMain, dialog } from "electron"; import { app, BrowserWindow, ipcMain, dialog } from "electron";
import { fs } from "fs";
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import { migrateDatabases } from './db/migration.js'; import { migrateDatabases } from './db/migration.js';
@ -82,6 +83,7 @@ function createWindow() {
preload: path.join(__dirname, "../electron/preload.js"), preload: path.join(__dirname, "../electron/preload.js"),
nodeIntegration: false, nodeIntegration: false,
contextIsolation: true, contextIsolation: true,
openDevTools: true,
}, },
}); });
@ -89,63 +91,62 @@ function createWindow() {
mainWindow.maximize(); mainWindow.maximize();
if (process.env.NODE_ENV === "development") { if (process.env.NODE_ENV === "development") {
mainWindow.loadURL("http://localhost:5173/"); mainWindow.loadURL("http://localhost:5173/");
mainWindow.webContents.openDevTools(); mainWindow.webContents.openDevTools();
} else { } else {
// 使用应用路径来构建更可靠的加载路径 // 使用应用路径来构建更可靠的加载路径
const appPath = app.getAppPath(); const appPath = app.getAppPath();
console.log(`应用路径: ${appPath}`); console.log(`应用路径: ${appPath}`);
// 尝试多种可能的路径 // 尝试多种可能的路径
const pathsToTry = [ const pathsToTry = [
// 尝试1: 相对路径 (原有逻辑) // 尝试1: 相对路径 (原有逻辑)
path.join(__dirname, "../dist/index.html"), path.join(__dirname, "../dist/index.html"),
// 尝试2: 直接使用应用路径 + dist // 尝试2: 直接使用应用路径 + dist
path.join(appPath, "dist/index.html"), path.join(appPath, "dist/index.html"),
// 尝试3: 应用路径的上一级 + dist // 尝试3: 应用路径的上一级 + dist
path.join(path.dirname(appPath), "dist/index.html") path.join(path.dirname(appPath), "dist/index.html")
]; ];
let indexPath = null; let indexPath = null;
// 检查哪个路径存在 // 检查哪个路径存在
for (const pathOption of pathsToTry) { for (const pathOption of pathsToTry) {
console.log(`尝试检查路径: ${pathOption}`); console.log(`尝试检查路径: ${pathOption}`);
try {
if (fs.existsSync(pathOption)) { if (fs.existsSync(pathOption)) {
indexPath = pathOption; indexPath = pathOption;
console.log(`找到文件: ${indexPath}`); console.log(`找到文件: ${indexPath}`);
break; break;
} }
} } catch (error) {
console.error(`检查路径时出错: ${error}`);
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('文件不存在', `无法找到应用界面文件,请检查打包配置`);
} }
} }
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) => { mainWindow.on('close', (event) => {
// 阻止默认关闭行为 // 阻止默认关闭行为

View File

@ -1,4 +1,4 @@
// 将导入createWebHistory改为createWebHashHistory // 确保使用createWebHashHistory
import { createRouter, createWebHashHistory } from 'vue-router' import { createRouter, createWebHashHistory } from 'vue-router'
import WelcomeView from '@/views/WelcomeView.vue' import WelcomeView from '@/views/WelcomeView.vue'
import ExamingView from '@/views/user/ExamingView.vue' import ExamingView from '@/views/user/ExamingView.vue'

View File

@ -2,10 +2,11 @@ import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools' import vueDevTools from 'vite-plugin-vue-devtools'
import electron from 'vite-plugin-electron' import electron from 'vite-plugin-electron'
// 只保留一个 fileURLToPath 导入
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
export default defineConfig({ export default defineConfig({
// 添加base配置确保静态资源使用相对路径
base: './',
plugins: [ plugins: [
vue(), vue(),
vueDevTools(), vueDevTools(),
@ -22,4 +23,14 @@ export default defineConfig({
'@': fileURLToPath(new URL('./src', import.meta.url)) '@': 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',
}
}
}
}) })