utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.on('close', () => {
  45. delete this.app.electron[config.id]; // 删除窗口引用
  46. });
  47. this.app.electron[config.id] = win ;
  48. }
  49. async openDirectory(optiops={
  50. title:"选择文件夹"
  51. }){
  52. const filePaths = dialog.showOpenDialogSync({
  53. title:optiops.title || '选择文件夹',
  54. properties: ['openDirectory']
  55. })
  56. if(filePaths[0]) return filePaths[0];
  57. return filePaths
  58. }
  59. async openImage(
  60. optiops= {
  61. title:"选择图片",
  62. filters:[
  63. { name: '支持JPG,png,gif', extensions: ['jpg','jpeg','png'] },
  64. ],
  65. }
  66. ){
  67. const filePaths = dialog.showOpenDialogSync({
  68. title:optiops.title || '选择图片',
  69. properties:['openFile'],
  70. filters:optiops.filters || [
  71. { name: '支持JPG,png,gif', extensions: ['jpg','jpeg','png'] },
  72. ]
  73. })
  74. const filePath = filePaths[0];
  75. const fileBuffer = fs.readFileSync(filePath);
  76. const base64Image = fileBuffer.toString('base64');
  77. // 获取文件扩展名
  78. const extension = path.extname(filePath).toLowerCase().replace('.', '');
  79. // 根据扩展名确定 MIME 类型
  80. let mimeType = '';
  81. switch (extension) {
  82. case 'jpg':
  83. case 'jpeg':
  84. mimeType = 'image/jpeg';
  85. break;
  86. case 'png':
  87. mimeType = 'image/png';
  88. break;
  89. case 'gif':
  90. mimeType = 'image/gif';
  91. break;
  92. default:
  93. mimeType = 'application/octet-stream'; // 默认 MIME 类型
  94. break;
  95. }
  96. // 构建 data URL
  97. const dataUrl = `data:${mimeType};base64,${base64Image}`;
  98. return {
  99. filePath:filePath,
  100. base64Image:dataUrl
  101. };
  102. }
  103. async openFile(optiops= {
  104. title:"选择文件",
  105. filters:[
  106. { name: '支持JPG', extensions: ['jpg','jpeg'] },
  107. ],
  108. }){
  109. const filePaths = dialog.showOpenDialogSync({
  110. title:optiops.title || '选择文件',
  111. properties: ['openFile'],
  112. filters: optiops.filters || [
  113. { name: '选择文件' },
  114. ]
  115. })
  116. if(filePaths[0]) return filePaths[0];
  117. return filePaths
  118. }
  119. }
  120. UtilsController.toString = () => '[class ExampleController]';
  121. module.exports = UtilsController;