example.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 executeJS(str) {
  11. let result = await socketClient.call('controller.example.executeJS', str);
  12. return result;
  13. }
  14. async uploadFileToSMMS(tmpFile) {
  15. const res = {
  16. code: 1000,
  17. message: 'unknown error',
  18. };
  19. try {
  20. //throw new Error('Sync Error');
  21. const headersObj = {
  22. 'Content-Type': 'multipart/form-data',
  23. 'Authorization': 'pHVaIfVX8kgxsEL2THTYMVzJDYY3MMZU'
  24. };
  25. const url = 'https://sm.ms/api/v2/upload';
  26. const response = await this.app.curl(url, {
  27. method: 'POST',
  28. headers: headersObj,
  29. files: {
  30. smfile: tmpFile,
  31. },
  32. dataType: 'json',
  33. timeout: 15000,
  34. });
  35. const result = response.data;
  36. if (this.app.config.env === 'local') {
  37. this.app.logger.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  38. }
  39. if (result.code !== 'success') {
  40. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  41. }
  42. return result;
  43. } catch (e) {
  44. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  45. }
  46. return res;
  47. }
  48. async autoLaunchEnable() {
  49. const callResult = await socketClient.call('controller.example.autoLaunchEnable');
  50. return callResult.data;
  51. }
  52. async autoLaunchDisable() {
  53. const callResult = await socketClient.call('controller.example.autoLaunchDisable');
  54. return callResult.data;
  55. }
  56. async autoLaunchIsEnabled() {
  57. const callResult = await socketClient.call('controller.example.autoLaunchIsEnabled');
  58. return callResult.data;
  59. }
  60. async openSoftware(softName) {
  61. const callResult = await socketClient.call('controller.example.openSoftware', softName);
  62. return callResult.data;
  63. }
  64. async messageShow() {
  65. await socketClient.call('controller.example.messageShow');
  66. return true;
  67. }
  68. async messageShowConfirm() {
  69. await socketClient.call('controller.example.messageShowConfirm');
  70. return true;
  71. }
  72. async loadExtension(filePath) {
  73. await socketClient.call('controller.example.loadExtension', filePath);
  74. return true;
  75. }
  76. }
  77. module.exports = ExampleService;