example.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }
  70. module.exports = ExampleService;