From cefb6ae5a77ba77bc1ad68e1e8d9aa0b01a3ba9d Mon Sep 17 00:00:00 2001 From: lyc Date: Mon, 22 Jul 2024 15:10:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E7=AA=97=E5=8F=A3/=E4=B8=BB?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/index.js | 144 +++++++++++------- src/renderer/src/layout/components/Header.vue | 8 +- src/renderer/src/layout/index.vue | 2 - src/renderer/src/router/index.js | 2 +- src/renderer/src/utils/request.js | 3 +- src/renderer/src/views/login/index.vue | 25 +-- 6 files changed, 96 insertions(+), 88 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 775c151..8ef847e 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -5,9 +5,13 @@ import icon from '../../resources/icon.png?asset' import File from './file' File({ app, shell, BrowserWindow, ipcMain }) -function createWindow() { - // Create the browser window. - const mainWindow = new BrowserWindow({ + +let mainWindow, loginWindow + +//登录窗口 +function createLoginWindow(){ + if(loginWindow) return + loginWindow = new BrowserWindow({ width: 888, height: 520, show: false, @@ -19,57 +23,113 @@ function createWindow() { sandbox: false, nodeIntegration: true } + + }) + const loginURL = is.dev ? `http://localhost:5173/#/login` : `file://${__dirname}/index.html/login` + loginWindow.loadURL(loginURL) + + loginWindow.once('ready-to-show', () => { + loginWindow.show() + }) + + loginWindow.on('closed', () => { + loginWindow = null + }) +} +//主窗口 +function createMainWindow() { + mainWindow = new BrowserWindow({ + width: 1200, + height: 700, + show: false, + frame: false, + autoHideMenuBar: true, + ...(process.platform === 'linux' ? { icon } : {}), + webPreferences: { + preload: join(__dirname, '../preload/index.js'), + sandbox: false, + nodeIntegration: true + } }) mainWindow.on('ready-to-show', () => { mainWindow.show() }) - - - mainWindow.webContents.setWindowOpenHandler((details) => { shell.openExternal(details.url) return { action: 'deny' } }) mainWindow.webContents.openDevTools() - // HMR for renderer base on electron-vite cli. - // Load the remote URL for development or the local html file for production. + if (is.dev && process.env['ELECTRON_RENDERER_URL']) { mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']) - - // mainWindow.loadURL('https://file.ysaix.com:7868/') - } else { - // mainWindow.loadURL('https://file.ysaix.com:7868/') mainWindow.loadFile(join(__dirname, '../renderer/index.html')) } } -// This method will be called when Electron has finished -// initialization and is ready to create browser windows. -// Some APIs can only be used after this event occurs. -app.whenReady().then(() => { - // Set app user model id for windows +// 初始化完成 +app.on('ready',()=>{ + // 设置应用程序用户模型标识符 electronApp.setAppUserModelId('com.electron') - // Default open or close DevTools by F12 in development - // and ignore CommandOrControl + R in production. - // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils + //一个新的browserWindow 被创建时触发 app.on('browser-window-created', (_, window) => { optimizer.watchWindowShortcuts(window) }) + //窗口 最大、最小、关闭 + ipcMain.on('minimize-window', () => { + if (loginWindow) { + loginWindow.minimize() + } + if (mainWindow) { + mainWindow.minimize() + } + }); + + ipcMain.on('maximize-window', () => { + mainWindow.isMaximized() ? mainWindow.unmaximize() : mainWindow.maximize(); + }); + + ipcMain.on('close-window', () => { + if (loginWindow) { + loginWindow.destroy() + } + if (mainWindow) { + mainWindow.destroy() + } + }); - createWindow() - - app.on('activate', function () { - // On macOS it's common to re-create a window in the app when the - // dock icon is clicked and there are no other windows open. - if (BrowserWindow.getAllWindows().length === 0) createWindow() + // 打开主窗口 + ipcMain.on('openMainWindow', () => { + if (!mainWindow) { + createMainWindow() + } + + loginWindow.destroy() + loginWindow = null }) + // 打开登录窗口 + ipcMain.on('openLoginWindow', () => { + if (!loginWindow) { + createLoginWindow() + } + mainWindow.destroy() + loginWindow.show() + loginWindow.focus() + }) + + createLoginWindow() + app.on('activate', function () { + if (BrowserWindow.getAllWindows().length === 0) createLoginWindow() + }) + }) + + // Quit when all windows are closed, except on macOS. There, it's common // for applications and their menu bar to stay active until the user quits // explicitly with Cmd + Q. @@ -78,35 +138,3 @@ app.on('window-all-closed', () => { app.quit() } }) - -ipcMain.on('toggle-top', (event) => { - const win = BrowserWindow.getFocusedWindow(); - const isAlwaysOnTop = win.isAlwaysOnTop(); - win.setAlwaysOnTop(!isAlwaysOnTop); - event.sender.send('top-status-changed', !isAlwaysOnTop); -}) - - -ipcMain.on('minimize-window', () => { - const win = BrowserWindow.getFocusedWindow(); - win.minimize(); -}); - -ipcMain.on('maximize-window', () => { - const win = BrowserWindow.getFocusedWindow(); - if (win.isMaximized()) { - win.unmaximize(); - } else { - win.maximize(); - } -}); - -ipcMain.on('close-window', () => { - const win = BrowserWindow.getFocusedWindow(); - win.close(); -}); -ipcMain.on('set-winsize', (e, {x, y})=>{ - const win = BrowserWindow.getFocusedWindow(); - win.setSize(x,y); - win.center() -}) diff --git a/src/renderer/src/layout/components/Header.vue b/src/renderer/src/layout/components/Header.vue index 54c773e..9bdb708 100644 --- a/src/renderer/src/layout/components/Header.vue +++ b/src/renderer/src/layout/components/Header.vue @@ -44,7 +44,7 @@ import { ElMessageBox } from 'element-plus' import WindowTools from '@/components/window-tools/index.vue' import useUserStore from '@/store/modules/user' - +const { ipcRenderer } = window.electron || {} const userStore = useUserStore() const router = useRouter() const currentRoute = ref('') @@ -100,9 +100,11 @@ function logout() { type: 'warning' }).then(() => { userStore.logOut().then(() => { - router.replace('/login') + // router.replace('/login') + ipcRenderer && ipcRenderer.send('openLoginWindow') }).catch(()=>{ - router.replace('/login') + // router.replace('/login') + ipcRenderer && ipcRenderer.send('openLoginWindow') }) }).catch(() => { }); } diff --git a/src/renderer/src/layout/index.vue b/src/renderer/src/layout/index.vue index 3f2af4d..fc79a0c 100644 --- a/src/renderer/src/layout/index.vue +++ b/src/renderer/src/layout/index.vue @@ -18,8 +18,6 @@ import uploaderState from '@/store/modules/uploader' import { ref } from 'vue' let uploaderStore = ref(uploaderState()) -const { ipcRenderer } = window.electron || {} -ipcRenderer ? ipcRenderer .send('set-winsize', { x: 1200, y: 700 }) : ''