example.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 'use strict';
  2. const _ = require('lodash');
  3. const path = require('path');
  4. const is = require('electron-is');
  5. const Controller = require('ee-core').Controller;
  6. const electronApp = require('electron').app;
  7. const {dialog, webContents, shell, BrowserWindow, BrowserView, Notification, powerMonitor, screen, nativeTheme} = require('electron');
  8. let myTimer = null;
  9. let browserViewObj = null;
  10. let notificationObj = null;
  11. /**
  12. * 示例控制器
  13. * @class
  14. */
  15. class ExampleController extends Controller {
  16. /**
  17. * 所有方法接收两个参数
  18. * args 前端传的参数
  19. * @param event - IpcMainEvent 文档:https://www.electronjs.org/docs/latest/api/structures/ipc-main-event
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. //this.myTimer = null;
  24. }
  25. /**
  26. * test
  27. */
  28. async test (args) {
  29. let obj = {
  30. status:'ok'
  31. }
  32. // 调用egg的某个api
  33. // const result = await this.app.curlEgg('post', '/api/v1/example/test2', {name: 'gsx2'});
  34. // console.log('fffffffffff: ', result);
  35. //this.app.logger.info('ssssssssssssssssssss');
  36. return obj;
  37. }
  38. /**
  39. * hello
  40. */
  41. hello (args) {
  42. let newMsg = args + " +1";
  43. let content = '';
  44. content = '收到:' + args + ',返回:' + newMsg;
  45. // let channel = "example.socketMessageStop";
  46. // event.reply(`${channel}`, '另外的数据');
  47. return content;
  48. }
  49. /**
  50. * 消息提示对话框
  51. */
  52. messageShow () {
  53. dialog.showMessageBoxSync({
  54. type: 'info', // "none", "info", "error", "question" 或者 "warning"
  55. title: '自定义标题-message',
  56. message: '自定义消息内容',
  57. detail: '其它的额外信息'
  58. })
  59. return '打开了消息框';
  60. }
  61. /**
  62. * 消息提示与确认对话框
  63. */
  64. messageShowConfirm () {
  65. const res = dialog.showMessageBoxSync({
  66. type: 'info',
  67. title: '自定义标题-message',
  68. message: '自定义消息内容',
  69. detail: '其它的额外信息',
  70. cancelId: 1, // 用于取消对话框的按钮的索引
  71. defaultId: 0, // 设置默认选中的按钮
  72. buttons: ['确认', '取消'], // 按钮及索引
  73. })
  74. let data = (res === 0) ? '点击确认按钮' : '点击取消按钮';
  75. return data;
  76. }
  77. /**
  78. * 选择目录
  79. */
  80. selectFolder () {
  81. const filePaths = dialog.showOpenDialogSync({
  82. properties: ['openDirectory', 'createDirectory']
  83. });
  84. if (_.isEmpty(filePaths)) {
  85. return null
  86. }
  87. return filePaths[0];
  88. }
  89. /**
  90. * 打开目录
  91. */
  92. openDirectory (args) {
  93. if (!args.id) {
  94. return false;
  95. }
  96. const dir = electronApp.getPath(args.id);
  97. shell.openPath(dir);
  98. return true;
  99. }
  100. /**
  101. * 长消息 - 开始
  102. */
  103. socketMessageStart (args, event) {
  104. // 每隔1秒,向前端页面发送消息
  105. // 用定时器模拟
  106. // 前端ipc频道 channel
  107. const channel = 'controller.example.socketMessageStart';
  108. myTimer = setInterval(function(e, c, msg) {
  109. let timeNow = Date.now();
  110. let data = msg + ':' + timeNow;
  111. e.reply(`${c}`, data)
  112. }, 1000, event, channel, args)
  113. return '开始了'
  114. }
  115. /**
  116. * 长消息 - 停止
  117. */
  118. socketMessageStop () {
  119. clearInterval(myTimer);
  120. return '停止了'
  121. }
  122. /**
  123. * 执行js语句
  124. */
  125. executeJS (args) {
  126. let jscode = `(()=>{alert('${args}');return 'fromJs:${args}';})()`;
  127. return webContents.fromId(1).executeJavaScript(jscode);
  128. }
  129. /**
  130. * 加载视图内容
  131. */
  132. loadViewContent (args) {
  133. let content = null;
  134. if (args.type == 'html') {
  135. content = path.join('file://', electronApp.getAppPath(), args.content)
  136. } else {
  137. content = args.content;
  138. }
  139. browserViewObj = new BrowserView();
  140. this.app.electron.mainWindow.setBrowserView(browserViewObj)
  141. browserViewObj.setBounds({
  142. x: 300,
  143. y: 170,
  144. width: 650,
  145. height: 400
  146. });
  147. browserViewObj.webContents.loadURL(content);
  148. return true
  149. }
  150. /**
  151. * 移除视图内容
  152. */
  153. removeViewContent () {
  154. this.app.electron.mainWindow.removeBrowserView(browserViewObj);
  155. return true
  156. }
  157. /**
  158. * 打开新窗口
  159. */
  160. createWindow (args) {
  161. let content = null;
  162. if (args.type == 'html') {
  163. content = path.join('file://', electronApp.getAppPath(), args.content)
  164. } else {
  165. content = args.content;
  166. }
  167. let winObj = new BrowserWindow({
  168. x: 10,
  169. y: 10,
  170. width: 980,
  171. height: 650
  172. })
  173. winObj.loadURL(content);
  174. return winObj.id
  175. }
  176. }
  177. module.exports = ExampleController;