http-util.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { AxiosService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const { isNullOrUndefined } = require('naf-core').Util;
  8. class UtilService extends AxiosService {
  9. constructor(ctx) {
  10. super(ctx, {}, {});
  11. }
  12. // 替换uri中的参数变量
  13. merge(uri, query = {}) {
  14. const keys = Object.keys(query);
  15. const arr = [];
  16. for (const k of keys) {
  17. arr.push(`${k}=${query[k]}`);
  18. }
  19. if (arr.length > 0) {
  20. uri = `${uri}?${arr.join('&')}`;
  21. }
  22. return uri;
  23. }
  24. /**
  25. * curl-get请求
  26. * @param {String} uri 接口地址
  27. * @param {String} project config中的项目key
  28. * @param {Object} query 地址栏参数
  29. * @param {Object} options 额外参数
  30. */
  31. async cget(uri, project, query, options) {
  32. return this.toRequest(uri, project, null, query, options);
  33. }
  34. /**
  35. * curl-post请求
  36. * @param {String} uri 接口地址
  37. * @param {String} project config中的项目key
  38. * @param {Object} data post的body
  39. * @param {Object} query 地址栏参数
  40. * @param {Object} options 额外参数
  41. */
  42. async cpost(uri, project, data = {}, query, options) {
  43. return this.toRequest(uri, project, data, query, options);
  44. }
  45. async toRequest(uri, project, data, query, options) {
  46. const prefix = _.get(this.ctx.app.config.project, project);
  47. if (!prefix) {
  48. throw new BusinessError(
  49. ErrorCode.SERVICE_FAULT,
  50. `未设置用户权限项目的关联:config.project.${project} is undefined`
  51. );
  52. }
  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. const url = this.merge(`${prefix}${uri}`, options.params);
  69. let res = await this.ctx.curl(url, {
  70. method: isNullOrUndefined(data) ? 'get' : 'post',
  71. url,
  72. data,
  73. dataType: 'json',
  74. headers,
  75. ...options,
  76. });
  77. if (res.status === 200) {
  78. res = res.data || {};
  79. const { errcode, errmsg, details } = res;
  80. if (errcode) {
  81. console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`);
  82. return { errcode, errmsg };
  83. }
  84. return res.data;
  85. }
  86. const { status } = res;
  87. console.warn(`[${uri}] fail: ${status}-${res.data.message} `);
  88. }
  89. }
  90. module.exports = UtilService;