example.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. 'use strict';
  2. const BaseController = require('./base');
  3. const os = require('os');
  4. const fs = require('fs');
  5. const path = require('path');
  6. const Utils = require('ee-core').Utils;
  7. class ExampleController extends BaseController {
  8. /**
  9. * test electron api
  10. */
  11. async testElectronApi() {
  12. const { ctx, service } = this;
  13. const body = ctx.request.body;
  14. const id = body.id;
  15. const data = {};
  16. await service.example.testElectronApi(id);
  17. this.sendSuccess(data);
  18. }
  19. /**
  20. * test2
  21. */
  22. test2() {
  23. const { ctx, service } = this;
  24. const body = ctx.request.body;
  25. console.log('test2 params:', body);
  26. const data = {
  27. age: 32
  28. };
  29. this.sendSuccess(data);
  30. }
  31. async openLocalDir() {
  32. const self = this;
  33. const { ctx, service } = this;
  34. const body = ctx.request.body;
  35. const id = body.id;
  36. const data = {};
  37. let dir = '';
  38. switch (id) {
  39. case 'download' :
  40. dir = os.userInfo().homedir + '/Downloads';
  41. break;
  42. case 'picture' :
  43. dir = os.userInfo().homedir + '/Pictures';
  44. break;
  45. case 'doc' :
  46. dir = os.userInfo().homedir + '/Documents';
  47. break;
  48. case 'music' :
  49. dir = os.userInfo().homedir + '/Music';
  50. break;
  51. }
  52. await service.example.openLocalDir(dir);
  53. self.sendSuccess(data);
  54. }
  55. async executeJS() {
  56. const self = this;
  57. const { ctx, service } = this;
  58. const body = ctx.request.body;
  59. const str = body.str;
  60. let data = await service.example.executeJS(str);
  61. self.sendSuccess(data);
  62. }
  63. /**
  64. * 上传文件
  65. */
  66. async uploadFile() {
  67. const self = this;
  68. const { ctx, service } = this;
  69. let tmpDir = Utils.getLogDir();
  70. const file = ctx.request.files[0];
  71. try {
  72. let tmpFile = fs.readFileSync(file.filepath)
  73. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  74. } finally {
  75. await fs.unlink(file.filepath, function(){});
  76. }
  77. const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  78. const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  79. self.sendData(uploadRes);
  80. }
  81. async uploadExtension() {
  82. const self = this;
  83. const { ctx, service } = this;
  84. const data = {};
  85. let tmpDir = service.storage.getStorageDir();
  86. const file = ctx.request.files[0];
  87. this.app.logger.info('file:', file);
  88. try {
  89. let tmpFile = fs.readFileSync(file.filepath)
  90. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  91. } finally {
  92. await fs.unlink(file.filepath, function(){});
  93. }
  94. const filePath = path.join(tmpDir, file.filename);
  95. await service.example.loadExtension(filePath);
  96. self.sendData(data);
  97. }
  98. async setShortcut() {
  99. const self = this;
  100. const { ctx, service } = this;
  101. const shortcutObj = ctx.request.body;
  102. const data = {};
  103. if (!shortcutObj['id'] || !shortcutObj['name'] || !shortcutObj['cmd']) {
  104. self.sendFail({}, 'param error', 100);
  105. return;
  106. }
  107. await service.example.setShortcut(shortcutObj);
  108. self.sendSuccess(data);
  109. }
  110. async dbOperation() {
  111. const self = this;
  112. const { ctx, service } = this;
  113. const paramsObj = ctx.request.body;
  114. const data = {
  115. action: paramsObj.action,
  116. result: null,
  117. all_list: []
  118. };
  119. switch (paramsObj.action) {
  120. case 'add' :
  121. data.result = await service.storage.addTestData(paramsObj.info);;
  122. break;
  123. case 'del' :
  124. data.result = await service.storage.delTestData(paramsObj.delete_name);;
  125. break;
  126. case 'update' :
  127. data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age);
  128. break;
  129. case 'get' :
  130. data.result = await service.storage.getTestData(paramsObj.search_age);
  131. break;
  132. }
  133. data.all_list = await service.storage.getAllTestData();
  134. self.sendSuccess(data);
  135. }
  136. async addTestData() {
  137. const self = this;
  138. const { service } = this;
  139. const data = {};
  140. const userInfo = {
  141. name: 'jame',
  142. age: 18,
  143. gender: 'man'
  144. }
  145. await service.storage.addTestData(userInfo);
  146. self.sendSuccess(data);
  147. }
  148. async delTestData() {
  149. const self = this;
  150. const { service } = this;
  151. const data = {};
  152. const name = 'jame';
  153. await service.storage.delTestData(name);
  154. self.sendSuccess(data);
  155. }
  156. async updateTestData() {
  157. const self = this;
  158. const { service } = this;
  159. const data = {};
  160. const name = 'jame';
  161. const age = 20;
  162. await service.storage.updateTestData(name, age);
  163. self.sendSuccess(data);
  164. }
  165. async getTestData() {
  166. const self = this;
  167. const { service } = this;
  168. const data = {};
  169. const name = 'jame';
  170. const user = await service.storage.getTestData(name);
  171. data.user = user;
  172. self.sendSuccess(data);
  173. }
  174. async autoLaunchEnable() {
  175. const { service } = this;
  176. await service.example.autoLaunchEnable();
  177. const data = {
  178. isEnabled: true
  179. };
  180. this.sendSuccess(data);
  181. }
  182. async autoLaunchDisable() {
  183. const { service } = this;
  184. await service.example.autoLaunchDisable();
  185. const data = {
  186. isEnabled: false
  187. };
  188. this.sendSuccess(data);
  189. }
  190. async autoLaunchIsEnabled() {
  191. const { service } = this;
  192. const data = {
  193. isEnabled: null
  194. };
  195. const isEnabled = await service.example.autoLaunchIsEnabled();
  196. data.isEnabled = isEnabled;
  197. this.sendSuccess(data);
  198. }
  199. /**
  200. * 调用其它程序
  201. */
  202. async openSoftware() {
  203. const { service } = this;
  204. const data = {};
  205. const openResult = await service.example.openSoftware('powershell.exe');
  206. if (!openResult) {
  207. this.sendFail({}, '程序不存在', 100);
  208. return;
  209. }
  210. this.sendSuccess(data);
  211. }
  212. /**
  213. * 选择文件夹目录
  214. */
  215. async selectFileDir() {
  216. const { service } = this;
  217. const data = {
  218. dir: ''
  219. };
  220. const dir = await service.example.selectDir();
  221. data.dir = dir;
  222. this.sendSuccess(data);
  223. }
  224. /**
  225. * 显示消息对话框
  226. */
  227. async messageShow() {
  228. const { service } = this;
  229. const data = {};
  230. await service.example.messageShow();
  231. this.sendSuccess(data);
  232. }
  233. /**
  234. * 显示消息对话框和确认
  235. */
  236. async messageShowConfirm() {
  237. const { service } = this;
  238. const data = {};
  239. await service.example.messageShowConfirm();
  240. this.sendSuccess(data);
  241. }
  242. }
  243. module.exports = ExampleController;