http-util.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }
  11. // 替换uri中的参数变量
  12. merge(uri, query = {}) {
  13. const keys = Object.keys(query);
  14. const arr = [];
  15. for (const k of keys) {
  16. arr.push(`${k}=${query[k]}`);
  17. }
  18. if (arr.length > 0) {
  19. uri = `${uri}?${arr.join('&')}`;
  20. }
  21. return uri;
  22. }
  23. /**
  24. * curl-get请求
  25. * @param {String} uri 接口地址
  26. * @param {String} project config中的项目key
  27. * @param {Object} query 地址栏参数
  28. * @param {Object} options 额外参数
  29. */
  30. async cget(uri, project, query, options) {
  31. return this.toRequest(uri, project, null, query, options);
  32. }
  33. /**
  34. * curl-post请求
  35. * @param {String} uri 接口地址
  36. * @param {String} project config中的项目key
  37. * @param {Object} data post的body
  38. * @param {Object} query 地址栏参数
  39. * @param {Object} options 额外参数
  40. */
  41. async cpost(uri, project, data = {}, query, options) {
  42. return this.toRequest(uri, project, data, query, options);
  43. }
  44. async toRequest(uri, project, data, query, options) {
  45. const prefix = _.get(this.ctx.app.config.project, project);
  46. if (!prefix) {
  47. throw new BusinessError(
  48. ErrorCode.SERVICE_FAULT,
  49. `未设置用户权限项目的关联:config.project.${project} is undefined`
  50. );
  51. }
  52. query = _.pickBy(
  53. query,
  54. val => val !== '' && val !== 'undefined' && val !== 'null'
  55. );
  56. if (!uri) console.error('uri不能为空');
  57. if (_.isObject(query) && _.isObject(options)) {
  58. const params = query.params ? query.params : query;
  59. options = { ...options, params };
  60. } else if (_.isObject(query) && !query.params) {
  61. options = { params: query };
  62. } else if (_.isObject(query) && query.params) {
  63. options = query;
  64. }
  65. // 是否多租户模式,需要改变headers
  66. const headers = { 'content-type': 'application/json' };
  67. const url = this.merge(`${prefix}${uri}`, options.params);
  68. let res = await this.ctx.curl(url, {
  69. method: isNullOrUndefined(data) ? 'get' : 'post',
  70. url,
  71. data,
  72. dataType: 'json',
  73. headers,
  74. ...options,
  75. });
  76. if (res.status === 200) {
  77. res = res.data || {};
  78. const { errcode, errmsg, details } = res;
  79. if (errcode) {
  80. console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`);
  81. return { errcode, errmsg };
  82. }
  83. return res.data;
  84. }
  85. const { status } = res;
  86. console.warn(`[${uri}] fail: ${status}-${res.data.message} `);
  87. }
  88. }
  89. module.exports = HttpUtilService;