example.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. const Service = require('ee-core').Service;
  3. /**
  4. * 示例服务
  5. * @class
  6. */
  7. class ExampleService extends Service {
  8. constructor(ctx) {
  9. super(ctx);
  10. }
  11. /**
  12. * test
  13. */
  14. async test (args) {
  15. let obj = {
  16. status:'ok',
  17. params: args
  18. }
  19. return obj;
  20. }
  21. /**
  22. * 上传到smms
  23. */
  24. async uploadFileToSMMS(tmpFile) {
  25. const res = {
  26. code: 1000,
  27. message: 'unknown error',
  28. };
  29. try {
  30. const headersObj = {
  31. 'Content-Type': 'multipart/form-data',
  32. 'Authorization': 'aaaaaaaaaaaaa' // 请修改这个token,用你自己的账号token
  33. };
  34. const url = 'https://sm.ms/api/v2/upload';
  35. const response = await this.app.curl(url, {
  36. method: 'POST',
  37. headers: headersObj,
  38. files: {
  39. smfile: tmpFile,
  40. },
  41. dataType: 'json',
  42. timeout: 15000,
  43. });
  44. const result = response.data;
  45. if (this.app.config.env === 'local') {
  46. this.app.logger.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  47. }
  48. if (result.code !== 'success') {
  49. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  50. }
  51. return result;
  52. } catch (e) {
  53. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  54. }
  55. return res;
  56. }
  57. }
  58. ExampleService.toString = () => '[class ExampleService]';
  59. module.exports = ExampleService;