Electron框架使用vue开发跨平台桌面工具应用-后台日志发送到前台和执行导入ZIP

Electron框架使用vue开发跨平台桌面工具应用-后台日志发送到前台和执行导入ZIP

一、后台日志发送到前台

javascript 复制代码
// windowManager.js
let mainWindow = null;

// 设置 mainWindow
export function setMainWindow(window) {
  mainWindow = window;
}

// 获取 mainWindow
export function getMainWindow() {
  return mainWindow;
}

// 发送日志到渲染进程
export function sendLog(message) {
  if (mainWindow) {
    mainWindow.webContents.send('sql-log', message);
  } else {
    console.error('Main window not initialized.');
  }
}
javascript 复制代码
// windowManager.js
let mainWindow = null;

// 设置 mainWindow
export function setMainWindow(window) {
  mainWindow = window;
}

// 获取 mainWindow
export function getMainWindow() {
  return mainWindow;
}

// 发送日志到渲染进程
export function sendLog(message) {
  if (mainWindow) {
    mainWindow.webContents.send('sql-log', message);
  } else {
    console.error('Main window not initialized.');
  }
}

backgroud.js里面引用,并 setMainWindow(win); 3.在vue的js里面调用 sendLog("日志信息")

二、执行导入sql然后删除

ini 复制代码
 // 解压 ZIP 文件
        const zip = new AdmZip(zipfile);
        //const tempDir = path.join(__dirname, 'temp');
        //sendLog(process.execPath);
        //const tempDir = path.join(path.dirname(process.execPath), 'temp');
        const tempDir = path.join(path.dirname(__dirname), 'temp');
        if (!fs.existsSync(tempDir)) {
          fs.mkdirSync(tempDir);
        }
        zip.extractAllTo(tempDir, true);
ini 复制代码
 // 解压 ZIP 文件
        const zip = new AdmZip(zipfile);
        //const tempDir = path.join(__dirname, 'temp');
        //sendLog(process.execPath);
        //const tempDir = path.join(path.dirname(process.execPath), 'temp');
        const tempDir = path.join(path.dirname(__dirname), 'temp');
        if (!fs.existsSync(tempDir)) {
          fs.mkdirSync(tempDir);
        }
        zip.extractAllTo(tempDir, true);
javascript 复制代码
  async function deleteTempDirectory(dirPath) {
    try {
      const files = fs.readdirSync(dirPath);
      
      // 删除目录中的所有文件
      for (const file of files) {
        const filePath = path.join(dirPath, file);
        const stat = fs.statSync(filePath);
  
        if (stat.isDirectory()) {
          // 如果是目录,递归删除
          await deleteTempDirectory(filePath);
        } else {
          // 如果是文件,直接删除
          fs.unlinkSync(filePath);
        }
      }
  
      // 删除空目录
      fs.rmdirSync(dirPath);
      console.log(`临时文件夹 ${dirPath} 已删除`);
    } catch (error) {
      console.error('删除临时文件夹时发生错误:', error);
    }
  }
javascript 复制代码
  async function deleteTempDirectory(dirPath) {
    try {
      const files = fs.readdirSync(dirPath);
      
      // 删除目录中的所有文件
      for (const file of files) {
        const filePath = path.join(dirPath, file);
        const stat = fs.statSync(filePath);
  
        if (stat.isDirectory()) {
          // 如果是目录,递归删除
          await deleteTempDirectory(filePath);
        } else {
          // 如果是文件,直接删除
          fs.unlinkSync(filePath);
        }
      }
  
      // 删除空目录
      fs.rmdirSync(dirPath);
      console.log(`临时文件夹 ${dirPath} 已删除`);
    } catch (error) {
      console.error('删除临时文件夹时发生错误:', error);
    }
  }
相关推荐
小满zs7 小时前
Go语言第十七章(反射)
后端·go
小满zs7 小时前
Go语言第十五章(JSON序列化)
后端·go
小满zs7 小时前
Go语言第十六章(自定义类型)
后端·go
金銀銅鐵8 小时前
[Java] 用 GUI 展示 EnumMap 增删键值对时的内部变化
java·后端·ai编程
学长毕设8 小时前
基于SpringBoot的上门洗车服务系统设计与实现
java·spring boot·后端
小溪学编程11 小时前
Spring @Bean 与 @Component 注解的区别详解
java·后端·spring
IT_陈寒12 小时前
Python的列表推导式用不好真的会爆内存你知道吗
前端·人工智能·后端
摸爬滚打的小李13 小时前
TomCat
后端
itmigrate13 小时前
Rust 里的所有权误区:不是不写 clone 就叫高性能
开发语言·后端·rust
码事漫谈14 小时前
Deepseek v5 来了
后端