import { app, shell, BrowserWindow, nativeImage, ipcMain } from 'electron' import * as path from 'path' import { electronApp, optimizer, is } from '@electron-toolkit/utils' const fs = require('fs'); // logo const logoIcon = nativeImage.createFromPath(path.join(__dirname, '../../public/icon/icon.jpg')) // 主窗口 let mainWindow function createWindow() { mainWindow = new BrowserWindow({ minWidth: 1366, minHeight: 900, height: 1260, show: false, autoHideMenuBar: true, icon: logoIcon, webPreferences: { preload: path.resolve(__dirname, '../preload/index.js'), sandbox: false } }) mainWindow.on('ready-to-show', () => { mainWindow.show() // mainWindow.webContents.openDevTools() }) mainWindow.webContents.setWindowOpenHandler((details) => { shell.openExternal(details.url) return { action: 'deny' } }) // mainWindow.loadURL('http://localhost:5173/') mainWindow.on('close', () => { app.exit() }) if (is.dev && process.env['ELECTRON_RENDERER_URL']) { mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']) } else { mainWindow.loadFile(path.join(__dirname, '../renderer/index.html')) } } app.whenReady().then(() => { 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 app.on('browser-window-created', (_, window) => { optimizer.watchWindowShortcuts(window) }) 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('createFloder', (e, pathListStr) => { let pathList = JSON.parse(pathListStr) // 获取应用程序的用户数据目录 let successList = [] let errorList = [] for (let index = 0; index < pathList.length; index++) { let element = pathList[index]; let folderPath = path.join("D:\\test\\", element); try { fs.mkdirSync(`${folderPath}\\`, { recursive: true }); successList.push(element) } catch (error) { errorList.push(element) } } mainWindow.webContents.send(`createFloderResult`, JSON.stringify({ successList, errorList })) }) // 重命名文件夹 ipcMain.on('putFloder', (e, pathObjStr) => { let pathObj = JSON.parse(pathObjStr) // 获取应用程序的用户数据目录 let successList = [] let errorList = [] let oldfolderPath = path.join("D:\\test\\", pathObj.oldPath); let newfolderPath = path.join("D:\\test\\", pathObj.newPath); try { fs.rename(`${oldfolderPath}\\`, `${newfolderPath}\\`, error => { if (error) { errorList.push(pathObj) } else { successList.push(pathObj) } mainWindow.webContents.send(`putFloderResult`, JSON.stringify({ successList, errorList })) }); } catch (error) { } }) // 删除文件夹 ipcMain.on('delFloder', (e, pathObjStr) => { let pathObj = JSON.parse(pathObjStr) // 获取应用程序的用户数据目录 let successList = [] let errorList = [] let folderPath = path.join("D:\\test\\", pathObj.name); fs.rm(folderPath, { recursive: true, force: true }, function (err) { if (err) { errorList.push(pathObj) } else { successList.push(pathObj) } mainWindow.webContents.send(`delFloderResult`, JSON.stringify({ successList, errorList })) }); }) // 新增文件 ipcMain.on('createFile', (e, pathListStr) => { let pathList = JSON.parse(pathListStr) // 获取应用程序的用户数据目录 let successList = [] let errorList = [] for (let index = 0; index < pathList.length; index++) { let element = pathList[index]; let destinationFilePath = path.join("D:\\test\\", element.path); let sourceFilePath = element.prePath; try { fs.copyFileSync(sourceFilePath, destinationFilePath); successList.push(element) } catch (error) { errorList.push(element) } } mainWindow.webContents.send(`createFileResult`, JSON.stringify({ successList, errorList })) }) // 重命名文件 ipcMain.on('putFile', (e, paramsStr) => { let paramsObj = JSON.parse(paramsStr) mainWindow.webContents.send(`putFileResult`, paramsObj) }) // 删除文件 ipcMain.on('delFile', (e, paramsStr) => { let paramsObj = JSON.parse(paramsStr) mainWindow.webContents.send(`delFileResult`, paramsObj) }) // 打开文件 ipcMain.on('openFile', (e, pathStr) => { let filePath = path.join("D:\\test\\", pathStr.replace("D:\\test\\", "")); shell.openPath(filePath) }) // 下载文件 ipcMain.on('downloadFile', (e, pathStr) => { let filePath = path.join("D:\\test\\", pathStr); // 读取文件后传给前端 fs.readFile(filePath, function (err, data) { if (err) { console.error('读取文件时出错:', err); mainWindow.webContents.send(`downloadFileResult`, { success: false, error: err.message }); } else { mainWindow.webContents.send(`downloadFileResult`, { success: true, data, path: pathStr }); } }); }); // 批量导入 ipcMain.on('inputDataList', (e, paramsStr) => { let { pathList, tableList } = JSON.parse(paramsStr) // 获取应用程序的用户数据目录 let successFolderList = [] let errorFolderList = [] for (let index = 0; index < pathList.length; index++) { let element = pathList[index]; let folderPath = path.join("D:\\test\\", element); try { fs.mkdirSync(`${folderPath}\\`, { recursive: true }); successFolderList.push(element) } catch (error) { errorFolderList.push(element) } } // 获取应用程序的用户数据目录 let successFileList = [] let errorFileList = [] for (let index = 0; index < tableList.length; index++) { let element = tableList[index]; let destinationFilePath = path.join("D:\\test\\", element.path.replace("D:\\test\\", "")); let sourceFilePath = element.prePath; // 读取文件大小 let fileSize = fs.statSync(sourceFilePath).size; element.size = fileSize try { fs.copyFileSync(sourceFilePath, destinationFilePath); successFileList.push(element) } catch (error) { errorFileList.push(element) } } mainWindow.webContents.send(`inputDataListResult`, JSON.stringify({ successFolderList, errorFolderList, successFileList, errorFileList })) }) }) app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required'); // 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. app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() } }) // In this file you can include the rest of your app"s specific main process // code. You can also put them in separate files and require them here.