example.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const BaseService = require('./base');
  3. const fs = require('fs');
  4. const path = require('path');
  5. class ExampleService extends BaseService {
  6. async openLocalDir(dir) {
  7. const self = this;
  8. await self.ipcCall('example.openDir', dir);
  9. return true;
  10. }
  11. async getSMMSToken() {
  12. const res = {
  13. code: 1000,
  14. message: 'unknown error',
  15. };
  16. try {
  17. //throw new Error('Sync Error');
  18. params = {
  19. username: '',
  20. password: ''
  21. };
  22. const url = 'https://sm.ms/api/v2/token';
  23. const response = await this.app.curl(url, {
  24. method: 'POST',
  25. contentType: 'application/json',
  26. data: params,
  27. dataType: 'json',
  28. timeout: 15000,
  29. });
  30. const result = response.data;
  31. if (this.app.config.env === 'local') {
  32. this.app.logger.info('[ExampleService] [getSMMSToken]: info result:%j', result);
  33. }
  34. // this.app.logger.info('[OutapiService] [api]: result:%j', result);
  35. if (result.code !== 'success') {
  36. this.app.logger.error('[ExampleService] [getSMMSToken]: res error result:%j', result);
  37. }
  38. return result;
  39. } catch (e) {
  40. this.app.logger.error('[ExampleService] [getSMMSToken]: ERROR ', e);
  41. }
  42. return res;
  43. }
  44. async uploadFileToSMMS(tmpFile) {
  45. const res = {
  46. code: 1000,
  47. message: 'unknown error',
  48. };
  49. try {
  50. //throw new Error('Sync Error');
  51. const headersObj = {
  52. 'Content-Type': 'multipart/form-data',
  53. 'Authorization': 'pHVaIfVX8kgxsEL2THTYMVzJDYY3MMZU'
  54. };
  55. const url = 'https://sm.ms/api/v2/upload';
  56. const response = await this.app.curl(url, {
  57. method: 'POST',
  58. headers: headersObj,
  59. files: {
  60. smfile: tmpFile,
  61. },
  62. //contentType: 'application/json',
  63. //data: params,
  64. dataType: 'json',
  65. timeout: 15000,
  66. });
  67. const result = response.data;
  68. if (this.app.config.env === 'local') {
  69. this.app.logger.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  70. }
  71. if (result.code !== 'success') {
  72. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  73. }
  74. return result;
  75. } catch (e) {
  76. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  77. }
  78. return res;
  79. }
  80. }
  81. module.exports = ExampleService;