example.js 2.7 KB

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