example.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. const BaseController = require('./base');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const Utils = require('ee-core').Utils;
  6. class ExampleController extends BaseController {
  7. /**
  8. * test electron api
  9. */
  10. async testElectronApi() {
  11. const { ctx, service } = this;
  12. const body = ctx.request.body;
  13. const id = body.id;
  14. const data = {};
  15. await service.example.testElectronApi(id);
  16. this.sendSuccess(data);
  17. }
  18. /**
  19. * test2
  20. */
  21. test2() {
  22. const { ctx, service } = this;
  23. const body = ctx.request.body;
  24. console.log('test2 params:', body);
  25. const data = {
  26. age: 32
  27. };
  28. this.sendSuccess(data);
  29. }
  30. /**
  31. * 上传文件
  32. */
  33. async uploadFile() {
  34. const self = this;
  35. const { ctx, service } = this;
  36. let tmpDir = Utils.getLogDir();
  37. const file = ctx.request.files[0];
  38. try {
  39. let tmpFile = fs.readFileSync(file.filepath)
  40. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  41. } finally {
  42. await fs.unlink(file.filepath, function(){});
  43. }
  44. const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  45. const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  46. self.sendData(uploadRes);
  47. }
  48. async uploadExtension() {
  49. const self = this;
  50. const { ctx, service } = this;
  51. const data = {};
  52. let tmpDir = service.storage.getStorageDir();
  53. const file = ctx.request.files[0];
  54. this.app.logger.info('file:', file);
  55. try {
  56. let tmpFile = fs.readFileSync(file.filepath)
  57. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  58. } finally {
  59. await fs.unlink(file.filepath, function(){});
  60. }
  61. const filePath = path.join(tmpDir, file.filename);
  62. await service.example.loadExtension(filePath);
  63. self.sendData(data);
  64. }
  65. /**
  66. * json数据库操作
  67. */
  68. async dbOperation() {
  69. const self = this;
  70. const { ctx, service } = this;
  71. const paramsObj = ctx.request.body;
  72. const data = {
  73. action: paramsObj.action,
  74. result: null,
  75. all_list: []
  76. };
  77. switch (paramsObj.action) {
  78. case 'add' :
  79. data.result = await service.storage.addTestData(paramsObj.info);;
  80. break;
  81. case 'del' :
  82. data.result = await service.storage.delTestData(paramsObj.delete_name);;
  83. break;
  84. case 'update' :
  85. data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age);
  86. break;
  87. case 'get' :
  88. data.result = await service.storage.getTestData(paramsObj.search_age);
  89. break;
  90. }
  91. data.all_list = await service.storage.getAllTestData();
  92. self.sendSuccess(data);
  93. }
  94. async autoLaunchEnable() {
  95. const { service } = this;
  96. await service.example.autoLaunchEnable();
  97. const data = {
  98. isEnabled: true
  99. };
  100. this.sendSuccess(data);
  101. }
  102. async autoLaunchDisable() {
  103. const { service } = this;
  104. await service.example.autoLaunchDisable();
  105. const data = {
  106. isEnabled: false
  107. };
  108. this.sendSuccess(data);
  109. }
  110. async autoLaunchIsEnabled() {
  111. const { service } = this;
  112. const data = {
  113. isEnabled: null
  114. };
  115. const isEnabled = await service.example.autoLaunchIsEnabled();
  116. data.isEnabled = isEnabled;
  117. this.sendSuccess(data);
  118. }
  119. /**
  120. * 调用其它程序
  121. */
  122. async openSoftware() {
  123. const { service } = this;
  124. const data = {};
  125. const openResult = await service.example.openSoftware('powershell.exe');
  126. if (!openResult) {
  127. this.sendFail({}, '程序不存在', 100);
  128. return;
  129. }
  130. this.sendSuccess(data);
  131. }
  132. /**
  133. * 显示消息对话框
  134. */
  135. async messageShow() {
  136. const { service } = this;
  137. const data = {};
  138. await service.example.messageShow();
  139. this.sendSuccess(data);
  140. }
  141. /**
  142. * 显示消息对话框和确认
  143. */
  144. async messageShowConfirm() {
  145. const { service } = this;
  146. const data = {};
  147. await service.example.messageShowConfirm();
  148. this.sendSuccess(data);
  149. }
  150. }
  151. module.exports = ExampleController;