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);
    }
  }
相关推荐
styshoo24 分钟前
[NVSentinel] syslog-health-monitor模块分析
后端
Zane199428 分钟前
自定义异常该继承 Exception 还是 RuntimeException,就看这一个问题
java·后端
风曳丷30 分钟前
10|攻击如何跨越 Context、阶段与时间
后端
tyung31 分钟前
znet 数据编解码:字节流怎么变成消息
后端·网络协议·go
码视野1 小时前
基于 Spring Boot + Vue3 的【高校化学实验室安全准入考试与危化品配伍排查系统】设计与实现(含PRD/三端高保真源码/大屏)
前端·人工智能·spring boot·后端·安全·vue3
LXMXHJ1 小时前
springboot项目测试
java·spring boot·后端·测试
叫我:松哥1 小时前
基于Flask的教师评教管理系统,支持学生和教师两种角色登录
数据库·后端·python·数据挖掘·flask
JuiceFS1 小时前
如何通过 S3 和 WebDAV 协议访问 JuiceFS?
运维·人工智能·后端
65岁退休Coder2 小时前
LangGraph v1.2.9 核心概念 & 流程控制
后端·python·langchain