123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- 'use strict'
- import { app, protocol, BrowserWindow, Menu, ipcMain, dialog, net } from 'electron'
- import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
- import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
- import { autoUpdater } from 'electron-updater'
- import Store from 'electron-store'
- import { exec } from 'child_process'
- import cqcyCode from "@/utils/cqcyCode";
- Store.initRenderer()
- const isDevelopment = process.env.NODE_ENV !== 'production'
- let win
- let cmdStr = 'start ./public/pkg/PrintServerApp-1.0.0.exe'
- /**
- * 方案必须在应用准备就绪之前注册
- */
- protocol.registerSchemesAsPrivileged([
- { scheme: 'app', privileges: { secure: true, standard: true } }
- ])
- /**
- * 创建菜单
- * @param name
- * @returns {Electron.BrowserWindow}
- */
- function creatMenuWin(name) {
- return new BrowserWindow({
- title: name ? name : 'Easy Industrial Report',
- width: 300,
- height: 300,
- resizable: false,
- movable: false,
- minimizable: false,
- maximizable: false,
- webPreferences: {
- nodeIntegration: true
- }
- })
- }
- /**
- * 更新通知信息
- * @type {{checking: {msg: string, status: number}, updateAva: {msg: string, status: number}, updateNotAva: {msg: string, status: number}, error: {msg: string, status: number}}}
- */
- const returnData = {
- error: { status: -1, msg: '检测更新查询异常' },
- checking: { status: 0, msg: '正在检查应用程序更新' },
- updateAva: { status: 1, msg: '检测到新版本,正在下载,请稍后' },
- updateNotAva: { status: -1, msg: '您现在使用的版本为最新版本,无需更新!' }
- }
- /**
- * 创建通知
- * @param text
- */
- function sendStatusToWindow(text) {
- win.webContents.send('message', text);
- }
- /**
- * 创建主窗口
- * @returns {Promise<void>}
- */
- async function createWindow() {
- // 初始化主窗口服务
- win = new BrowserWindow({
- width: 1920,
- height: 1080,
- frame: true, // false 表示去掉顶部的导航以及最大化、最小化、关闭按钮
- fullscreen: false, // true 全屏模式
- resizable: true, // 缩放
- movable: true, // 移动
- webPreferences: {
- webSecurity: false,
- nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
- contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
- }
- })
- // 设置窗口最大化
- win.maximize()
- if (isDevelopment) {
- // 开启调试模式
- win.webContents.openDevTools()
- } else {
- // 去掉顶部菜单
- win.setMenu(null)
- }
- // 菜单模板设置
- let templateMenu = [
- {
- label: '首页'
- },
- {
- label: '帮助',
- submenu: [
- {
- label: '检查更新',
- click: () => {
- }
- }
- ]
- },
- {
- label: '关于',
- click: () => {
- let winForAbout = creatMenuWin('关于')
- // 去掉顶部菜单
- winForAbout.setMenu(null)
- winForAbout.loadFile('yellow.html')
- winForAbout.on('closed', () => {
- winForAbout = null
- })
- }
- },
- ]
- // 加载菜单
- let m = Menu.buildFromTemplate(templateMenu)
- // Menu.setApplicationMenu(m)
- if (process.env.WEBPACK_DEV_SERVER_URL) {
- // 如果处于开发模式,则加载开发服务器的 url
- await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
- if (!process.env.IS_TEST) win.webContents.openDevTools()
- } else {
- createProtocol('app')
- // 在正式环境时加载 index.html
- await win.loadURL('app://./index.html')
- }
- }
- /**
- * 检查更新
- */
- autoUpdater.on('checking-for-update', (res) => {
- sendStatusToWindow(returnData.checking);
- })
- /**
- * 有更新发现
- */
- autoUpdater.on('update-available', (info) => {
- // sendStatusToWindow(returnData.updateAva);
- dialog.showMessageBox({
- type: 'info',
- title: '软件更新',
- message: '发现新版本, 确定更新?',
- buttons: ['确定', '取消']
- }).then(resp => {
- if (resp.response == 0) {
- createWindow().then(r => {})
- autoUpdater.downloadUpdate().then(r => {})
- }
- })
- })
- /**
- * 暂无更新可用
- */
- autoUpdater.on('update-not-available', (info) => {
- sendStatusToWindow(returnData.updateNotAva);
- })
- /**
- * 检测更新失败
- */
- autoUpdater.on('error', (err) => {
- sendStatusToWindow(returnData.error + err);
- })
- /**
- * 更新下载进度
- */
- autoUpdater.on('download-progress', (progressObj) => {
- // let log_message = "Download speed: " + progressObj.bytesPerSecond;
- // log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
- // log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
- // sendStatusToWindow(log_message);
- win.webContents.send('downloadProgress', progressObj);
- })
- /**
- * 更新下载完成
- */
- autoUpdater.on('update-downloaded', (info) => {
- // sendStatusToWindow('Update downloaded');
- // ipcMain.on('isUpdateNow', (e, arg) => {
- // // autoUpdater.quitAndInstall()方法,可实现立即关闭程序并安装
- // autoUpdater.quitAndInstall();
- // });
- dialog.showMessageBox({
- title: '下载完成',
- message: '最新版本已下载完成, 退出程序进行安装'
- }).then(() => {
- autoUpdater.quitAndInstall()
- })
- });
- /**
- * 关闭所有窗口后退出。
- */
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit()
- }
- })
- /**
- * 应用激活
- */
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) createWindow().then(r => {})
- })
- /**
- * 当 Electron 完成后,将调用此方法。初始化并准备创建浏览器窗口。某些API只能在此事件发生后使用。
- */
- app.on('ready', async () => {
- if (isDevelopment && !process.env.IS_TEST) {
- try {
- await installExtension(VUEJS_DEVTOOLS)
- } catch (e) {
- console.error('Vue Devtools failed to install:', e.toString())
- }
- }
- await createWindow()
- runExec()
- })
- app.on('ready', function() {
- autoUpdater.checkForUpdatesAndNotify().then(r => {});
- });
- function runExec () {
- // 请求网络服务net
- let request = net.request(cqcyCode['checkPrintServerInstated']);
- request.on("response", response=> {
- // 获取请求状态码
- if (response.statusCode !== 200) {
- // dialog.showMessageBox({
- // title: '系统提示',
- // message: '检测到您还未安装自动打印服务,建议您进行安装。'
- // }).then(e => {
- // exec(cmdStr, {});
- // });
- exec(cmdStr, {});
- }
- });
- request.on('error', (error) => {
- // dialog.showMessageBox({
- // title: '系统提示',
- // message: JSON.stringify(error)
- // }).then(e => {
- // exec(cmdStr, {});
- // });
- exec(cmdStr, {});
- });
- request.end();
- }
- if (isDevelopment) {
- if (process.platform === 'win32') {
- process.on('message', (data) => {
- if (data === 'graceful-exit') {
- app.quit();
- }
- });
- } else {
- process.on('SIGTERM', () => {
- app.quit();
- });
- }
- }
|