http-util.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.dbServerPort = this.app.config.dbServerPort;
  11. this.dbServerIp = this.app.config.dbServerIp;
  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(query, val => val && val !== '' && val !== 'undefined' && val !== 'null');
  57. if (!(query.page && query.size)) {
  58. delete query.page;
  59. delete query.size;
  60. }
  61. if (!uri) console.error('uri不能为空');
  62. if (_.isObject(query) && _.isObject(options)) {
  63. const params = query.params ? query.params : query;
  64. options = { ...options, params };
  65. } else if (_.isObject(query) && !query.params) {
  66. options = { params: query };
  67. } else if (_.isObject(query) && query.params) {
  68. options = query;
  69. }
  70. // 是否多租户模式,需要改变headers
  71. const headers = { 'content-type': 'application/json' };
  72. let url = '';
  73. if (uri.includes('http')) url = uri;
  74. else url = this.merge(`http://${this.dbServerIp}:${this.dbServerPort}${uri}`, options.params);
  75. let res = await this.ctx.curl(url, {
  76. method: isNullOrUndefined(data) ? 'get' : 'post',
  77. url,
  78. data,
  79. dataType: 'json',
  80. headers,
  81. ...options,
  82. });
  83. if (res.status === 200) {
  84. res = res.data || {};
  85. const { code, message } = res;
  86. if (code) {
  87. console.warn(`[${uri}] fail: ${code}-${message} `);
  88. return { code, message };
  89. }
  90. if (_.isArray(res.data)) return { list: res.data };
  91. return res.data;
  92. }
  93. const { status } = res;
  94. console.warn(`[${uri}] fail: ${status}-${res.data.message} `);
  95. }
  96. }
  97. module.exports = HttpUtilService;