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);
    }
  }
相关推荐
码事漫谈41 分钟前
以后人人都可以成为导演
后端
Resean02231 小时前
Spring 源码分析(二)配置类的解析与扫描
java·后端·spring
一只爱撸猫的程序猿1 小时前
五周、两个开源仓库、九次拦截之后:AI 时代程序员缺的到底是什么
人工智能·后端·开源
北城笑笑2 小时前
Vue 110 ,若依SSE流式接口限次空500排查与解决
前端·javascript·vue.js·后端·vue
ServBay3 小时前
Local harness 将退役,VS Code 本地开发要不要搬到云端?
后端·ai编程·visual studio code
ashiho3 小时前
【LeetCode hot 100】3. 无重复字符的最长子串 (三种解法)
java·数据结构·后端·算法·leetcode·滑动窗口
张一凡934 小时前
你的 AGENTS.md,真的管到模型写代码了吗?
前端·后端·agent
00后程序员4 小时前
iOS 实时活动怎么开发,Windows 上用 KXBuild 和 ActivityKit 写灵动岛
后端·ios
程序员cxuan4 小时前
推荐 10 个 WorkBuddy 中的邪修用法
人工智能·后端·程序员
Bacon5 小时前
给项目做一次"全身 CT":Agent Skills 生态里的代码体检全链路
前端·人工智能·后端