example.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. /**
  3. * egg服务调用electron功能时,建议使用该模块
  4. */
  5. const path = require('path');
  6. const fs = require('fs');
  7. const _ = require('lodash');
  8. const {exec} = require('child_process');
  9. const {app, webContents, shell, dialog} = require('electron');
  10. const AutoLaunchManager = require('../lib/autoLaunch');
  11. const shortcut = require('../lib/shortcut');
  12. /**
  13. * app根目录
  14. */
  15. exports.getPath = function () {
  16. const dir = app.getAppPath();
  17. return dir;
  18. }
  19. /**
  20. * 打开目录
  21. */
  22. exports.openDir = function (dir = '') {
  23. if (!dir) {
  24. return false;
  25. }
  26. dir = getElectronPath(dir);
  27. shell.openPath(dir);
  28. return true;
  29. }
  30. /**
  31. * 执行js
  32. */
  33. exports.executeJS = function (str) {
  34. let jscode = `(()=>{alert('${str}');return 'fromJs:${str}';})()`;
  35. console.log(jscode);
  36. return webContents.fromId(1).executeJavaScript(jscode);
  37. }
  38. /**
  39. * 快捷键-注册
  40. */
  41. exports.setShortcut = function (shortcutObj) {
  42. shortcut.register(shortcutObj, true, function (){
  43. MAIN_WINDOW.hide()
  44. });
  45. return true;
  46. }
  47. /**
  48. * 开机启动-开启
  49. */
  50. exports.autoLaunchEnable = function () {
  51. const autoLaunchManager = new AutoLaunchManager()
  52. const enable = autoLaunchManager.enable()
  53. return enable
  54. }
  55. /**
  56. * 开机启动-关闭
  57. */
  58. exports.autoLaunchDisable = function () {
  59. const autoLaunchManager = new AutoLaunchManager()
  60. const disable = autoLaunchManager.disable()
  61. return disable
  62. }
  63. /**
  64. * 开机启动-是否开启
  65. */
  66. exports.autoLaunchIsEnabled = function () {
  67. const autoLaunchManager = new AutoLaunchManager()
  68. const isEnable = autoLaunchManager.isEnabled()
  69. return isEnable
  70. }
  71. /**
  72. * 调用其它程序(exe、bash等可执行程序)
  73. */
  74. exports.openSoftware = function (softName = '') {
  75. if (!softName) {
  76. return false;
  77. }
  78. // 资源路径不同
  79. let softwarePath = '';
  80. if (app.isPackaged) {
  81. // 打包后
  82. softwarePath = path.join(app.getAppPath(), "..", "extraResources", softName);
  83. } else {
  84. // 打包前
  85. softwarePath = path.join(app.getAppPath(), "build", "extraResources", softName);
  86. }
  87. // 检查程序是否存在
  88. if (!fs.existsSync(softwarePath)) {
  89. return false;
  90. }
  91. // 命令行字符串 并 执行
  92. let cmdStr = 'start ' + softwarePath;
  93. exec(cmdStr);
  94. return true;
  95. }
  96. /**
  97. * 选择目录
  98. */
  99. exports.selectDir = function () {
  100. const filePaths = dialog.showOpenDialogSync({
  101. properties: ['openDirectory', 'createDirectory']
  102. });
  103. console.log('[example] [selectDir] filePaths:', filePaths);
  104. if (_.isEmpty(filePaths)) {
  105. return null
  106. }
  107. return filePaths[0];
  108. }
  109. /**
  110. * 测试用的 - 忽略
  111. */
  112. exports.testElectronApi = function () {
  113. const filePaths = dialog.showSaveDialogSync({
  114. properties: ['openFile', 'multiSelections']
  115. });
  116. console.log('[example] [testElectronApi] filePaths:', filePaths);
  117. return true;
  118. }
  119. /**
  120. * 显示消息对话框
  121. */
  122. exports.messageShow = function () {
  123. dialog.showMessageBoxSync({
  124. type: 'info', // "none", "info", "error", "question" 或者 "warning"
  125. title: '自定义标题-message',
  126. message: '自定义消息内容',
  127. detail: '其它的额外信息'
  128. })
  129. return true;
  130. }
  131. /**
  132. * 显示消息对话框和确认
  133. */
  134. exports.messageShowConfirm = function () {
  135. const res = dialog.showMessageBoxSync({
  136. type: 'info',
  137. title: '自定义标题-message',
  138. message: '自定义消息内容',
  139. detail: '其它的额外信息',
  140. cancelId: 1, // 用于取消对话框的按钮的索引
  141. defaultId: 0, // 设置默认选中的按钮
  142. buttons: ['确认', '取消'], // 按钮及索引
  143. })
  144. console.log('[example] [messageShowConfirm] 结果:', res, res === 0 ? '点击确认按钮' : '点击取消按钮');
  145. return true;
  146. }
  147. function getElectronPath(filepath) {
  148. //filepath = path.resolve(filepath);
  149. filepath = filepath.replace("resources", "");
  150. filepath = filepath.replace("app.asar", "");
  151. filepath = path.normalize(filepath);
  152. return filepath;
  153. };