example.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 getWsUrl() {
  111. const self = this;
  112. const { service } = this;
  113. const data = {};
  114. const addr = await service.socket.getWsUrl();
  115. data.url = addr;
  116. self.sendSuccess(data);
  117. }
  118. async dbOperation() {
  119. const self = this;
  120. const { ctx, service } = this;
  121. const paramsObj = ctx.request.body;
  122. const data = {
  123. action: paramsObj.action,
  124. result: null,
  125. all_list: []
  126. };
  127. switch (paramsObj.action) {
  128. case 'add' :
  129. data.result = await service.storage.addTestData(paramsObj.info);;
  130. break;
  131. case 'del' :
  132. data.result = await service.storage.delTestData(paramsObj.delete_name);;
  133. break;
  134. case 'update' :
  135. data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age);
  136. break;
  137. case 'get' :
  138. data.result = await service.storage.getTestData(paramsObj.search_age);
  139. break;
  140. }
  141. data.all_list = await service.storage.getAllTestData();
  142. self.sendSuccess(data);
  143. }
  144. async addTestData() {
  145. const self = this;
  146. const { service } = this;
  147. const data = {};
  148. const userInfo = {
  149. name: 'jame',
  150. age: 18,
  151. gender: 'man'
  152. }
  153. await service.storage.addTestData(userInfo);
  154. self.sendSuccess(data);
  155. }
  156. async delTestData() {
  157. const self = this;
  158. const { service } = this;
  159. const data = {};
  160. const name = 'jame';
  161. await service.storage.delTestData(name);
  162. self.sendSuccess(data);
  163. }
  164. async updateTestData() {
  165. const self = this;
  166. const { service } = this;
  167. const data = {};
  168. const name = 'jame';
  169. const age = 20;
  170. await service.storage.updateTestData(name, age);
  171. self.sendSuccess(data);
  172. }
  173. async getTestData() {
  174. const self = this;
  175. const { service } = this;
  176. const data = {};
  177. const name = 'jame';
  178. const user = await service.storage.getTestData(name);
  179. data.user = user;
  180. self.sendSuccess(data);
  181. }
  182. async autoLaunchEnable() {
  183. const { service } = this;
  184. await service.example.autoLaunchEnable();
  185. const data = {
  186. isEnabled: true
  187. };
  188. this.sendSuccess(data);
  189. }
  190. async autoLaunchDisable() {
  191. const { service } = this;
  192. await service.example.autoLaunchDisable();
  193. const data = {
  194. isEnabled: false
  195. };
  196. this.sendSuccess(data);
  197. }
  198. async autoLaunchIsEnabled() {
  199. const { service } = this;
  200. const data = {
  201. isEnabled: null
  202. };
  203. const isEnabled = await service.example.autoLaunchIsEnabled();
  204. data.isEnabled = isEnabled;
  205. this.sendSuccess(data);
  206. }
  207. /**
  208. * 调用其它程序
  209. */
  210. async openSoftware() {
  211. const { service } = this;
  212. const data = {};
  213. const openResult = await service.example.openSoftware('powershell.exe');
  214. if (!openResult) {
  215. this.sendFail({}, '程序不存在', 100);
  216. return;
  217. }
  218. this.sendSuccess(data);
  219. }
  220. /**
  221. * 选择文件夹目录
  222. */
  223. async selectFileDir() {
  224. const { service } = this;
  225. const data = {
  226. dir: ''
  227. };
  228. const dir = await service.example.selectDir();
  229. data.dir = dir;
  230. this.sendSuccess(data);
  231. }
  232. /**
  233. * 显示消息对话框
  234. */
  235. async messageShow() {
  236. const { service } = this;
  237. const data = {};
  238. await service.example.messageShow();
  239. this.sendSuccess(data);
  240. }
  241. /**
  242. * 显示消息对话框和确认
  243. */
  244. async messageShowConfirm() {
  245. const { service } = this;
  246. const data = {};
  247. await service.example.messageShowConfirm();
  248. this.sendSuccess(data);
  249. }
  250. }
  251. module.exports = ExampleController;