lrf402788946 пре 4 година
родитељ
комит
ef9a356f44
2 измењених фајлова са 25 додато и 25 уклоњено
  1. 22 24
      app/service/util/http-util.js
  2. 3 1
      config/config.default.js

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

@@ -1,14 +1,16 @@
 '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 UtilService extends AxiosService {
+//
+class HttpUtilService extends AxiosService {
   constructor(ctx) {
     super(ctx, {}, {});
   }
 
+
   // 替换uri中的参数变量
   merge(uri, query = {}) {
     const keys = Object.keys(query);
@@ -25,37 +27,34 @@ class UtilService extends AxiosService {
   /**
    * curl-get请求
    * @param {String} uri 接口地址
+   * @param {String} project config中的项目key
    * @param {Object} query 地址栏参数
    * @param {Object} options 额外参数
    */
-  async cget(uri, query, options) {
-    return this.toRequest(uri, null, query, options);
+  async cget(uri, project, query, options) {
+    return this.toRequest(uri, project, 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, data = {}, query, options) {
-    return this.toRequest(uri, data, query, options);
-  }
-
-  /**
-   * 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 cpost(uri, project, data = {}, query, options) {
+    return this.toRequest(uri, project, data, query, options);
   }
 
-  async 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`
+      );
+    }
     query = _.pickBy(
       query,
       val => val !== '' && val !== 'undefined' && val !== 'null'
@@ -71,9 +70,7 @@ class UtilService extends AxiosService {
     }
     // 是否多租户模式,需要改变headers
     const headers = { 'content-type': 'application/json' };
-    let url = this.merge(`${uri}`, options.params);
-    if (!url.includes('http')) url = `http://localhost${url}`;
-    console.log(url);
+    const url = this.merge(`${prefix}${uri}`, options.params);
     let res = await this.ctx.curl(url, {
       method: isNullOrUndefined(data) ? 'get' : 'post',
       url,
@@ -89,10 +86,11 @@ class UtilService extends AxiosService {
         console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`);
         return { errcode, errmsg };
       }
-      return res;
+      return res.data;
     }
     const { status } = res;
     console.warn(`[${uri}] fail: ${status}-${res.data.message} `);
   }
 }
-module.exports = UtilService;
+
+module.exports = HttpUtilService;

+ 3 - 1
config/config.default.js

@@ -75,7 +75,9 @@ module.exports = appInfo => {
   config.export = {
     root_path: 'D:\\workspace\\service\\service-file\\upload\\',
   };
-
+  config.project = {
+    mission: 'http://127.0.0.1:4001',
+  };
   return {
     ...config,
     ...userConfig,