lrf402788946 4 năm trước cách đây
mục cha
commit
15c4f75790
1 tập tin đã thay đổi với 24 bổ sung22 xóa
  1. 24 22
      app/service/util/http-util.js

+ 24 - 22
app/service/util/http-util.js

@@ -1,16 +1,14 @@
 'use strict';
+const _ = require('lodash');
 const { AxiosService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const { isNullOrUndefined } = require('naf-core').Util;
-const _ = require('lodash');
 
-//
-class HttpUtilService extends AxiosService {
+class UtilService extends AxiosService {
   constructor(ctx) {
     super(ctx, {}, {});
   }
 
-
   // 替换uri中的参数变量
   merge(uri, query = {}) {
     const keys = Object.keys(query);
@@ -27,34 +25,37 @@ class HttpUtilService extends AxiosService {
   /**
    * curl-get请求
    * @param {String} uri 接口地址
-   * @param {String} project config中的项目key
    * @param {Object} query 地址栏参数
    * @param {Object} options 额外参数
    */
-  async cget(uri, project, query, options) {
-    return this.toRequest(uri, project, null, query, options);
+  async cget(uri, query, options) {
+    return this.toRequest(uri, null, query, options);
   }
 
   /**
    * curl-post请求
    * @param {String} uri 接口地址
-   * @param {String} project config中的项目key
    * @param {Object} data post的body
    * @param {Object} query 地址栏参数
    * @param {Object} options 额外参数
    */
-  async cpost(uri, project, data = {}, query, options) {
-    return this.toRequest(uri, project, data, query, options);
+  async cpost(uri, data = {}, query, options) {
+    return this.toRequest(uri, data, query, options);
   }
 
-  async toRequest(uri, project, data, query, options) {
-    const prefix = _.get(this.ctx.app.config.project, project);
-    if (!prefix) {
-      throw new BusinessError(
-        ErrorCode.SERVICE_FAULT,
-        `未设置用户权限项目的关联:config.project.${project} is undefined`
-      );
-    }
+  /**
+   * curl-delete请求
+   * @param {String} uri 接口地址
+   * @param {Object} data post的body
+   * @param {Object} query 地址栏参数
+   * @param {Object} options 额外参数默认 method:delete
+   */
+  async cdelete(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'
@@ -70,7 +71,9 @@ class HttpUtilService extends AxiosService {
     }
     // 是否多租户模式,需要改变headers
     const headers = { 'content-type': 'application/json' };
-    const url = this.merge(`${prefix}${uri}`, options.params);
+    let url = this.merge(`${uri}`, options.params);
+    if (!url.includes('http')) url = `http://localhost${url}`;
+    console.log(url);
     let res = await this.ctx.curl(url, {
       method: isNullOrUndefined(data) ? 'get' : 'post',
       url,
@@ -86,11 +89,10 @@ class HttpUtilService extends AxiosService {
         console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`);
         return { errcode, errmsg };
       }
-      return res.data;
+      return res;
     }
     const { status } = res;
     console.warn(`[${uri}] fail: ${status}-${res.data.message} `);
   }
 }
-
-module.exports = HttpUtilService;
+module.exports = UtilService;