Skip to content

Commit 9056491

Browse files
committed
自动更新
1 parent 2557f9a commit 9056491

23 files changed

+466
-151
lines changed

Diff for: package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
{
22
"name": "electron-vue-first",
3-
"version": "0.1.0",
3+
"version": "0.0.4",
44
"private": true,
55
"scripts": {
66
"serve": "vue-cli-service serve",
77
"build": "vue-cli-service build",
88
"lint": "vue-cli-service lint",
9-
"electron:build": "vue-cli-service electron:build -w",
9+
"electron:build": "vue-cli-service electron:build --mac --win",
10+
"electron:publish": "vue-cli-service electron:build --mac --win -p always",
1011
"electron:serve": "vue-cli-service electron:serve",
1112
"postinstall": "electron-builder install-app-deps",
1213
"postuninstall": "electron-builder install-app-deps"
1314
},
1415
"main": "background.js",
1516
"dependencies": {
17+
"@vue/composition-api": "^0.5.0",
1618
"core-js": "^3.6.4",
19+
"electron-log": "^4.1.1",
20+
"electron-updater": "^4.2.5",
1721
"vue": "^2.6.11",
1822
"vue-router": "^3.1.6",
1923
"vuex": "^3.1.3"

Diff for: src/App.vue

-32
This file was deleted.

Diff for: src/common/config.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module.exports = {
2+
windowSizeList: [
3+
{
4+
id: 0,
5+
name: 'smaller',
6+
width: 828,
7+
height: 530,
8+
fontSize: '14px'
9+
},
10+
{
11+
id: 1,
12+
name: 'small',
13+
width: 920,
14+
height: 590,
15+
fontSize: '16px'
16+
},
17+
{
18+
id: 2,
19+
name: 'medium',
20+
width: 1012,
21+
height: 650,
22+
fontSize: '16px'
23+
},
24+
{
25+
id: 3,
26+
name: 'big',
27+
width: 1104,
28+
height: 708,
29+
fontSize: '17px'
30+
},
31+
{
32+
id: 4,
33+
name: 'larger',
34+
width: 1198,
35+
height: 766,
36+
fontSize: '18px'
37+
}
38+
]
39+
}

Diff for: src/common/error.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { log } = require('./utils')
2+
3+
process.on('uncaughtException', function (err) {
4+
console.error('An uncaught error occurred!')
5+
console.error(err)
6+
log.error(err)
7+
})
8+
process.on('unhandledRejection', (reason, p) => {
9+
console.error('Unhandled Rejection at: Promise ', p)
10+
console.error(' reason: ', reason)
11+
log.error(reason)
12+
})

Diff for: src/common/ipc.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { ipcMain, ipcRenderer } = require('electron')
2+
3+
exports.mainOn = (event, callback) => {
4+
ipcMain.on(event, callback)
5+
}
6+
exports.mainOnce = (event, callback) => {
7+
ipcMain.once(event, callback)
8+
}
9+
10+
exports.mainHandle = (name, callback) => {
11+
ipcMain.handle(name, callback)
12+
}
13+
exports.mainHandleOnce = (name, callback) => {
14+
ipcMain.handleOnce(name, callback)
15+
}
16+
17+
exports.rendererSend = (name, params) => {
18+
ipcRenderer.send(name, params)
19+
}
20+
exports.rendererSendSync = (name, params) => ipcRenderer.sendSync(name, params)
21+
22+
exports.rendererInvoke = (name, params) => ipcRenderer.invoke(name, params)
23+
24+
exports.rendererOn = (name, callback) => {
25+
ipcRenderer.on(name, callback)
26+
}
27+
exports.rendererOnce = (name, callback) => {
28+
ipcRenderer.once(name, callback)
29+
}

Diff for: src/common/utils.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const log = require('electron-log')
2+
3+
exports.isLinux = process.platform === 'linux'
4+
exports.isWin = process.platform === 'win32'
5+
exports.isMac = process.platform === 'darwin'
6+
7+
exports.log = log

Diff for: src/components/HelloWorld.vue

-60
This file was deleted.

Diff for: src/background.js renamed to src/main/index.js

+40-20
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,67 @@
1-
'use strict'
21
import { app, protocol, BrowserWindow, Menu, globalShortcut } from 'electron'
32
import {
43
createProtocol
54
/* installVueDevtools */
65
} from 'vue-cli-plugin-electron-builder/lib'
7-
86
const path = require('path')
9-
const isDevelopment = process.env.NODE_ENV !== 'production'
7+
8+
// 单例应用程序
9+
if (!app.requestSingleInstanceLock()) {
10+
app.quit()
11+
// return false
12+
}
13+
app.on('second-instance', (event, argv, cwd) => {
14+
if (mainWindow) {
15+
if (mainWindow.isMinimized()) mainWindow.restore()
16+
mainWindow.focus()
17+
} else {
18+
app.quit()
19+
}
20+
})
21+
22+
const isDev = global.isDev = process.env.NODE_ENV !== 'production'
23+
24+
const autoUpdate = require('./libs/autoUpdate')
1025

1126
// Keep a global reference of the window object, if you don't, the window will
1227
// be closed automatically when the JavaScript object is garbage collected.
13-
let win
28+
let mainWindow
29+
let winURL
30+
31+
if (isDev) {
32+
// eslint-disable-next-line no-undef
33+
global.__static = path.join(__dirname, '/static')
34+
winURL = 'http://localhost:8080'
35+
} else {
36+
global.__static = path.join(__dirname, '/static')
37+
winURL = 'app://./index.html'
38+
}
1439

1540
// Scheme must be registered before the app is ready
1641
protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true, standard: true } }])
1742

