utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. const { Controller } = require('ee-core');
  3. const { shell } = require('electron');
  4. const { dialog } = require('electron');
  5. const fs = require('fs');
  6. const path = require('path');
  7. const CoreWindow = require('ee-core/electron/window');
  8. const { BrowserWindow, Menu } = require('electron');
  9. const errData = {
  10. msg :'请求失败,请联系管理员',
  11. code:999
  12. }
  13. /**
  14. * example
  15. * @class
  16. */
  17. class UtilsController extends Controller {
  18. constructor(ctx) {
  19. super(ctx);
  20. }
  21. /**
  22. * 所有方法接收两个参数
  23. * @param args 前端传的参数
  24. * @param event - ipc通信时才有值。详情见:控制器文档
  25. */
  26. /**
  27. * upload
  28. */
  29. async shellFun (params) {
  30. shell[params.action](params.params)
  31. }
  32. async openMain (config) {
  33. if( this.app.electron[config.id]) return;
  34. const win = new BrowserWindow({
  35. ...config,
  36. webPreferences: {
  37. contextIsolation: false, // false -> 可在渲染进程中使用electron的api,true->需要bridge.js(contextBridge)
  38. nodeIntegration: true,
  39. preload: path.join('../preload/bridge.js'),
  40. },
  41. });
  42. win.loadURL(config.url); // 设置窗口的 URL
  43. // 监听窗口关闭事件
  44. win.webContents.openDevTools(config.openDevTools);
  45. win.on('close', () => {
  46. delete this.app.electron[config.id]; // 删除窗口引用
  47. });
  48. this.app.electron[config.id] = win ;
  49. }
  50. async openDirectory(optiops={
  51. title:"选择文件夹"
  52. }){
  53. const filePaths = dialog.showOpenDialogSync({
  54. title:optiops.title || '选择文件夹',
  55. properties: ['openDirectory']
  56. })
  57. if(filePaths[0]) return filePaths[0];
  58. return filePaths
  59. }
  60. async openImage(
  61. optiops= {
  62. title:"选择图片",
  63. filters:[
  64. { name: '支持JPG,png,gif', extensions: ['jpg','jpeg','png'] },
  65. ],
  66. }
  67. ){
  68. const filePaths = dialog.showOpenDialogSync({
  69. title:optiops.title || '选择图片',
  70. properties:['openFile'],
  71. filters:optiops.filters || [
  72. { name: '支持JPG,png,gif', extensions: ['jpg','jpeg','png'] },
  73. ]
  74. })
  75. const filePath = filePaths[0];
  76. const fileBuffer = fs.readFileSync(filePath);
  77. const base64Image = fileBuffer.toString('base64');
  78. // 获取文件扩展名
  79. const extension = path.extname(filePath).toLowerCase().replace('.', '');
  80. // 根据扩展名确定 MIME 类型
  81. let mimeType = '';
  82. switch (extension) {
  83. case 'jpg':
  84. case 'jpeg':
  85. mimeType = 'image/jpeg';
  86. break;
  87. case 'png':
  88. mimeType = 'image/png';
  89. break;
  90. case 'gif':
  91. mimeType = 'image/gif';
  92. break;
  93. default:
  94. mimeType = 'application/octet-stream'; // 默认 MIME 类型
  95. break;
  96. }
  97. // 构建 data URL
  98. const dataUrl = `data:${mimeType};base64,${base64Image}`;
  99. return {
  100. filePath:filePath,
  101. base64Image:dataUrl
  102. };
  103. }
  104. async openFile(optiops= {
  105. title:"选择文件",
  106. filters:[
  107. { name: '支持JPG', extensions: ['jpg','jpeg'] },
  108. ],
  109. }){
  110. const filePaths = dialog.showOpenDialogSync({
  111. title:optiops.title || '选择文件',
  112. properties: ['openFile'],
  113. filters: optiops.filters || [
  114. { name: '选择文件' },
  115. ]
  116. })
  117. if(filePaths[0]) return filePaths[0];
  118. return filePaths
  119. }
  120. }
  121. UtilsController.toString = () => '[class ExampleController]';
  122. module.exports = UtilsController;