example.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  74. await service.example.setShortcut(shortcutObj);
  75. self.sendSuccess(data);
  76. }
  77. async getWsUrl() {
  78. const self = this;
  79. const { service } = this;
  80. const data = {};
  81. const addr = await service.socket.getWsUrl();
  82. data.url = addr;
  83. self.sendSuccess(data);
  84. }
  85. async addTestData() {
  86. const self = this;
  87. const { service } = this;
  88. const data = {};
  89. const userInfo = {
  90. name: 'jame',
  91. age: 18,
  92. gender: 'man'
  93. }
  94. await service.storage.addTestData(userInfo);
  95. self.sendSuccess(data);
  96. }
  97. async delTestData() {
  98. const self = this;
  99. const { service } = this;
  100. const data = {};
  101. const name = 'jame';
  102. await service.storage.delTestData(name);
  103. self.sendSuccess(data);
  104. }
  105. async updateTestData() {
  106. const self = this;
  107. const { service } = this;
  108. const data = {};
  109. const name = 'jame';
  110. const age = 20;
  111. await service.storage.updateTestData(name, age);
  112. self.sendSuccess(data);
  113. }
  114. async getTestData() {
  115. const self = this;
  116. const { service } = this;
  117. const data = {};
  118. const name = 'jame';
  119. const user = await service.storage.getTestData(name);
  120. data.user = user;
  121. self.sendSuccess(data);
  122. }
  123. }
  124. module.exports = ExampleController;