example.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // for (const file of ctx.request.files) {
  55. // this.app.logger.info('file:', file);
  56. // try {
  57. // let tmpFile = fs.readFileSync(file.filepath)
  58. // fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  59. // } finally {
  60. // await fs.unlink(file.filepath, function(){});
  61. // }
  62. // const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  63. // const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  64. // }
  65. const file = ctx.request.files[0];
  66. //this.app.logger.info('file:', file);
  67. try {
  68. let tmpFile = fs.readFileSync(file.filepath)
  69. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  70. } finally {
  71. await fs.unlink(file.filepath, function(){});
  72. }
  73. const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  74. const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  75. self.sendData(uploadRes);
  76. }
  77. async setShortcut() {
  78. const self = this;
  79. const { ctx, service } = this;
  80. const shortcutObj = ctx.request.body;
  81. const data = {};
  82. if (!shortcutObj['id'] || !shortcutObj['name'] || !shortcutObj['cmd']) {
  83. self.sendFail({}, 'param error', 100);
  84. return;
  85. }
  86. await service.example.setShortcut(shortcutObj);
  87. self.sendSuccess(data);
  88. }
  89. async getWsUrl() {
  90. const self = this;
  91. const { service } = this;
  92. const data = {};
  93. const addr = await service.socket.getWsUrl();
  94. data.url = addr;
  95. self.sendSuccess(data);
  96. }
  97. async addTestData() {
  98. const self = this;
  99. const { service } = this;
  100. const data = {};
  101. const userInfo = {
  102. name: 'jame',
  103. age: 18,
  104. gender: 'man'
  105. }
  106. await service.storage.addTestData(userInfo);
  107. self.sendSuccess(data);
  108. }
  109. async delTestData() {
  110. const self = this;
  111. const { service } = this;
  112. const data = {};
  113. const name = 'jame';
  114. await service.storage.delTestData(name);
  115. self.sendSuccess(data);
  116. }
  117. async updateTestData() {
  118. const self = this;
  119. const { service } = this;
  120. const data = {};
  121. const name = 'jame';
  122. const age = 20;
  123. await service.storage.updateTestData(name, age);
  124. self.sendSuccess(data);
  125. }
  126. async getTestData() {
  127. const self = this;
  128. const { service } = this;
  129. const data = {};
  130. const name = 'jame';
  131. const user = await service.storage.getTestData(name);
  132. data.user = user;
  133. self.sendSuccess(data);
  134. }
  135. async autoLaunchEnable() {
  136. const { service } = this;
  137. await service.example.autoLaunchEnable();
  138. const data = {
  139. isEnabled: true
  140. };
  141. this.sendSuccess(data);
  142. }
  143. async autoLaunchDisable() {
  144. const { service } = this;
  145. await service.example.autoLaunchDisable();
  146. const data = {
  147. isEnabled: false
  148. };
  149. this.sendSuccess(data);
  150. }
  151. async autoLaunchIsEnabled() {
  152. const { service } = this;
  153. const data = {
  154. isEnabled: null
  155. };
  156. const isEnabled = await service.example.autoLaunchIsEnabled();
  157. data.isEnabled = isEnabled;
  158. this.sendSuccess(data);
  159. }
  160. /**
  161. * 调用其它程序
  162. */
  163. async openSoftware() {
  164. const { service } = this;
  165. const data = {};
  166. const openResult = await service.example.openSoftware('powershell.exe');
  167. if (!openResult) {
  168. this.sendFail({}, '程序不存在', 100);
  169. return;
  170. }
  171. this.sendSuccess(data);
  172. }
  173. /**
  174. * 选择文件夹目录
  175. */
  176. async selectFileDir() {
  177. const { service } = this;
  178. const data = {
  179. dir: ''
  180. };
  181. const dir = await service.example.selectDir();
  182. data.dir = dir;
  183. this.sendSuccess(data);
  184. }
  185. /**
  186. * 显示消息对话框
  187. */
  188. async messageShow() {
  189. const { service } = this;
  190. const data = {};
  191. await service.example.messageShow();
  192. this.sendSuccess(data);
  193. }
  194. /**
  195. * 显示消息对话框和确认
  196. */
  197. async messageShowConfirm() {
  198. const { service } = this;
  199. const data = {};
  200. await service.example.messageShowConfirm();
  201. this.sendSuccess(data);
  202. }
  203. }
  204. module.exports = ExampleController;