axios-service.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { BusinessError, ErrorCode } = require('naf-core').Error;
  5. const { trimData, isNullOrUndefined } = require('naf-core').Util;
  6. const { NafService } = require('./naf-service');
  7. const axios = require('axios');
  8. /**
  9. * meta 格式
  10. * {
  11. * "baseUrl": "可选",
  12. * "uri": "接口地址",
  13. * "method": "GET or POST",如果为空根据接口data参数推断
  14. * }
  15. *
  16. * 接口参数定义
  17. * api(query, data)
  18. * query - 查询参数对象
  19. * data - POST data
  20. */
  21. class AxiosService extends NafService {
  22. constructor(ctx, meta, { baseUrl = '' }) {
  23. super(ctx);
  24. assert(_.isObject(meta));
  25. this.baseUrl = baseUrl;
  26. _.forEach(meta, (val, key) => {
  27. const { method, uri = key, baseUrl: _baseUrl } = val;
  28. this[key] = async (query = {}, data, options) => {
  29. if (_.isUndefined(options) && _.toLower(method) === 'get') {
  30. // TODO: get 请求可以没有data参数,直接是query,options
  31. options = data;
  32. data = undefined;
  33. }
  34. if (_.isUndefined(options) && _.isUndefined(data) && _.toLower(method) === 'post') {
  35. // TODO: post 请求可以只有一个data
  36. if (AxiosService.isOpts(query)) {
  37. options = query;
  38. } else {
  39. data = query;
  40. }
  41. query = undefined;
  42. }
  43. options = AxiosService.mergeOpts(query, data, options);
  44. options = _.merge(trimData({ method, baseURL: _baseUrl }), options);
  45. return await this.request(uri, options);
  46. };
  47. });
  48. }
  49. static isOpts(data) {
  50. // TODO: 判断是否Options对象
  51. return _.isObject(data) &&
  52. (_.isString(data.baseURL) || _.isObject(data.params) || _.isObject(data.data));
  53. }
  54. // 替换uri中的参数变量
  55. static mergeOpts(query, data, options) {
  56. // TODO: 合并query、data和options
  57. if (query && _.isUndefined(data) && _.isUndefined(options)) { // 只有一个参数,作为options或者query
  58. options = AxiosService.isOpts(query) ? query : { };
  59. }
  60. options = options || {};
  61. options.params = trimData(_.merge(options.params, query));
  62. if (data) {
  63. options.data = trimData(data);
  64. }
  65. return options;
  66. }
  67. // 替换uri中的参数变量
  68. static merge(uri, query = {}) {
  69. if (!uri.includes(':')) {
  70. return uri;
  71. }
  72. const keys = [];
  73. const regexp = /\/:([a-z0-9_]+)/ig;
  74. let res;
  75. // eslint-disable-next-line no-cond-assign
  76. while ((res = regexp.exec(uri)) != null) {
  77. keys.push(res[1]);
  78. }
  79. keys.forEach(key => {
  80. if (!isNullOrUndefined(query[key])) {
  81. uri = uri.replace(`:${key}`, query[key]);
  82. }
  83. });
  84. return uri;
  85. }
  86. httpGet(uri, query = {}, options) {
  87. options = AxiosService.mergeOpts(query, null, options);
  88. options = _.merge(options, { method: 'get' });
  89. return this.request(uri, options);
  90. }
  91. httpPost(uri, query = {}, data, options) {
  92. options = AxiosService.mergeOpts(query, data, options);
  93. options = _.merge(options, { method: 'post' });
  94. return this.request(uri, options);
  95. }
  96. async request(uri, query, data, options) {
  97. // TODO: 合并query和options
  98. options = AxiosService.mergeOpts(query, data, options);
  99. // TODO: 处理租户信息
  100. if (!options.params._tenant) {
  101. options.params._tenant = this.ctx.tenant; // 租户信息
  102. }
  103. const url = AxiosService.merge(uri, options.params);
  104. try {
  105. let res = await axios({
  106. method: isNullOrUndefined(data) ? 'get' : 'post',
  107. url,
  108. baseURL: this.baseUrl, // 可以被options中的baseURL覆盖
  109. responseType: 'json',
  110. ...options,
  111. });
  112. if (res.status !== 200) {
  113. throw new BusinessError(ErrorCode.NETWORK, `Http Code: ${res.status}`, res.data);
  114. }
  115. res = res.data;
  116. const { errcode, errmsg, details } = res;
  117. if (errcode) {
  118. throw new BusinessError(errcode, errmsg, details);
  119. }
  120. res = _.omit(res, [ 'errcode', 'errmsg', 'details' ]);
  121. const keys = Object.keys(res);
  122. if (keys.length === 1 && keys.includes('data')) {
  123. res = res.data;
  124. }
  125. return res;
  126. } catch (err) {
  127. if (err instanceof BusinessError) {
  128. throw err;
  129. }
  130. let errmsg = '接口请求失败';
  131. if (err.response) {
  132. const { status } = err.response;
  133. if (status === 401) {
  134. errmsg += ': 用户认证失败';
  135. } else if (status === 403) {
  136. errmsg += ': 当前用户不允许执行该操作';
  137. } else if (status >= 300) {
  138. errmsg += `: 网络错误:HttpCode ${status}`;
  139. }
  140. }
  141. this.ctx.logger.error(`[AxiosWrapper] 接口请求失败: ${err.config && err.config.url} - ${err.message}`);
  142. if (err.response && err.response.data) {
  143. this.ctx.logger.debug('[AxiosService]', err.response.data);
  144. }
  145. throw new BusinessError(ErrorCode.SERVICE_FAULT, errmsg, err.message);
  146. }
  147. }
  148. }
  149. module.exports.AxiosService = AxiosService;