http-util.js 2.8 KB

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