utils.js 3.7 KB

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