example.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. async executeJS() {
  31. const self = this;
  32. const { ctx, service } = this;
  33. const body = ctx.request.body;
  34. const str = body.str;
  35. let data = await service.example.executeJS(str);
  36. self.sendSuccess(data);
  37. }
  38. /**
  39. * 上传文件
  40. */
  41. async uploadFile() {
  42. const self = this;
  43. const { ctx, service } = this;
  44. let tmpDir = Utils.getLogDir();
  45. const file = ctx.request.files[0];
  46. try {
  47. let tmpFile = fs.readFileSync(file.filepath)
  48. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  49. } finally {
  50. await fs.unlink(file.filepath, function(){});
  51. }
  52. const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  53. const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  54. self.sendData(uploadRes);
  55. }
  56. async uploadExtension() {
  57. const self = this;
  58. const { ctx, service } = this;
  59. const data = {};
  60. let tmpDir = service.storage.getStorageDir();
  61. const file = ctx.request.files[0];
  62. this.app.logger.info('file:', file);
  63. try {
  64. let tmpFile = fs.readFileSync(file.filepath)
  65. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  66. } finally {
  67. await fs.unlink(file.filepath, function(){});
  68. }
  69. const filePath = path.join(tmpDir, file.filename);
  70. await service.example.loadExtension(filePath);
  71. self.sendData(data);
  72. }
  73. async dbOperation() {
  74. const self = this;
  75. const { ctx, service } = this;
  76. const paramsObj = ctx.request.body;
  77. const data = {
  78. action: paramsObj.action,
  79. result: null,
  80. all_list: []
  81. };
  82. switch (paramsObj.action) {
  83. case 'add' :
  84. data.result = await service.storage.addTestData(paramsObj.info);;
  85. break;
  86. case 'del' :
  87. data.result = await service.storage.delTestData(paramsObj.delete_name);;
  88. break;
  89. case 'update' :
  90. data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age);
  91. break;
  92. case 'get' :
  93. data.result = await service.storage.getTestData(paramsObj.search_age);
  94. break;
  95. }
  96. data.all_list = await service.storage.getAllTestData();
  97. self.sendSuccess(data);
  98. }
  99. async addTestData() {
  100. const self = this;
  101. const { service } = this;
  102. const data = {};
  103. const userInfo = {
  104. name: 'jame',
  105. age: 18,
  106. gender: 'man'
  107. }
  108. await service.storage.addTestData(userInfo);
  109. self.sendSuccess(data);
  110. }
  111. async delTestData() {
  112. const self = this;
  113. const { service } = this;
  114. const data = {};
  115. const name = 'jame';
  116. await service.storage.delTestData(name);
  117. self.sendSuccess(data);
  118. }
  119. async updateTestData() {
  120. const self = this;
  121. const { service } = this;
  122. const data = {};
  123. const name = 'jame';
  124. const age = 20;
  125. await service.storage.updateTestData(name, age);
  126. self.sendSuccess(data);
  127. }
  128. async getTestData() {
  129. const self = this;
  130. const { service } = this;
  131. const data = {};
  132. const name = 'jame';
  133. const user = await service.storage.getTestData(name);
  134. data.user = user;
  135. self.sendSuccess(data);
  136. }
  137. async autoLaunchEnable() {
  138. const { service } = this;
  139. await service.example.autoLaunchEnable();
  140. const data = {
  141. isEnabled: true
  142. };
  143. this.sendSuccess(data);
  144. }
  145. async autoLaunchDisable() {
  146. const { service } = this;
  147. await service.example.autoLaunchDisable();
  148. const data = {
  149. isEnabled: false
  150. };
  151. this.sendSuccess(data);
  152. }
  153. async autoLaunchIsEnabled() {
  154. const { service } = this;
  155. const data = {
  156. isEnabled: null
  157. };
  158. const isEnabled = await service.example.autoLaunchIsEnabled();
  159. data.isEnabled = isEnabled;
  160. this.sendSuccess(data);
  161. }
  162. /**
  163. * 调用其它程序
  164. */
  165. async openSoftware() {
  166. const { service } = this;
  167. const data = {};
  168. const openResult = await service.example.openSoftware('powershell.exe');
  169. if (!openResult) {
  170. this.sendFail({}, '程序不存在', 100);
  171. return;
  172. }
  173. this.sendSuccess(data);
  174. }
  175. /**
  176. * 显示消息对话框
  177. */
  178. async messageShow() {
  179. const { service } = this;
  180. const data = {};
  181. await service.example.messageShow();
  182. this.sendSuccess(data);
  183. }
  184. /**
  185. * 显示消息对话框和确认
  186. */
  187. async messageShowConfirm() {
  188. const { service } = this;
  189. const data = {};
  190. await service.example.messageShowConfirm();
  191. this.sendSuccess(data);
  192. }
  193. }
  194. module.exports = ExampleController;