preload.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // preload.js
  2. const { contextBridge, ipcRenderer } = require('electron');
  3. // 暴露安全的 API 给渲染进程
  4. contextBridge.exposeInMainWorld('electronAPI', {
  5. // 系统退出相关操作
  6. exitApp: () => ipcRenderer.invoke('exit-close'),
  7. shutdownSystem: () => ipcRenderer.invoke('exit-shutdown'),
  8. // 日志功能(保留原有功能)
  9. writeLog: (level, msg) => ipcRenderer.invoke('write-log', level, msg)
  10. });
  11. // 暴露存储API
  12. contextBridge.exposeInMainWorld('electronStorage', {
  13. /**
  14. * 获取存储项
  15. * @param {string} key - 存储键
  16. * @returns {Promise<string|null>} 存储的值
  17. */
  18. getItem: (key) => ipcRenderer.invoke('storage-get', key),
  19. /**
  20. * 设置存储项
  21. * @param {string} key - 存储键
  22. * @param {string} value - 要存储的值
  23. * @returns {Promise<void>}
  24. */
  25. setItem: (key, value) => ipcRenderer.invoke('storage-set', key, value),
  26. /**
  27. * 删除存储项
  28. * @param {string} key - 存储键
  29. * @returns {Promise<void>}
  30. */
  31. removeItem: (key) => ipcRenderer.invoke('storage-remove', key),
  32. });
  33. // 暴露打印API
  34. contextBridge.exposeInMainWorld('electronPrint', {
  35. /**
  36. * 打印HTML内容(传统方式)
  37. * @param {Object} options - 打印选项
  38. * @param {string} options.html - 要打印的HTML内容
  39. * @param {string} options.orientation - 打印方向 ('portrait' | 'landscape')
  40. * @param {string} options.paperSize - 纸张尺寸 ('A3' | 'A4')
  41. * @param {boolean} options.silent - 是否静默打印
  42. * @param {Object} options.margins - 页边距
  43. */
  44. print: (options) => ipcRenderer.invoke('print-film', options),
  45. /**
  46. * 打印HTML内容(使用File System API)
  47. * @param {Object} options - 打印选项
  48. * @param {string} options.html - 要打印的HTML内容
  49. * @param {Array} options.imageDataList - 图片数据数组
  50. * @param {string} options.orientation - 打印方向 ('portrait' | 'landscape')
  51. * @param {string} options.paperSize - 纸张尺寸 ('A3' | 'A4')
  52. * @param {boolean} options.silent - 是否静默打印
  53. * @param {Object} options.margins - 页边距
  54. */
  55. printToFile: (options) => ipcRenderer.invoke('print-film-to-file', options),
  56. /**
  57. * 导出为PDF
  58. * @param {Object} options - 导出选项
  59. */
  60. printToPDF: (options) => ipcRenderer.invoke('print-to-pdf', options),
  61. });