example.js 2.8 KB

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