example.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. async openLocalDir() {
  8. const self = this;
  9. const { ctx, service } = this;
  10. const body = ctx.request.body;
  11. const id = body.id;
  12. const data = {};
  13. let dir = '';
  14. switch (id) {
  15. case 'download' :
  16. dir = os.userInfo().homedir + '/Downloads';
  17. break;
  18. case 'picture' :
  19. dir = os.userInfo().homedir + '/Pictures';
  20. break;
  21. case 'doc' :
  22. dir = os.userInfo().homedir + '/Documents';
  23. break;
  24. case 'music' :
  25. dir = os.userInfo().homedir + '/Music';
  26. break;
  27. }
  28. await service.example.openLocalDir(dir);
  29. self.sendSuccess(data);
  30. }
  31. async executeJS() {
  32. const self = this;
  33. const { ctx, service } = this;
  34. const body = ctx.request.body;
  35. const str = body.str;
  36. let data = await service.example.executeJS(str);
  37. self.sendSuccess(data);
  38. }
  39. async uploadFile() {
  40. const self = this;
  41. const { ctx, service } = this;
  42. let tmpDir = service.storage.getStorageDir();
  43. // for (const file of ctx.request.files) {
  44. // this.app.logger.info('file:', file);
  45. // try {
  46. // let tmpFile = fs.readFileSync(file.filepath)
  47. // fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  48. // } finally {
  49. // await fs.unlink(file.filepath, function(){});
  50. // }
  51. // const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  52. // const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  53. // }
  54. const file = ctx.request.files[0];
  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. self.sendData(uploadRes);
  65. }
  66. async setShortcut() {
  67. const self = this;
  68. const { ctx, service } = this;
  69. const shortcutObj = ctx.request.body;
  70. const data = {};
  71. if (!shortcutObj['id'] || !shortcutObj['name'] || !shortcutObj['cmd']) {
  72. self.sendFail({}, 'param error', 100);
  73. return;
  74. }
  75. await service.example.setShortcut(shortcutObj);
  76. self.sendSuccess(data);
  77. }
  78. async getWsUrl() {
  79. const self = this;
  80. const { service } = this;
  81. const data = {};
  82. const addr = await service.socket.getWsUrl();
  83. data.url = addr;
  84. self.sendSuccess(data);
  85. }
  86. async addTestData() {
  87. const self = this;
  88. const { service } = this;
  89. const data = {};
  90. const userInfo = {
  91. name: 'jame',
  92. age: 18,
  93. gender: 'man'
  94. }
  95. await service.storage.addTestData(userInfo);
  96. self.sendSuccess(data);
  97. }
  98. async delTestData() {
  99. const self = this;
  100. const { service } = this;
  101. const data = {};
  102. const name = 'jame';
  103. await service.storage.delTestData(name);
  104. self.sendSuccess(data);
  105. }
  106. async updateTestData() {
  107. const self = this;
  108. const { service } = this;
  109. const data = {};
  110. const name = 'jame';
  111. const age = 20;
  112. await service.storage.updateTestData(name, age);
  113. self.sendSuccess(data);
  114. }
  115. async getTestData() {
  116. const self = this;
  117. const { service } = this;
  118. const data = {};
  119. const name = 'jame';
  120. const user = await service.storage.getTestData(name);
  121. data.user = user;
  122. self.sendSuccess(data);
  123. }
  124. async autoLaunchEnable() {
  125. const { service } = this;
  126. await service.example.autoLaunchEnable();
  127. const data = {
  128. isEnabled: true
  129. };
  130. this.sendSuccess(data);
  131. }
  132. async autoLaunchDisable() {
  133. const { service } = this;
  134. await service.example.autoLaunchDisable();
  135. const data = {
  136. isEnabled: false
  137. };
  138. this.sendSuccess(data);
  139. }
  140. async autoLaunchIsEnabled() {
  141. const { service } = this;
  142. const data = {
  143. isEnabled: null
  144. };
  145. const isEnabled = await service.example.autoLaunchIsEnabled();
  146. data.isEnabled = isEnabled;
  147. this.sendSuccess(data);
  148. }
  149. /**
  150. * 调用其它程序
  151. */
  152. async openSoftware() {
  153. const { service } = this;
  154. const data = {};
  155. const openResult = await service.example.openSoftware('powershell.exe');
  156. if (!openResult) {
  157. this.sendFail({}, '程序不存在', 100);
  158. return;
  159. }
  160. this.sendSuccess(data);
  161. }
  162. }
  163. module.exports = ExampleController;