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,
}, },
}); });
@ -110,32 +112,30 @@ function createWindow() {
// 检查哪个路径存在 // 检查哪个路径存在
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) { if (indexPath) {
try { try {
// 使用file://协议加载本地文件,避免路径问题
mainWindow.loadFile(indexPath); mainWindow.loadFile(indexPath);
// 添加加载完成事件监听 // 添加加载完成事件监听
mainWindow.webContents.on('did-finish-load', () => { mainWindow.webContents.on('did-finish-load', () => {
console.log('文件加载完成'); console.log('文件加载完成');
// 显示开发者工具便于调试
mainWindow.webContents.openDevTools();
}); });
// 添加加载失败事件监听 // 添加加载失败事件监听
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => { mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
console.error(`文件加载失败: ${errorCode} - ${errorDescription}`); console.error(`文件加载失败: ${errorCode} - ${errorDescription}`);
dialog.showErrorBox('加载失败', `无法加载应用界面: ${errorDescription}`); dialog.showErrorBox('加载失败', `无法加载应用界面: ${errorDescription}`);
}); });
// 添加页面崩溃事件监听
mainWindow.webContents.on('crashed', (event, killed) => {
console.error(`页面崩溃: ${killed ? '被杀死' : '意外崩溃'}`);
dialog.showErrorBox('页面崩溃', `应用界面崩溃: ${killed ? '被杀死' : '意外崩溃'}`);
});
} catch (error) { } catch (error) {
console.error(`加载文件时出错: ${error}`); console.error(`加载文件时出错: ${error}`);
dialog.showErrorBox('加载失败', `无法加载应用界面: ${error.message}`); dialog.showErrorBox('加载失败', `无法加载应用界面: ${error.message}`);
@ -146,6 +146,7 @@ function createWindow() {
} }
} }
// 添加窗口关闭前的确认对话框 // 添加窗口关闭前的确认对话框
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',
}
}
}
}) })