1.安装electron-store,它是Electron应用程序中持久化存储数据的模块。

npm install electron-store

2.如果出现版本问题。

npm install electron-store --legacy-peer-deps

3.引入electron-store,在background.js中使用。

const Store = require('electron-store')
const store = new Store({
})
let wins

async function createWindow() {
  // Create the browser window.
  Menu.setApplicationMenu(null)
  wins = new BrowserWindow({
    width: 485,
    height: 344,
    title: '交换机路由器数据集成分析系统',
    closable: true,
    resizable: false,//禁止窗口随意拉伸
    webPreferences: {
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
      backgroundThrottling: false,//窗口最小化代码仍然执行
      enableRemoteModule: true,//控制窗口最小化
    }
  })
  if (process.env.WEBPACK_DEV_SERVER_URL) { //process.env.WEBPACK_DEV_SERVER_URL判断是开发环境还是生产环境。
    await wins.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    if (!process.env.IS_TEST) wins.webContents.openDevTools()
    console.log(!store.get('first'));

//开发环境中的判断
    if (!store.get('first')) {
      // 第一次打开软件的逻辑
      console.log('第一次打开软件')
      const windows = BrowserWindow.getAllWindows()
      windows.forEach(win => {
        win.webContents.send('first-run', 'true')
      })
    } else {
      // 非第一次打开软件的逻辑
      const windows = BrowserWindow.getAllWindows()
      windows.forEach(win => {
        win.webContents.send('first-run', 'false')
      })
      console.log('非第一次打开软件')
    }


  } else {


    createProtocol('app')
    // Load the index.html when not in development
    wins.loadURL('app://./index.html')
    // console.log(isFirstRun());

//生产环境中的判断
    if (!store.get('first')) {
      // 第一次打开软件的逻辑
      console.log('第一次打开软件')
      wins.webContents.on('did-finish-load', () => {
        const windows = BrowserWindow.getAllWindows()
        windows.forEach(win => {
          win.webContents.send('first-run', 'true')
        })
      })
    } else {
      // 非第一次打开软件的逻辑
      wins.webContents.on('did-finish-load', () => {
        const windows = BrowserWindow.getAllWindows()
        windows.forEach(win => {
          win.webContents.send('first-run', 'false')
        })
      })
      console.log('非第一次打开软件')
    }
  }

}








ipcMain.on('is-first-open', (event, newValue) => {
  console.log(newValue);
  // 判断是否是第一次打开软件
  if (newValue == 'true') {
    store.set('first', true)
  }
})

4.在渲染进程中的created中监听,也可以在其他钩子中监听,根据需求。

  ipcRenderer.on('first-run', (event, isFirstRun) => {
            console.log(isFirstRun);
            if (isFirstRun == 'true') {
                // 第一次打开应用程序的逻辑
                console.log('第一次打开该应用程序');
            }
            if (isFirstRun == 'false') {
                // 非第一次打开应用程序的逻辑
                this.$router.push('/home')
                console.log('非第一次打开该应用程序');
            }
        });

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