209 lines
5.6 KiB
TypeScript
209 lines
5.6 KiB
TypeScript
import { app, screen, globalShortcut, BrowserWindow, ipcMain, dialog } from 'electron'
|
||
import { join } from "path"
|
||
import config from './config'
|
||
import './load-serialport'
|
||
import { keyword } from './KeyWord'
|
||
|
||
const child_process = require('child_process')
|
||
const crypto = require('crypto')
|
||
|
||
function queryPass(passPath: string, passValue: string) {
|
||
return new Promise(function (resolve, reject) {
|
||
try {
|
||
child_process.exec(`reg query ${passPath} /v ${passValue}`, (error: Error, stdout: string, stderr: string) => {
|
||
if (error) {
|
||
reject(error)
|
||
return
|
||
}
|
||
resolve({stdout, stderr})
|
||
})
|
||
} catch (error) {
|
||
reject(error)
|
||
}
|
||
})
|
||
}
|
||
|
||
function queryKey(keyPath: string, keyValue: string) {
|
||
return new Promise(function (resolve, reject) {
|
||
try {
|
||
child_process.exec(`reg query ${keyPath} /v ${keyValue}`, (error: Error, stdout: string, stderr: string) => {
|
||
if (error) {
|
||
reject(error)
|
||
return
|
||
}
|
||
resolve({stdout, stderr})
|
||
})
|
||
} catch (error) {
|
||
reject(error)
|
||
}
|
||
})
|
||
}
|
||
|
||
function cryptMD5(GUID: string) {
|
||
let md5 = crypto.createHash('md5')
|
||
let ciphertext = md5.update(GUID).digest('hex')
|
||
return ciphertext.slice(0,8)+'-'+ciphertext.slice(8,12)+'-'+ciphertext.slice(12,16)+'-'+ciphertext.slice(16,20)+'-'+ciphertext.slice(20,32)
|
||
}
|
||
|
||
const passPath = 'HKEY_CURRENT_USER\\SOFTWARE\\HwaSmart'
|
||
const passValue = 'BDAuthorization'
|
||
const keyPath = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography'
|
||
const keyValue = 'MachineGuid'
|
||
|
||
async function checkLaunchEnv() {
|
||
try {
|
||
const passResult: any = await queryPass(passPath, passValue)
|
||
const keyResult: any = await queryKey(keyPath, keyValue)
|
||
if(cryptMD5(keyResult.stdout.slice(83,119) + keyword) == passResult.stdout.slice(72,108)){
|
||
return true
|
||
}else{
|
||
return false
|
||
}
|
||
// 成功
|
||
// 查询到 有这个app启动项
|
||
} catch (error) {
|
||
// 没有查询到该app启动项目
|
||
return false
|
||
}
|
||
}
|
||
|
||
async function main() {
|
||
// 异步代码
|
||
const checkReault: any = await checkLaunchEnv()
|
||
console.log('env right:', checkReault)
|
||
|
||
// 异步代码执行完毕后执行的代码
|
||
if (checkReault) {
|
||
const URl_REGEX = /[a-zA-z]+:\/\/[^\s]*/
|
||
const DEFAULT_OPTION = {
|
||
fullscreen: true,
|
||
frame: false,
|
||
webPreferences: {
|
||
nodeIntegration: true,
|
||
preload: join(__dirname, 'preload.js')
|
||
}
|
||
}
|
||
|
||
const launch = () => {
|
||
const extend_tdt_window = config.get('extend_tdt_window')
|
||
const displays = screen.getAllDisplays()
|
||
|
||
const { option, url } = getMonitorWindowArguments(displays[0])
|
||
createWindow(option, url)
|
||
|
||
if (extend_tdt_window) {
|
||
const { option, url } = getTDTWindowArguments(displays[displays.length - 1])
|
||
createWindow(option, url)
|
||
}
|
||
}
|
||
|
||
const getMonitorWindowArguments = (display: any) => {
|
||
const option = getOption(display)
|
||
const url = process.env.NODE_ENV === 'development' ? 'http://localhost:3000/' : 'dist/render/index.html'
|
||
|
||
return { option, url }
|
||
}
|
||
const getTDTWindowArguments = (display: any) => {
|
||
const option = getOption(display)
|
||
const url = 'http://gd.map.hwasmart.com/'
|
||
|
||
return { option, url }
|
||
}
|
||
|
||
const getOption = (display: any) => {
|
||
const origin = getDisplayOrigin(display)
|
||
const option = Object.assign({}, DEFAULT_OPTION, origin)
|
||
return option
|
||
}
|
||
|
||
const getDisplayOrigin = (display: any) => {
|
||
const origin = !display ? {x: 0, y: 0} : {x: display.bounds.x, y: display.bounds.y}
|
||
return origin
|
||
}
|
||
|
||
function createWindow(option: any, url: any) {
|
||
const win = new BrowserWindow(option)
|
||
|
||
if (URl_REGEX.test(url)) {
|
||
win.loadURL(url)
|
||
} else {
|
||
win.loadFile(url)
|
||
}
|
||
|
||
const isWindows = process.platform === 'win32';
|
||
let needsFocusFix = false;
|
||
let triggeringProgrammaticBlur = false;
|
||
|
||
//弹出警报窗口后 input获取不到焦点
|
||
win.on('blur', (event: any) => {
|
||
if(!triggeringProgrammaticBlur) {
|
||
needsFocusFix = true;
|
||
}
|
||
})
|
||
|
||
win.on('focus', (event: any) => {
|
||
if(isWindows && needsFocusFix) {
|
||
needsFocusFix = false;
|
||
triggeringProgrammaticBlur = true;
|
||
//弹出弹窗后重新聚焦导致windows任务栏显示,重新隐藏底部windows任务栏
|
||
win.minimize(); //最小化函数
|
||
win.restore(); //取消最小化函数(从最小化窗口还原函数)
|
||
setTimeout(function () {
|
||
win.blur(); //失去焦点函数
|
||
win.focus(); //获取焦点函数
|
||
setTimeout(function () {
|
||
triggeringProgrammaticBlur = false;
|
||
}, 100);
|
||
}, 100);
|
||
}
|
||
})
|
||
|
||
if(process.env.NODE_ENV === 'development') win.webContents.openDevTools()
|
||
|
||
ipcMain.on('CLOSE', (event) => {
|
||
const res = dialog.showMessageBox({
|
||
type: 'warning',
|
||
title: '警告',
|
||
message: '确定要关闭软件吗?',
|
||
detail: '关闭软件',
|
||
cancelId: 1, // 按esc默认点击索引按钮
|
||
defaultId: 0, // 默认高亮的按钮下标
|
||
buttons: ['确认', '取消'], // 按钮按索引从右往左排序
|
||
})
|
||
|
||
res.then((data)=>{
|
||
if(data.response == 0){
|
||
win.close()
|
||
}else{
|
||
console.log('not close software')
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
app.whenReady().then(() => {
|
||
launch()
|
||
|
||
// 屏蔽 F11 进入/退出全屏功能
|
||
globalShortcut.register('F11', () => {return})
|
||
})
|
||
|
||
app.on('window-all-closed', () => {
|
||
if (process.platform !== 'darwin') {
|
||
app.quit()
|
||
}
|
||
})
|
||
|
||
app.on('activate', () => {
|
||
if (BrowserWindow.getAllWindows().length === 0) {
|
||
launch()
|
||
}
|
||
})
|
||
} else {
|
||
dialog.showErrorBox('系统提示', '软件启动出错,请联系售后技术支持人员')
|
||
process.exit(1)
|
||
}
|
||
}
|
||
|
||
main()
|