123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 'use strict';
- const { AxiosService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { isNullOrUndefined } = require('naf-core').Util;
- const _ = require('lodash');
- //
- class HttpUtilService extends AxiosService {
- constructor(ctx) {
- super(ctx, {}, {});
- this.serverPort = this.app.config.serverPort;
- this.serverIp = this.app.config.serverIp;
- }
- // 替换uri中的参数变量
- merge(uri, query = {}) {
- const keys = Object.keys(query);
- const arr = [];
- for (const k of keys) {
- arr.push(`${k}=${query[k]}`);
- }
- if (arr.length > 0) {
- uri = `${uri}?${arr.join('&')}`;
- }
- return uri;
- }
- /**
- * curl-get请求
- * @param {String} uri 接口地址
- * @param {Object} query 地址栏参数
- * @param {Object} options 额外参数
- */
- async $get(uri, query, options) {
- return this.toRequest(uri, null, query, options);
- }
- /**
- * curl-post请求
- * @param {String} uri 接口地址
- * @param {Object} data post的body
- * @param {Object} query 地址栏参数
- * @param {Object} options 额外参数
- */
- async $post(uri, data = {}, query, options) {
- return this.toRequest(uri, data, query, options);
- }
- /**
- * curl-post请求
- * @param {String} uri 接口地址
- * @param {Object} data post的body
- * @param {Object} query 地址栏参数
- * @param {Object} options 额外参数
- */
- async $delete(uri, data = {}, query, options) {
- options = { ...options, method: 'delete' };
- return this.toRequest(uri, data, query, options);
- }
- async toRequest(uri, data, query, options) {
- query = _.pickBy(
- query,
- val => val !== '' && val !== 'undefined' && val !== 'null'
- );
- if (!uri) console.error('uri不能为空');
- if (_.isObject(query) && _.isObject(options)) {
- const params = query.params ? query.params : query;
- options = { ...options, params };
- } else if (_.isObject(query) && !query.params) {
- options = { params: query };
- } else if (_.isObject(query) && query.params) {
- options = query;
- }
- // 是否多租户模式,需要改变headers
- const headers = { 'content-type': 'application/json' };
- const authorization = _.get(this.ctx.request.header, 'authorization');
- if (authorization) headers.authorization = authorization;
- const url = this.merge(`http://${this.serverIp}:${this.serverPort}${uri}`, options.params);
- console.log(url);
- console.log(data);
- const res = await this.ctx.curl(url, {
- method: isNullOrUndefined(data) ? 'get' : 'post',
- url,
- data,
- dataType: 'json',
- headers,
- ...options,
- timeout: 6000,
- });
- console.log(res.data);
- return res.data;
- // if (res.status === 200) {
- // res = res.data || {};
- // const { code, message } = res;
- // if (code) {
- // console.warn(`[${uri}] fail: ${code}-${message} `);
- // return { code, message };
- // }
- // return res.data;
- // }
- // const { status } = res;
- // console.warn(`[${uri}] fail: ${status}-${res.data.message} `);
- }
- }
- module.exports = HttpUtilService;
|