1843
function createWindow () {
1944
// Create the browser window.
20-
win = new BrowserWindow({
45+
mainWindow = global.mainWindow = new BrowserWindow({
2146
width: 800,
2247
height: 600,
2348
webPreferences: {
2449
webSecurity: false, // 取消跨域
2550
nodeIntegration: true
2651
},
2752
// eslint-disable-next-line no-undef
28-
icon: path.join(__static, 'app.ico')
53+
icon: path.join(global.__static, 'app.ico')
2954
})
30-
31-
if (process.env.WEBPACK_DEV_SERVER_URL) {
32-
// Load the url of the dev server if in development mode
33-
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
34-
if (!process.env.IS_TEST) win.webContents.openDevTools()
35-
} else {
55+
if (!isDev) {
3656
createProtocol('app')
37-
// Load the index.html when not in development
38-
win.loadURL('app://./index.html')
3957
}
58+
mainWindow.loadURL(winURL)
4059

41-
win.on('closed', () => {
42-
win = null
60+
mainWindow.on('closed', () => {
61+
mainWindow = null
4362
})
4463
createMenu()
64+
if (!isDev) autoUpdate()
4565
}
4666

4767
/**
@@ -85,7 +105,7 @@ app.on('window-all-closed', () => {
85105
app.on('activate', () => {
86106
// On macOS it's common to re-create a window in the app when the
87107
// dock icon is clicked and there are no other windows open.
88-
if (win === null) {
108+
if (mainWindow === null) {
89109
createWindow()
90110
}
91111
})
@@ -94,7 +114,7 @@ app.on('activate', () => {
94114
// initialization and is ready to create browser windows.
95115
// Some APIs can only be used after this event occurs.
96116
app.on('ready', async () => {
97-
if (isDevelopment && !process.env.IS_TEST) {
117+
if (isDev && !process.env.IS_TEST) {
98118
// Install Vue Devtools
99119
// Devtools extensions are broken in Electron 6.0.0 and greater
100120
// See https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/378 for more info
@@ -110,13 +130,13 @@ app.on('ready', async () => {
110130
}
111131
// 在开发环境和生产环境均可通过快捷键打开devTools
112132
globalShortcut.register('CommandOrControl+Shift+i', function () {
113-
win.webContents.openDevTools()
133+
mainWindow.webContents.openDevTools()
114134
})
115135
createWindow()
116136
})
117137

118138
// Exit cleanly on request from parent process in development mode.
119-
if (isDevelopment) {
139+
if (isDev) {
120140
if (process.platform === 'win32') {
121141
process.on('message', data => {
122142
if (data === 'graceful-exit') {

0 commit comments

Comments
 (0)