http-util.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. const { AxiosService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { isNullOrUndefined } = require('naf-core').Util;
  5. const _ = require('lodash');
  6. //
  7. class HttpUtilService extends AxiosService {
  8. constructor(ctx) {
  9. super(ctx, {}, {});
  10. this.serverPort = this.app.config.serverPort;
  11. this.serverIp = this.app.config.serverIp;
  12. }
  13. // 替换uri中的参数变量
  14. merge(uri, query = {}) {
  15. const keys = Object.keys(query);
  16. const arr = [];
  17. for (const k of keys) {
  18. arr.push(`${k}=${query[k]}`);
  19. }
  20. if (arr.length > 0) {
  21. uri = `${uri}?${arr.join('&')}`;
  22. }
  23. return uri;
  24. }
  25. /**
  26. * curl-get请求
  27. * @param {String} uri 接口地址
  28. * @param {Object} query 地址栏参数
  29. * @param {Object} options 额外参数
  30. */
  31. async $get(uri, query, options) {
  32. return this.toRequest(uri, null, query, options);
  33. }
  34. /**
  35. * curl-post请求
  36. * @param {String} uri 接口地址
  37. * @param {Object} data post的body
  38. * @param {Object} query 地址栏参数
  39. * @param {Object} options 额外参数
  40. */
  41. async $post(uri, data = {}, query, options) {
  42. return this.toRequest(uri, data, query, options);
  43. }
  44. /**
  45. * curl-post请求
  46. * @param {String} uri 接口地址
  47. * @param {Object} data post的body
  48. * @param {Object} query 地址栏参数
  49. * @param {Object} options 额外参数
  50. */
  51. async $delete(uri, data = {}, query, options) {
  52. options = { ...options, method: 'delete' };
  53. return this.toRequest(uri, data, query, options);
  54. }
  55. async toRequest(uri, data, query, options) {
  56. query = _.pickBy(
  57. query,
  58. val => val !== '' && val !== 'undefined' && val !== 'null'
  59. );
  60. if (!uri) console.error('uri不能为空');
  61. if (_.isObject(query) && _.isObject(options)) {
  62. const params = query.params ? query.params : query;
  63. options = { ...options, params };
  64. } else if (_.isObject(query) && !query.params) {
  65. options = { params: query };
  66. } else if (_.isObject(query) && query.params) {
  67. options = query;
  68. }
  69. // 是否多租户模式,需要改变headers
  70. const headers = { 'content-type': 'application/json' };
  71. const authorization = _.get(this.ctx.request.header, 'authorization');
  72. if (authorization) headers.authorization = authorization;
  73. const url = this.merge(`http://${this.serverIp}:${this.serverPort}${uri}`, options.params);
  74. console.log(url);
  75. console.log(data);
  76. const res = await this.ctx.curl(url, {
  77. method: isNullOrUndefined(data) ? 'get' : 'post',
  78. url,
  79. data,
  80. dataType: 'json',
  81. headers,
  82. ...options,
  83. timeout: 6000,
  84. });
  85. console.log(res.data);
  86. return res.data;
  87. // if (res.status === 200) {
  88. // res = res.data || {};
  89. // const { code, message } = res;
  90. // if (code) {
  91. // console.warn(`[${uri}] fail: ${code}-${message} `);
  92. // return { code, message };
  93. // }
  94. // return res.data;
  95. // }
  96. // const { status } = res;
  97. // console.warn(`[${uri}] fail: ${status}-${res.data.message} `);
  98. }
  99. }
  100. module.exports = HttpUtilService;