example.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. async dbOperation() {
  66. const self = this;
  67. const { ctx, service } = this;
  68. const paramsObj = ctx.request.body;
  69. const data = {
  70. action: paramsObj.action,
  71. result: null,
  72. all_list: []
  73. };
  74. switch (paramsObj.action) {
  75. case 'add' :
  76. data.result = await service.storage.addTestData(paramsObj.info);;
  77. break;
  78. case 'del' :
  79. data.result = await service.storage.delTestData(paramsObj.delete_name);;
  80. break;
  81. case 'update' :
  82. data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age);
  83. break;
  84. case 'get' :
  85. data.result = await service.storage.getTestData(paramsObj.search_age);
  86. break;
  87. }
  88. data.all_list = await service.storage.getAllTestData();
  89. self.sendSuccess(data);
  90. }
  91. async addTestData() {
  92. const self = this;
  93. const { service } = this;
  94. const data = {};
  95. const userInfo = {
  96. name: 'jame',
  97. age: 18,
  98. gender: 'man'
  99. }
  100. await service.storage.addTestData(userInfo);
  101. self.sendSuccess(data);
  102. }
  103. async delTestData() {
  104. const self = this;
  105. const { service } = this;
  106. const data = {};
  107. const name = 'jame';
  108. await service.storage.delTestData(name);
  109. self.sendSuccess(data);
  110. }
  111. async updateTestData() {
  112. const self = this;
  113. const { service } = this;
  114. const data = {};
  115. const name = 'jame';
  116. const age = 20;
  117. await service.storage.updateTestData(name, age);
  118. self.sendSuccess(data);
  119. }
  120. async getTestData() {
  121. const self = this;
  122. const { service } = this;
  123. const data = {};
  124. const name = 'jame';
  125. const user = await service.storage.getTestData(name);
  126. data.user = user;
  127. self.sendSuccess(data);
  128. }
  129. async autoLaunchEnable() {
  130. const { service } = this;
  131. await service.example.autoLaunchEnable();
  132. const data = {
  133. isEnabled: true
  134. };
  135. this.sendSuccess(data);
  136. }
  137. async autoLaunchDisable() {
  138. const { service } = this;
  139. await service.example.autoLaunchDisable();
  140. const data = {
  141. isEnabled: false
  142. };
  143. this.sendSuccess(data);
  144. }
  145. async autoLaunchIsEnabled() {
  146. const { service } = this;
  147. const data = {
  148. isEnabled: null
  149. };
  150. const isEnabled = await service.example.autoLaunchIsEnabled();
  151. data.isEnabled = isEnabled;
  152. this.sendSuccess(data);
  153. }
  154. /**
  155. * 调用其它程序
  156. */
  157. async openSoftware() {
  158. const { service } = this;
  159. const data = {};
  160. const openResult = await service.example.openSoftware('powershell.exe');
  161. if (!openResult) {
  162. this.sendFail({}, '程序不存在', 100);
  163. return;
  164. }
  165. this.sendSuccess(data);
  166. }
  167. /**
  168. * 显示消息对话框
  169. */
  170. async messageShow() {
  171. const { service } = this;
  172. const data = {};
  173. await service.example.messageShow();
  174. this.sendSuccess(data);
  175. }
  176. /**
  177. * 显示消息对话框和确认
  178. */
  179. async messageShowConfirm() {
  180. const { service } = this;
  181. const data = {};
  182. await service.example.messageShowConfirm();
  183. this.sendSuccess(data);
  184. }
  185. }
  186. module.exports = ExampleController;