example.js 6.5 KB

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