example.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. console.log('selectDir: result:', result);
  75. if (!result.data) {
  76. return '';
  77. }
  78. return result.data;
  79. }
  80. async messageShow() {
  81. await socketClient.call('controller.example.messageShow');
  82. return true;
  83. }
  84. async messageShowConfirm() {
  85. await socketClient.call('controller.example.messageShowConfirm');
  86. return true;
  87. }
  88. async loadExtension(filePath) {
  89. await socketClient.call('controller.example.loadExtension', filePath);
  90. return true;
  91. }
  92. }
  93. module.exports = ExampleService;