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);
    }
  }
相关推荐
考虑考虑8 小时前
Springboot环境变量占位符语法
spring boot·后端·spring
步行cgn10 小时前
Spring 基于 XML 的自动装配:byName 详解
java·后端·spring
RISCV_Explorer12 小时前
RISC-V启动与运行时规范机制解析——BRS约定、HSM多核启动与设备树要求
后端·risc-v
星栈独行12 小时前
ADK-Rust 是什么?Rust 生态新一代 AI Agent 开发套件
人工智能·后端·rust
JavaGuide12 小时前
对标 MinIO!全新一代分布式文件系统正式发布
前端·后端
用户83562907805113 小时前
使用 Python 将 DOCX 转换为 Markdown
后端·python
IT_陈寒13 小时前
Vue computed属性这个坑,我居然踩了三次才爬出来
前端·人工智能·后端
孙启超13 小时前
【AI开发之Rust】第 7 课:错误处理 —— panic、Result 与 `?`
人工智能·分布式·后端·爬虫·spring cloud·架构·rust
烈风逍遥14 小时前
第一篇:SeaPack 全栈项目工程化实践
前端·后端·架构
烈风逍遥14 小时前
第二篇:SeaPack 权限体系:从"谁都能看"到"该看什么看什么"
前端·后端·架构