139 lines
3.6 KiB
TypeScript
139 lines
3.6 KiB
TypeScript
import { app, globalShortcut, BrowserWindow, ipcMain, dialog } from 'electron'
|
|
import { join } from 'path'
|
|
|
|
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) + 'BD') == passResult.stdout.slice(72,108)){
|
|
return true
|
|
}else{
|
|
return false
|
|
}
|
|
// 成功
|
|
// 查询到 有这个app启动项
|
|
} catch (error) {
|
|
// 没有查询到该app启动项目
|
|
return false
|
|
}
|
|
}
|
|
|
|
function createWindow() {
|
|
const win = new BrowserWindow({
|
|
fullscreen: true,
|
|
frame: false,
|
|
webPreferences: {
|
|
nodeIntegration: true,
|
|
preload: join(__dirname, 'preload.js')
|
|
}
|
|
})
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
win.loadURL('http://localhost:3000/')
|
|
win.webContents.openDevTools()
|
|
} else {
|
|
win.loadFile('dist/render/index.html')
|
|
}
|
|
|
|
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')
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
async function main() {
|
|
// 异步代码
|
|
const checkReault: any = await checkLaunchEnv()
|
|
console.log('env right:', checkReault)
|
|
|
|
// 异步代码执行完毕后执行的代码
|
|
if (checkReault) {
|
|
app.whenReady().then(async () => {
|
|
|
|
createWindow()
|
|
|
|
// 屏蔽 F11 进入/退出全屏功能
|
|
globalShortcut.register('F11', () => {return})
|
|
})
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit()
|
|
}
|
|
})
|
|
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow()
|
|
}
|
|
})
|
|
} else {
|
|
dialog.showErrorBox('系统提示', '软件启动出错,请联系售后技术支持人员')
|
|
process.exit(1)
|
|
}
|
|
}
|
|
|
|
main()
|