example.js 2.4 KB

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