// preload.js const { contextBridge, ipcRenderer } = require('electron'); // 暴露安全的 API 给渲染进程 contextBridge.exposeInMainWorld('electronAPI', { // 系统退出相关操作 exitApp: () => ipcRenderer.invoke('exit-close'), shutdownSystem: () => ipcRenderer.invoke('exit-shutdown'), // 日志功能(保留原有功能) writeLog: (level, msg) => ipcRenderer.invoke('write-log', level, msg) }); // 暴露存储API contextBridge.exposeInMainWorld('electronStorage', { /** * 获取存储项 * @param {string} key - 存储键 * @returns {Promise} 存储的值 */ getItem: (key) => ipcRenderer.invoke('storage-get', key), /** * 设置存储项 * @param {string} key - 存储键 * @param {string} value - 要存储的值 * @returns {Promise} */ setItem: (key, value) => ipcRenderer.invoke('storage-set', key, value), /** * 删除存储项 * @param {string} key - 存储键 * @returns {Promise} */ removeItem: (key) => ipcRenderer.invoke('storage-remove', key), }); // 暴露打印API contextBridge.exposeInMainWorld('electronPrint', { /** * 打印HTML内容(传统方式) * @param {Object} options - 打印选项 * @param {string} options.html - 要打印的HTML内容 * @param {string} options.orientation - 打印方向 ('portrait' | 'landscape') * @param {string} options.paperSize - 纸张尺寸 ('A3' | 'A4') * @param {boolean} options.silent - 是否静默打印 * @param {Object} options.margins - 页边距 */ print: (options) => ipcRenderer.invoke('print-film', options), /** * 打印HTML内容(使用File System API) * @param {Object} options - 打印选项 * @param {string} options.html - 要打印的HTML内容 * @param {Array} options.imageDataList - 图片数据数组 * @param {string} options.orientation - 打印方向 ('portrait' | 'landscape') * @param {string} options.paperSize - 纸张尺寸 ('A3' | 'A4') * @param {boolean} options.silent - 是否静默打印 * @param {Object} options.margins - 页边距 */ printToFile: (options) => ipcRenderer.invoke('print-film-to-file', options), /** * 导出为PDF * @param {Object} options - 导出选项 */ printToPDF: (options) => ipcRenderer.invoke('print-to-pdf', options), });