example.js 6.3 KB

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