1 Star 0 Fork 0

guo / 桌面打包

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main copy.js 5.99 KB
一键复制 编辑 原始数据 按行查看 历史
guo 提交于 2022-04-09 15:07 . 1
const {
app,
BrowserWindow,
Menu,
globalShortcut,
dialog
} = require('electron')
const path = require('path')
const url = require('url')
// const mysql = require('mysql');
// const sql = require('mssql');
const regedit = require('regedit');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
const Menus = [];
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 1300,
height: 900,
// frame: false,
webPreferences: {
webviewTag: true,
}
})
win.webContents.openDevTools({
mode:'bottom'
});
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
// win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
let canQuit = false;
win.on('close', (event) => {
if (!canQuit) {
event.preventDefault();
dialog.showMessageBox({
type: 'info',
title: '退出',
message: '确定要退出吗',
buttons: ['yes', 'no']
}).then(idx => {
if (idx.response == 1) {
console.log('index==1,取消关闭')
e.preventDefault();
} else {
console.log('index==0,关闭')
mainWindow = null
app.exit();
}
})
}
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('window-all-closed', function (data) {
alert(data)
dialog.showMessageBox({
type: 'info',
title: '退出',
message: '确定要退出吗',
buttons: ['yes', 'no']
}).then(idx => {
console.log('~~~~~~~~~~~you chose', idx)
if (idx.response == 1) {
console.log('index==1,取消关闭')
e.preventDefault();
} else {
console.log('index==0,关闭')
mainWindow = null
app.exit();
}
})
if (process.platform !== 'darwin') app.quit()
})
const mainMenu = Menu.buildFromTemplate(Menus);
Menu.setApplicationMenu(mainMenu);
// conn = mysql.createConnection({
// host: '127.0.0.1',
// user: 'root',
// password: 's123456',
// database: 'test',
// port: 3306 //MySQL 的默认端口号
// });
// conn.connect();
// conn.query('select * from user', function (error, results, fields) {
// if (error) throw error;
// debugger;
// console.log('数据: ', results);
// });
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient('electron-fiddle', process.execPath, [path.resolve(process.argv[1])])
}
} else {
app.setAsDefaultProtocolClient('electron-fiddle')
}
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.whenReady().then(() => {
createWindow()
})
// 处理协议 在本例中,我们选择显示一个错误提示对话框。
app.on('open-url', (event, url) => {
dialog.showErrorBox('欢迎回来', `导向自: ${url}`)
})
}
// let appurl = app.getPath('exe'); // 获取可运行exe存放目录
// console.log("app路径;",appurl)
// function setPath (url) {
// regedit.putValue({
// 'HKLM\\SOFTWARE\\Classes\\electronAPP': { // 设置注册表url调用electronApp
// 'defaule': {
// value: 'electronAPP', // 设置点击url的弹出框名字(表现不好)
// type: 'REG_DEFAULT'
// },
// 'URL Protocol': {
// value: '',
// type: 'REG_SZ'
// },
// 'path': {
// value: `${url}`,
// type: 'REG_SZ'
// }
// },
// 'HKLM\\SOFTWARE\\Classes\\electronAPP\\shell\\open\\command': {
// 'defaule': {
// value: `"${url}" "$1"`, // 需要唤起的应用程序路劲
// type: 'REG_DEFAULT'
// }
// }
// }, (putErr) => {
// console.log(putErr)
// })
// }
// if (appurl) { // 判断启动url是否正确(用户重新安装,并将安装目录修改)
// regedit.list('HKLM\\SOFTWARE\\Classes\\electronAPP', (listErr, docData) => {
// console.log("~~~~~~~:",listErr,docData)
// if (listErr) {
// regedit.createKey(['HKLM\\SOFTWARE\\Classes\\electronAPP\\shell\\open\\command'], (createErr) => {
// if (!createErr) {
// setPath(appurl)
// }
// })
// } else {
// if (docData['HKLM\\SOFTWARE\\Classes\\electronAPP'].values.path.values !== appurl) {
// setPath(appurl)
// }
// }
// })
// }
// var dbConfig = {
// server: "localhost",
// database: "test",
// user: "sa",
// password: "s123456",
// port: 1433,
// synchronize: true,
// options: { encrypt: false },
// };
// // sql.connect("mssql://sa:123@localhost/john/test").then(function() {
// sql.connect(dbConfig).then(function() {
// // 'select * from user2'
// // 'insert into user2(id) values(10)'
// new sql.Request().query('select * from user2').then(function(recordset) {
// console.log("查询结果:",recordset);
// }).catch(function(err) {
// console.log(err);
// });
// // Stored Procedure
// }).catch(function(err) {
// console.log(err);
// });
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})
// https://npm.taobao.org/mirrors/electron/
// electron-packager . 豫健发布平台 --win --out MyApp --arch=x64 --electron-version 15.1.2 --overwrite --ignore=node_modules --icon=public/icon/logo.ico
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
1
https://gitee.com/guoshize/desktop-packaging.git
git@gitee.com:guoshize/desktop-packaging.git
guoshize
desktop-packaging
桌面打包
master

搜索帮助