example.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 addTestData() {
  103. const self = this;
  104. const { service } = this;
  105. const data = {};
  106. const userInfo = {
  107. name: 'jame',
  108. age: 18,
  109. gender: 'man'
  110. }
  111. await service.storage.addTestData(userInfo);
  112. self.sendSuccess(data);
  113. }
  114. async delTestData() {
  115. const self = this;
  116. const { service } = this;
  117. const data = {};
  118. const name = 'jame';
  119. await service.storage.delTestData(name);
  120. self.sendSuccess(data);
  121. }
  122. async updateTestData() {
  123. const self = this;
  124. const { service } = this;
  125. const data = {};
  126. const name = 'jame';
  127. const age = 20;
  128. await service.storage.updateTestData(name, age);
  129. self.sendSuccess(data);
  130. }
  131. async getTestData() {
  132. const self = this;
  133. const { service } = this;
  134. const data = {};
  135. const name = 'jame';
  136. const user = await service.storage.getTestData(name);
  137. data.user = user;
  138. self.sendSuccess(data);
  139. }
  140. async autoLaunchEnable() {
  141. const { service } = this;
  142. await service.example.autoLaunchEnable();
  143. const data = {
  144. isEnabled: true
  145. };
  146. this.sendSuccess(data);
  147. }
  148. async autoLaunchDisable() {
  149. const { service } = this;
  150. await service.example.autoLaunchDisable();
  151. const data = {
  152. isEnabled: false
  153. };
  154. this.sendSuccess(data);
  155. }
  156. async autoLaunchIsEnabled() {
  157. const { service } = this;
  158. const data = {
  159. isEnabled: null
  160. };
  161. const isEnabled = await service.example.autoLaunchIsEnabled();
  162. data.isEnabled = isEnabled;
  163. this.sendSuccess(data);
  164. }
  165. /**
  166. * 调用其它程序
  167. */
  168. async openSoftware() {
  169. const { service } = this;
  170. const data = {};
  171. const openResult = await service.example.openSoftware('powershell.exe');
  172. if (!openResult) {
  173. this.sendFail({}, '程序不存在', 100);
  174. return;
  175. }
  176. this.sendSuccess(data);
  177. }
  178. /**
  179. * 选择文件夹目录
  180. */
  181. async selectFileDir() {
  182. const { service } = this;
  183. const data = {
  184. dir: ''
  185. };
  186. const dir = await service.example.selectDir();
  187. data.dir = dir;
  188. this.sendSuccess(data);
  189. }
  190. /**
  191. * 显示消息对话框
  192. */
  193. async messageShow() {
  194. const { service } = this;
  195. const data = {};
  196. await service.example.messageShow();
  197. this.sendSuccess(data);
  198. }
  199. /**
  200. * 显示消息对话框和确认
  201. */
  202. async messageShowConfirm() {
  203. const { service } = this;
  204. const data = {};
  205. await service.example.messageShowConfirm();
  206. this.sendSuccess(data);
  207. }
  208. }
  209. module.exports = ExampleController;