example.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const EeSocket = require('ee-core').Socket.EeSocket;
  4. const socketClient = EeSocket.getClient();
  5. class ExampleService extends Service {
  6. async test1(id = 0) {
  7. const res = await socketClient.call('controller.example.test', {name:"gsx"}, {age:12});
  8. return null;
  9. }
  10. async uploadFileToSMMS(tmpFile) {
  11. const res = {
  12. code: 1000,
  13. message: 'unknown error',
  14. };
  15. try {
  16. //throw new Error('Sync Error');
  17. const headersObj = {
  18. 'Content-Type': 'multipart/form-data',
  19. 'Authorization': 'pHVaIfVX8kgxsEL2THTYMVzJDYY3MMZU'
  20. };
  21. const url = 'https://sm.ms/api/v2/upload';
  22. const response = await this.app.curl(url, {
  23. method: 'POST',
  24. headers: headersObj,
  25. files: {
  26. smfile: tmpFile,
  27. },
  28. dataType: 'json',
  29. timeout: 15000,
  30. });
  31. const result = response.data;
  32. if (this.app.config.env === 'local') {
  33. this.app.logger.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  34. }
  35. if (result.code !== 'success') {
  36. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  37. }
  38. return result;
  39. } catch (e) {
  40. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  41. }
  42. return res;
  43. }
  44. async messageShow() {
  45. await socketClient.call('controller.example.messageShow');
  46. return true;
  47. }
  48. async messageShowConfirm() {
  49. await socketClient.call('controller.example.messageShowConfirm');
  50. return true;
  51. }
  52. async loadExtension(filePath) {
  53. await socketClient.call('controller.example.loadExtension', filePath);
  54. return true;
  55. }
  56. }
  57. module.exports = ExampleService;