utils.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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,app } = require('electron');
  10. const { spawn } = require('child_process');
  11. const Log = require('ee-core/log');
  12. const { readConfigFile } = require('../utils/config');
  13. const configDeault = readConfigFile();
  14. const errData = {
  15. msg :'请求失败,请联系管理员',
  16. code:999
  17. }
  18. const sharp = require('sharp'); // 确保安装:npm install sharp
  19. /**
  20. * example
  21. * @class
  22. */
  23. class UtilsController extends Controller {
  24. constructor(ctx) {
  25. super(ctx);
  26. }
  27. /**
  28. * 运行外部工具(如exe)
  29. * @param {{exeName?: string, exePath?: string, args?: string[]}} params
  30. */
  31. async runExternalTool (params = {}) {
  32. try {
  33. const { exeName, exePath, args = [] } = params;
  34. const targetName = exePath || exeName;
  35. if (!targetName) {
  36. throw new Error('缺少可执行文件名称');
  37. }
  38. const isPackaged = app.isPackaged;
  39. const execDir = isPackaged ? path.dirname(app.getPath('exe')) : path.join(app.getAppPath(), '.');
  40. const resourcesDir = process.resourcesPath;
  41. const candidates = [];
  42. const pushCandidate = (candidatePath, isAbsolute = false) => {
  43. if (!candidatePath) return;
  44. const normalized = isAbsolute || path.isAbsolute(candidatePath)
  45. ? candidatePath
  46. : path.join(execDir, candidatePath);
  47. if (!candidates.includes(normalized)) {
  48. candidates.push(normalized);
  49. }
  50. };
  51. // 允许前端显式传入候选路径数组
  52. if (Array.isArray(params.candidatePaths)) {
  53. params.candidatePaths.forEach((p) => pushCandidate(p));
  54. }
  55. if (path.isAbsolute(targetName)) {
  56. pushCandidate(targetName, true);
  57. } else {
  58. if (isPackaged) {
  59. pushCandidate(path.join(execDir, targetName), true);
  60. pushCandidate(path.join(resourcesDir, targetName), true);
  61. pushCandidate(path.join(resourcesDir, 'extraResources', targetName), true);
  62. } else {
  63. pushCandidate(targetName);
  64. pushCandidate(path.join('build', 'extraResources', targetName));
  65. pushCandidate(path.join('extraResources', targetName));
  66. }
  67. }
  68. console.log('=======');
  69. console.log(candidates);
  70. const resolvedPath = candidates.find((filePath) => filePath && fs.existsSync(filePath));
  71. if (!resolvedPath) {
  72. throw new Error(`未找到工具:${targetName}`);
  73. }
  74. // 如果已经有正在运行的子进程,则先关闭旧进程,再重新启动新的(保证始终只有一个进程,同时刷新页面参数)
  75. if (this.app.externalToolProcess && !this.app.externalToolProcess.killed) {
  76. try {
  77. this.app.externalToolProcess.kill();
  78. } catch (e) {
  79. console.warn('关闭旧 externalTool 进程失败(忽略继续启动新进程):', e);
  80. }
  81. this.app.externalToolProcess = null;
  82. }
  83. const child = spawn(resolvedPath, args, {
  84. cwd: path.dirname(resolvedPath),
  85. detached: true,
  86. stdio: 'ignore',
  87. windowsHide: false
  88. });
  89. // 记录子进程,方便后续复用/判断是否已退出
  90. this.app.externalToolProcess = child;
  91. child.on('exit', () => {
  92. if (this.app.externalToolProcess === child) {
  93. this.app.externalToolProcess = null;
  94. }
  95. });
  96. child.unref();
  97. return {
  98. code: 0,
  99. data: {
  100. path: resolvedPath,
  101. reused: false
  102. }
  103. };
  104. } catch (error) {
  105. console.error('runExternalTool error:', error);
  106. return {
  107. code: 1,
  108. msg: error.message || '运行外部工具失败'
  109. };
  110. }
  111. }
  112. /**
  113. * 所有方法接收两个参数
  114. * @param args 前端传的参数
  115. * @param event - ipc通信时才有值。详情见:控制器文档
  116. */
  117. /**
  118. * upload
  119. */
  120. async shellFun (params) {
  121. // 如果是打开路径操作,确保路径存在
  122. if (params.action === 'openMkPath') {
  123. try {
  124. // 确保目录存在,如果不存在就创建
  125. if (!fs.existsSync(params.params)) {
  126. fs.mkdirSync(params.params, { recursive: true });
  127. }
  128. shell.openPath(params.params)
  129. return;
  130. } catch (err) {
  131. console.error('创建目录失败:', err);
  132. // 即使创建目录失败,也尝试打开路径(可能已经存在)
  133. }
  134. }
  135. shell[params.action](params.params)
  136. }
  137. async openMain (config) {
  138. const { id, url } = config;
  139. if (this.app.electron[id]) {
  140. const win = this.app.electron[id];
  141. // 切换到指定的 URL
  142. await win.loadURL(url);
  143. win.focus();
  144. win.show();
  145. return;
  146. }
  147. const win = new BrowserWindow({
  148. ...config,
  149. webPreferences: {
  150. webSecurity: false,
  151. contextIsolation: false, // false -> 可在渲染进程中使用electron的api,true->需要bridge.js(contextBridge)
  152. nodeIntegration: true,
  153. // preload: path.join('../preload/preload.js','../preload/bridge.js'),
  154. },
  155. });
  156. await win.loadURL(config.url); // 设置窗口的 URL
  157. // 监听窗口关闭事件
  158. if(configDeault.debug) win.webContents.openDevTools()
  159. //
  160. win.on('close', () => {
  161. delete this.app.electron[config.id]; // 删除窗口引用
  162. });
  163. this.app.electron[config.id] = win ;
  164. }
  165. async openDirectory(optiops={
  166. title:"选择文件夹"
  167. }){
  168. const filePaths = dialog.showOpenDialogSync({
  169. title:optiops.title || '选择文件夹',
  170. properties: ['openDirectory']
  171. })
  172. if(filePaths[0]) return filePaths[0];
  173. return filePaths
  174. }
  175. /**
  176. * 关闭所有子窗口
  177. */
  178. closeAllWindows() {
  179. try {
  180. // 获取所有窗口
  181. const windows = this.app.electron;
  182. // 关闭除主窗口外的所有窗口
  183. for (const [id, window] of Object.entries(windows)) {
  184. if (!['mainWindow','extra'].includes(id)) { // 保留主窗口
  185. try {
  186. window.close();
  187. delete this.app.electron[id];
  188. } catch (error) {
  189. console.error(`关闭窗口 ${id} 失败:`, error);
  190. }
  191. }
  192. }
  193. console.log('所有子窗口已关闭');
  194. return { success: true, message: '所有子窗口已关闭' };
  195. } catch (error) {
  196. console.error('关闭所有窗口失败:', error);
  197. return { success: false, message: '关闭所有窗口失败: ' + error.message };
  198. }
  199. }
  200. async openImage(
  201. optiops= {
  202. title:"选择图片",
  203. filters:[
  204. { name: '支持JPG,png,gif', extensions: ['jpg','jpeg','png'] },
  205. ],
  206. }
  207. ){
  208. const filePaths = dialog.showOpenDialogSync({
  209. title:optiops.title || '选择图片',
  210. properties:['openFile'],
  211. filters:optiops.filters || [
  212. { name: '支持JPG,png,gif', extensions: ['jpg','jpeg','png'] },
  213. ]
  214. })
  215. const filePath = filePaths[0];
  216. const fileBuffer = fs.readFileSync(filePath);
  217. const base64Image = fileBuffer.toString('base64');
  218. // 获取文件扩展名
  219. const extension = path.extname(filePath).toLowerCase().replace('.', '');
  220. // 根据扩展名确定 MIME 类型
  221. let mimeType = '';
  222. switch (extension) {
  223. case 'jpg':
  224. case 'jpeg':
  225. mimeType = 'image/jpeg';
  226. break;
  227. case 'png':
  228. mimeType = 'image/png';
  229. break;
  230. case 'gif':
  231. mimeType = 'image/gif';
  232. break;
  233. default:
  234. mimeType = 'application/octet-stream'; // 默认 MIME 类型
  235. break;
  236. }
  237. // 构建 data URL
  238. const dataUrl = `data:${mimeType};base64,${base64Image}`;
  239. return {
  240. filePath:filePath,
  241. base64Image:dataUrl
  242. };
  243. }
  244. /**
  245. * 将前端生成的详情图数据写入 EXE 同级目录下的 output 文件夹。
  246. * @param {{ bundles: Array }} payload
  247. */
  248. async saveGeneratedImages (payload = {}) {
  249. try {
  250. const { app } = require('electron');
  251. const bundles = payload.bundles || [];
  252. if (!Array.isArray(bundles) || !bundles.length) {
  253. return { code: 1, msg: '无可保存的图片数据' };
  254. }
  255. // 运行目录:打包后为 EXE 所在目录,开发环境为项目根目录
  256. const isPackaged = app.isPackaged;
  257. const exeDir = isPackaged ? path.dirname(app.getPath('exe')) : process.cwd();
  258. const baseOutputDir = path.join(exeDir, 'output');
  259. if (!fs.existsSync(baseOutputDir)) {
  260. fs.mkdirSync(baseOutputDir, { recursive: true });
  261. }
  262. let fileCount = 0;
  263. const saveDataUrlToFile = (dataUrl, targetPath) => {
  264. if (!dataUrl || typeof dataUrl !== 'string') return;
  265. const match = dataUrl.match(/^data:image\/\w+;base64,(.+)$/);
  266. const base64Data = match ? match[1] : dataUrl;
  267. const buffer = Buffer.from(base64Data, 'base64');
  268. const dir = path.dirname(targetPath);
  269. if (!fs.existsSync(dir)) {
  270. fs.mkdirSync(dir, { recursive: true });
  271. }
  272. fs.writeFileSync(targetPath, buffer);
  273. fileCount += 1;
  274. };
  275. bundles.forEach(bundle => {
  276. if (!bundle) return;
  277. const styleNo = (bundle.styleNo || bundle.styleKey || 'UNKNOWN').toString().replace(/[\\\/:*?"<>|]/g, '_');
  278. const styleDir = path.join(baseOutputDir, styleNo);
  279. if (!fs.existsSync(styleDir)) {
  280. fs.mkdirSync(styleDir, { recursive: true });
  281. }
  282. // 单画布图片
  283. (bundle.images || []).forEach((img, idx) => {
  284. if (!img || typeof img.dataUrl !== 'string') return;
  285. if (img.dataUrl === 'model' || img.dataUrl === 'scene') return;
  286. if (!img.dataUrl.startsWith('data:image')) return;
  287. const fileName = `canvas_${img.canvasIndex || 0}_img_${idx}.jpg`;
  288. const targetPath = path.join(styleDir, fileName);
  289. saveDataUrlToFile(img.dataUrl, targetPath);
  290. });
  291. // 合成长图
  292. if (bundle.combined && bundle.combined.dataUrl) {
  293. const combinedPath = path.join(styleDir, 'all_canvases.jpg');
  294. saveDataUrlToFile(bundle.combined.dataUrl, combinedPath);
  295. }
  296. });
  297. return {
  298. code: 0,
  299. data: {
  300. outputDir: baseOutputDir,
  301. fileCount,
  302. },
  303. };
  304. } catch (error) {
  305. console.error('saveGeneratedImages error:', error);
  306. return {
  307. code: 1,
  308. msg: error.message || '保存生成图片失败',
  309. };
  310. }
  311. }
  312. async openFile(optiops= {
  313. title:"选择文件",
  314. filters:[
  315. { name: '支持JPG', extensions: ['jpg','jpeg'] },
  316. ],
  317. }){
  318. const filePaths = dialog.showOpenDialogSync({
  319. title:optiops.title || '选择文件',
  320. properties: ['openFile'],
  321. filters: optiops.filters || [
  322. { name: '选择文件' },
  323. ]
  324. })
  325. if(filePaths[0]) return filePaths[0];
  326. return filePaths
  327. }
  328. getAppConfig(){
  329. const config = readConfigFile()
  330. const appPath = path.join(app.getAppPath(), '../..');
  331. const pyPath = path.join(path.dirname(appPath), 'extraResources', 'py');
  332. return {
  333. ...config,
  334. userDataPath: app.getPath('userData'),
  335. appPath: appPath,
  336. pyPath:pyPath,
  337. }
  338. }
  339. // 字体管理相关方法
  340. async downloadFont(args, event) {
  341. try {
  342. console.log('🔤 IPC downloadFont called with args:', args, 'event:', event)
  343. const { url, fontName } = args;
  344. console.log('🔤 IPC downloadFont parsed:', { url, fontName })
  345. Log.info('downloadFontdownloadFontdownloadFontdownloadFont:', url, fontName)
  346. const https = require('https');
  347. const http = require('http');
  348. const fs = require('fs');
  349. const path = require('path');
  350. const crypto = require('crypto');
  351. // 获取字体缓存目录
  352. const userDataPath = app.getPath('userData');
  353. const fontCacheDir = path.join(userDataPath, 'fonts');
  354. // 确保字体缓存目录存在
  355. if (!fs.existsSync(fontCacheDir)) {
  356. fs.mkdirSync(fontCacheDir, { recursive: true });
  357. }
  358. // 生成字体文件名(使用URL的哈希值避免文件名冲突)
  359. const urlHash = crypto.createHash('md5').update(url).digest('hex').substring(0, 8);
  360. const fontFileName = `${fontName.replace(/[^a-zA-Z0-9]/g, '_')}_${urlHash}.ttf`;
  361. const localFontPath = path.join(fontCacheDir, fontFileName);
  362. // 检查本地是否已有字体文件
  363. if (fs.existsSync(localFontPath)) {
  364. console.log('字体已存在:', localFontPath);
  365. return { success: true, path: localFontPath };
  366. }
  367. console.log('开始下载字体:', fontName, '到:', localFontPath);
  368. // 下载字体文件
  369. const protocol = url.startsWith('https:') ? https : http;
  370. return new Promise((resolve, reject) => {
  371. const request = protocol.get(url, (response) => {
  372. if (response.statusCode !== 200) {
  373. reject(new Error(`Failed to download font: ${response.statusCode}`));
  374. return;
  375. }
  376. const fileStream = fs.createWriteStream(localFontPath);
  377. response.pipe(fileStream);
  378. fileStream.on('finish', () => {
  379. fileStream.close();
  380. console.log('字体下载完成:', localFontPath);
  381. resolve({ success: true, path: localFontPath });
  382. });
  383. fileStream.on('error', (error) => {
  384. fs.unlinkSync(localFontPath); // 删除失败的文件
  385. reject(error);
  386. });
  387. });
  388. request.on('error', (error) => {
  389. reject(error);
  390. });
  391. // 设置超时
  392. request.setTimeout(30000, () => {
  393. request.abort();
  394. reject(new Error('Font download timeout'));
  395. });
  396. });
  397. } catch (error) {
  398. Log.info('downloadFont Error:', error);
  399. return { success: false, error: error.message };
  400. }
  401. }
  402. async loadFontFromCache(args, event) {
  403. try {
  404. const { fontName, fontUrl } = args;
  405. const fs = require('fs');
  406. const path = require('path');
  407. const crypto = require('crypto');
  408. // 获取字体缓存目录
  409. const userDataPath = app.getPath('userData');
  410. const fontCacheDir = path.join(userDataPath, 'fonts');
  411. // 生成字体文件名
  412. const urlHash = crypto.createHash('md5').update(fontUrl).digest('hex').substring(0, 8);
  413. const fontFileName = `${fontName.replace(/[^a-zA-Z0-9]/g, '_')}_${urlHash}.ttf`;
  414. const localFontPath = path.join(fontCacheDir, fontFileName);
  415. // 检查本地是否已有字体文件
  416. if (fs.existsSync(localFontPath)) {
  417. // 读取字体文件内容并转换为base64
  418. const fontData = fs.readFileSync(localFontPath);
  419. const base64Data = fontData.toString('base64');
  420. const dataUrl = `data:font/ttf;base64,${base64Data}`;
  421. return { success: true, dataUrl, path: localFontPath };
  422. }
  423. return { success: false, message: 'Font not found in cache' };
  424. } catch (error) {
  425. console.error('从缓存加载字体失败:', error);
  426. return { success: false, error: error.message };
  427. }
  428. }
  429. getFontCachePath(args, event) {
  430. const path = require('path');
  431. const userDataPath = app.getPath('userData');
  432. return path.join(userDataPath, 'fonts');
  433. }
  434. // 测试方法
  435. async testIPC(args, event) {
  436. console.log('🧪 IPC test method called:', args)
  437. return { success: true, message: 'IPC is working', args }
  438. }
  439. async readFileImageForPath(filePath,maxWidth=1500){
  440. const getMimeType = (fileName)=>{
  441. const extension = path.extname(fileName).toLowerCase().replace('.', '');
  442. let mimeType = '';
  443. switch (extension) {
  444. case 'jpg':
  445. case 'jpeg':
  446. mimeType = 'image/jpeg';
  447. break;
  448. case 'png':
  449. mimeType = 'image/png';
  450. break;
  451. case 'gif':
  452. mimeType = 'image/gif';
  453. break;
  454. case 'webp':
  455. mimeType = 'image/webp';
  456. break;
  457. case 'avif':
  458. mimeType = 'image/avif';
  459. break;
  460. default:
  461. mimeType = 'application/octet-stream';
  462. break;
  463. }
  464. return mimeType;
  465. }
  466. try {
  467. const fileName = path.basename(filePath);
  468. const image = sharp(filePath);
  469. const metadata = await image.metadata();
  470. let mimeType = getMimeType(fileName); // 调用下面定义的私有方法获取 MIME 类型
  471. let fileBuffer;
  472. if (metadata.width > maxWidth) {
  473. // 如果宽度大于 1500px,压缩至 1500px 宽度,保持比例
  474. fileBuffer = await image.resize(maxWidth).toBuffer();
  475. } else {
  476. // 否则直接读取原图
  477. fileBuffer = fs.readFileSync(filePath);
  478. }
  479. return {
  480. fileBuffer,
  481. fileName,
  482. mimeType
  483. };
  484. } catch (error) {
  485. console.error('Error processing image:', error);
  486. throw error;
  487. }
  488. }
  489. }
  490. UtilsController.toString = () => '[class ExampleController]';
  491. module.exports = UtilsController;