example.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. const path = require('path');
  3. const fs = require('fs');
  4. const _ = require('lodash');
  5. const {exec} = require('child_process');
  6. const {app, webContents, shell, dialog} = require('electron');
  7. const AutoLaunchManager = require('../lib/autoLaunch');
  8. const shortcut = require('../lib/shortcut');
  9. const eLogger = require('../lib/eLogger').get();
  10. /**
  11. * app根目录
  12. */
  13. exports.getPath = function () {
  14. const dir = app.getAppPath();
  15. return dir;
  16. }
  17. /**
  18. * 打开目录
  19. */
  20. exports.openDir = function (dir = '') {
  21. if (!dir) {
  22. return false;
  23. }
  24. dir = getElectronPath(dir);
  25. shell.openPath(dir);
  26. return true;
  27. }
  28. /**
  29. * 执行js
  30. */
  31. exports.executeJS = function (str) {
  32. let jscode = `(()=>{alert('${str}');return 'fromJs:${str}';})()`;
  33. console.log(jscode);
  34. return webContents.fromId(1).executeJavaScript(jscode);
  35. }
  36. /**
  37. * 快捷键-注册
  38. */
  39. exports.setShortcut = function (shortcutObj) {
  40. shortcut.register(shortcutObj, true, function (){
  41. MAIN_WINDOW.hide()
  42. });
  43. return true;
  44. }
  45. /**
  46. * 开机启动-开启
  47. */
  48. exports.autoLaunchEnable = function () {
  49. const autoLaunchManager = new AutoLaunchManager()
  50. const enable = autoLaunchManager.enable()
  51. return enable
  52. }
  53. /**
  54. * 开机启动-关闭
  55. */
  56. exports.autoLaunchDisable = function () {
  57. const autoLaunchManager = new AutoLaunchManager()
  58. const disable = autoLaunchManager.disable()
  59. return disable
  60. }
  61. /**
  62. * 开机启动-是否开启
  63. */
  64. exports.autoLaunchIsEnabled = function () {
  65. const autoLaunchManager = new AutoLaunchManager()
  66. const isEnable = autoLaunchManager.isEnabled()
  67. return isEnable
  68. }
  69. /**
  70. * 调用其它程序(exe、bash等可执行程序)
  71. */
  72. exports.openSoftware = function (softName = '') {
  73. if (!softName) {
  74. return false;
  75. }
  76. // 资源路径不同
  77. let softwarePath = '';
  78. if (app.isPackaged) {
  79. // 打包后
  80. softwarePath = path.join(app.getAppPath(), "..", "extraResources", softName);
  81. } else {
  82. // 打包前
  83. softwarePath = path.join(app.getAppPath(), "build", "extraResources", softName);
  84. }
  85. // 检查程序是否存在
  86. if (!fs.existsSync(softwarePath)) {
  87. return false;
  88. }
  89. // 命令行字符串 并 执行
  90. let cmdStr = 'start ' + softwarePath;
  91. exec(cmdStr);
  92. return true;
  93. }
  94. /**
  95. * 选择目录
  96. */
  97. exports.selectDir = function () {
  98. var filePaths = dialog.showOpenDialogSync({
  99. properties: ['openDirectory', 'createDirectory']
  100. });
  101. console.log('[example] [selectDir] filePaths:', filePaths);
  102. if (_.isEmpty(filePaths)) {
  103. return null
  104. }
  105. return filePaths[0];
  106. }
  107. function getElectronPath(filepath) {
  108. //filepath = path.resolve(filepath);
  109. filepath = filepath.replace("resources", "");
  110. filepath = filepath.replace("app.asar", "");
  111. filepath = path.normalize(filepath);
  112. return filepath;
  113. };