utils.js 3.1 KB

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