http-util.js 2.8 KB

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