import { app, BrowserWindow, globalShortcut, dialog, ipcMain } from 'electron'
|
import path from 'path'
|
// import fs from 'fs'
|
import { init as initDownloadTask } from './downloadTask'
|
// import { init as initExportTask } from './exportTask'
|
import { checkUpdate } from './update'
|
|
import axios from 'axios'
|
import { ctx } from './config'
|
|
// 用于本地测试更新
|
// 手动设置为已打包
|
Object.defineProperty(app, 'isPackaged', {
|
get() {
|
return true
|
}
|
})
|
|
let myWindow: any = null
|
|
const request = axios.create()
|
|
const development = process.env.NODE_ENV == 'development' ? true : false
|
|
const createWindow = () => {
|
const win = new BrowserWindow({
|
minWidth: development ? 1800 : 1300,
|
minHeight: development ? 1000 : 800,
|
width: development ? 1400 : 1300,
|
height: development ? 1000 : 800,
|
//窗口是否在屏幕居中. 默认值为 false
|
center: true,
|
//窗口是否在创建时显示。 默认值为 true。
|
show: true,
|
title: '数字教材阅读器',
|
icon: 'public/favicon.ico',
|
webPreferences: {
|
// nodeIntegration: true,
|
// nodeIntegrationInWorker: true,
|
// contextIsolation: true,
|
// sandbox: false,
|
preload: path.join(__dirname, 'preload.js')
|
}
|
})
|
win.setMenu(null)
|
if (development) {
|
const elePath = path.join(__dirname, '../node_modules/electron')
|
require('electron-reload')('../', {
|
electron: require(elePath)
|
})
|
win.loadURL('http://localhost:8005')
|
win.webContents.openDevTools()
|
} else {
|
win.loadURL(path.join(__dirname, 'index.html'))
|
}
|
|
globalShortcut.register('CommandOrControl+Shift+i', function () {
|
win.webContents.openDevTools()
|
})
|
|
// 解决应用启动白屏问题
|
win.on('ready-to-show', () => {
|
win.show()
|
win.focus()
|
})
|
|
request.interceptors.request.use((config) => {
|
if (config.url && config.url.indexOf('http') == -1) {
|
config.url = ctx + config.url
|
}
|
return config
|
})
|
|
// 响应拦截器
|
request.interceptors.response.use(
|
(response) => {
|
return response.data
|
},
|
(error) => {
|
if (error.response && error.response.status == 401) {
|
win.webContents.send('logout')
|
} else {
|
if (error.response && error.response.data && error.response.data.error) {
|
console.error(error.response.data.error.msg)
|
} else {
|
console.error('请求发生错误')
|
}
|
// dialog.showMessageBox({
|
// type: 'error',
|
// message: '错误',
|
// detail: JSON.stringify(error.response)
|
// })
|
}
|
return Promise.reject(error)
|
}
|
)
|
|
// 业务主体
|
// 下载器
|
initDownloadTask(win, request)
|
// initExportTask(win, request)
|
|
checkUpdate(win, ipcMain)
|
|
ipcMain.on('openSelectFileOrFolderDialog', (ev, opt) => {
|
const path = dialog.showOpenDialogSync(win, opt)
|
ev.returnValue = path
|
})
|
|
return win
|
}
|
|
// 获取单实例锁
|
const gotTheLock = app.requestSingleInstanceLock()
|
if (!gotTheLock) {
|
// 如果获取失败,说明已经有实例在运行了,直接退出
|
app.quit()
|
} else {
|
app.whenReady().then(() => {
|
myWindow = createWindow()
|
|
app.on('activate', () => {
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
})
|
|
// 注册自定义协议
|
const agreement = 'DigitalTextbookReader' // 开发环境
|
|
app.removeAsDefaultProtocolClient(agreement) // 每次运行都删除自定义协议 然后再重新注册
|
// 开发模式下在window运行需要做兼容
|
if (development) {
|
// 设置electron.exe 和 app的路径
|
app.setAsDefaultProtocolClient(agreement, process.execPath, [
|
path.resolve(process.argv[1]),
|
'--'
|
])
|
}
|
// 验证是否为自定义协议的链接
|
const AGREEMENT_REGEXP = new RegExp(`^${agreement}://`)
|
|
// mac唤醒应用 会激活open-url事件 在open-url中判断是否为自定义协议打开事件
|
app.on('open-url', (event, url) => {
|
const isProtocol = AGREEMENT_REGEXP.test(url)
|
if (isProtocol) {
|
dialog.showMessageBox({
|
type: 'info',
|
message: 'Mac protocol 自定义协议打开',
|
detail: `自定义协议链接:${url}`
|
})
|
}
|
})
|
|
// window系统下唤醒应用会激活second-instance事件 它在ready执行之后才能被监听
|
app.on('second-instance', (event, argv) => {
|
if (myWindow) {
|
if (myWindow.isMinimized()) myWindow.restore()
|
myWindow.focus()
|
}
|
if (process.platform === 'win32') {
|
// 开发阶段,跳过前两个参数(`electron.exe .`)
|
// 打包后,跳过第一个参数(`myapp.exe`)
|
const offset = app.isPackaged ? 1 : 2
|
let url: any = argv.find((arg, i) => i >= offset && arg.startsWith(agreement))
|
if (url) {
|
url = url.replace(`${agreement}://`, '')
|
url = url.substr(0, url.length - 1)
|
}
|
myWindow.webContents.send('openUrl', url)
|
}
|
})
|
})
|
|
app.on('window-all-closed', () => {
|
if (process.platform !== 'darwin') app.quit()
|
})
|
}
|