example.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. const BaseController = require('./base');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const Utils = require('ee-core').Utils;
  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. /**
  31. * 上传文件
  32. */
  33. async uploadFile() {
  34. const self = this;
  35. const { ctx, service } = this;
  36. let tmpDir = Utils.getLogDir();
  37. const file = ctx.request.files[0];
  38. try {
  39. let tmpFile = fs.readFileSync(file.filepath)
  40. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  41. } finally {
  42. await fs.unlink(file.filepath, function(){});
  43. }
  44. const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  45. const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  46. self.sendData(uploadRes);
  47. }
  48. async uploadExtension() {
  49. const self = this;
  50. const { ctx, service } = this;
  51. const data = {};
  52. let tmpDir = Utils.getLogDir();
  53. const file = ctx.request.files[0];
  54. this.app.logger.info('file:', file);
  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 filePath = path.join(tmpDir, file.filename);
  62. await service.example.loadExtension(filePath);
  63. self.sendData(data);
  64. }
  65. /**
  66. * json数据库操作
  67. */
  68. async dbOperation() {
  69. const self = this;
  70. const { ctx, service } = this;
  71. const paramsObj = ctx.request.body;
  72. const data = {
  73. action: paramsObj.action,
  74. result: null,
  75. all_list: []
  76. };
  77. switch (paramsObj.action) {
  78. case 'add' :
  79. data.result = await service.storage.addTestData(paramsObj.info);;
  80. break;
  81. case 'del' :
  82. data.result = await service.storage.delTestData(paramsObj.delete_name);;
  83. break;
  84. case 'update' :
  85. data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age);
  86. break;
  87. case 'get' :
  88. data.result = await service.storage.getTestData(paramsObj.search_age);
  89. break;
  90. }
  91. data.all_list = await service.storage.getAllTestData();
  92. self.sendSuccess(data);
  93. }
  94. /**
  95. * 显示消息对话框
  96. */
  97. async messageShow() {
  98. const { service } = this;
  99. const data = {};
  100. await service.example.messageShow();
  101. this.sendSuccess(data);
  102. }
  103. /**
  104. * 显示消息对话框和确认
  105. */
  106. async messageShowConfirm() {
  107. const { service } = this;
  108. const data = {};
  109. await service.example.messageShowConfirm();
  110. this.sendSuccess(data);
  111. }
  112. }
  113. module.exports = ExampleController;