example.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 test1() {
  11. const { ctx, service } = this;
  12. const body = ctx.request.body;
  13. const id = body.id;
  14. const data = {};
  15. await service.example.test1(id);
  16. this.sendSuccess(data);
  17. }
  18. /**
  19. * 上传文件
  20. */
  21. async uploadFile() {
  22. const self = this;
  23. const { ctx, service } = this;
  24. let tmpDir = Utils.getLogDir();
  25. const file = ctx.request.files[0];
  26. try {
  27. let tmpFile = fs.readFileSync(file.filepath)
  28. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  29. } finally {
  30. await fs.unlink(file.filepath, function(){});
  31. }
  32. const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  33. const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  34. self.sendData(uploadRes);
  35. }
  36. async uploadExtension() {
  37. const self = this;
  38. const { ctx, service } = this;
  39. const data = {};
  40. let tmpDir = Utils.getLogDir();
  41. const file = ctx.request.files[0];
  42. this.app.logger.info('file:', file);
  43. try {
  44. let tmpFile = fs.readFileSync(file.filepath)
  45. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  46. } finally {
  47. await fs.unlink(file.filepath, function(){});
  48. }
  49. const filePath = path.join(tmpDir, file.filename);
  50. await service.example.loadExtension(filePath);
  51. self.sendData(data);
  52. }
  53. /**
  54. * json数据库操作
  55. */
  56. async dbOperation() {
  57. const self = this;
  58. const { ctx, service } = this;
  59. const paramsObj = ctx.request.body;
  60. const data = {
  61. action: paramsObj.action,
  62. result: null,
  63. all_list: []
  64. };
  65. switch (paramsObj.action) {
  66. case 'add' :
  67. data.result = await service.storage.addTestData(paramsObj.info);;
  68. break;
  69. case 'del' :
  70. data.result = await service.storage.delTestData(paramsObj.delete_name);;
  71. break;
  72. case 'update' :
  73. data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age);
  74. break;
  75. case 'get' :
  76. data.result = await service.storage.getTestData(paramsObj.search_age);
  77. break;
  78. }
  79. data.all_list = await service.storage.getAllTestData();
  80. self.sendSuccess(data);
  81. }
  82. /**
  83. * 显示消息对话框
  84. */
  85. async messageShow() {
  86. const { service } = this;
  87. const data = {};
  88. await service.example.messageShow();
  89. this.sendSuccess(data);
  90. }
  91. /**
  92. * 显示消息对话框和确认
  93. */
  94. async messageShowConfirm() {
  95. const { service } = this;
  96. const data = {};
  97. await service.example.messageShowConfirm();
  98. this.sendSuccess(data);
  99. }
  100. }
  101. module.exports = ExampleController;