electron-odoo/main.js
2025-06-17 11:39:51 +08:00

72 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const i18n = require('i18n-js');
// 设置默认语言和路径
i18n.defaultLocale = 'zh'; // 默认语言改为中文
i18n.locale = 'zh'; // 当前语言环境改为中文
i18n.fallbacks = true; // 启用回退到默认语言
i18n.translations = {
zh: require('./locales/zh.json') // 中文翻译文件
// 可以添加更多语言文件
};
// Odoo服务器URL - 请替换为你的Odoo实例地址
const ODOO_URL = 'https://www.gzsjfz.top';
let mainWindow;
function createWindow() {
// 创建浏览器窗口
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false, // 禁用Node.js集成以增强安全性
contextIsolation: true, // 启用上下文隔离
preload: path.join(__dirname, 'preload.js') // 使用预加载脚本
}
});
// 加载Odoo URL
mainWindow.loadURL(ODOO_URL);
// 打开开发者工具(可选)
// mainWindow.webContents.openDevTools();
}
// 当Electron完成初始化并准备创建浏览器窗口时调用此方法
app.whenReady().then(() => {
createWindow();
// 处理来自渲染进程的打印请求
ipcMain.handle('print', (event, options) => {
return new Promise((resolve) => {
mainWindow.webContents.print(options || {}, (success, errorType) => {
resolve({ success, errorType });
});
});
});
// 处理PDF打印请求
ipcMain.handle('print-to-pdf', (event, options) => {
return new Promise((resolve, reject) => {
mainWindow.webContents.printToPDF(options || {})
.then(data => resolve(data))
.catch(error => reject(error));
});
});
// 在macOS上当点击dock图标并且没有其他窗口打开时通常会重新创建应用程序窗口
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// 在Windows和Linux上当所有窗口关闭时退出应用程序
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});