example.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const EeSocket = require('ee-core').Socket.EeSocket;
  4. const socketClient = EeSocket.getClient();
  5. class ExampleService extends Service {
  6. async testElectronApi(id = 0) {
  7. const res = await socketClient.call('controller.example.test', {name:"gsx"}, {age:12});
  8. return null;
  9. }
  10. async openLocalDir(dir) {
  11. await socketClient.call('controller.example.openDir', dir);
  12. return true;
  13. }
  14. async executeJS(str) {
  15. let result = await socketClient.call('controller.example.executeJS', str);
  16. return result;
  17. }
  18. async setShortcut(shortcutStr) {
  19. let result = await socketClient.call('controller.example.setShortcut', shortcutStr);
  20. return result;
  21. }
  22. async uploadFileToSMMS(tmpFile) {
  23. const res = {
  24. code: 1000,
  25. message: 'unknown error',
  26. };
  27. try {
  28. //throw new Error('Sync Error');
  29. const headersObj = {
  30. 'Content-Type': 'multipart/form-data',
  31. 'Authorization': 'pHVaIfVX8kgxsEL2THTYMVzJDYY3MMZU'
  32. };
  33. const url = 'https://sm.ms/api/v2/upload';
  34. const response = await this.app.curl(url, {
  35. method: 'POST',
  36. headers: headersObj,
  37. files: {
  38. smfile: tmpFile,
  39. },
  40. dataType: 'json',
  41. timeout: 15000,
  42. });
  43. const result = response.data;
  44. if (this.app.config.env === 'local') {
  45. this.app.logger.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  46. }
  47. if (result.code !== 'success') {
  48. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  49. }
  50. return result;
  51. } catch (e) {
  52. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  53. }
  54. return res;
  55. }
  56. async autoLaunchEnable() {
  57. const callResult = await socketClient.call('controller.example.autoLaunchEnable');
  58. return callResult.data;
  59. }
  60. async autoLaunchDisable() {
  61. const callResult = await socketClient.call('controller.example.autoLaunchDisable');
  62. return callResult.data;
  63. }
  64. async autoLaunchIsEnabled() {
  65. const callResult = await socketClient.call('controller.example.autoLaunchIsEnabled');
  66. return callResult.data;
  67. }
  68. async openSoftware(softName) {
  69. const callResult = await socketClient.call('controller.example.openSoftware', softName);
  70. return callResult.data;
  71. }
  72. async selectDir() {
  73. const result = await socketClient.call('controller.example.selectDir');
  74. if (!result) {
  75. return '';
  76. }
  77. return result;
  78. }
  79. async messageShow() {
  80. await socketClient.call('controller.example.messageShow');
  81. return true;
  82. }
  83. async messageShowConfirm() {
  84. await socketClient.call('controller.example.messageShowConfirm');
  85. return true;
  86. }
  87. async loadExtension(filePath) {
  88. await socketClient.call('controller.example.loadExtension', filePath);
  89. return true;
  90. }
  91. }
  92. module.exports = ExampleService;