example.js 1.4 KB

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