lrf402788946 hace 4 años
padre
commit
bbdeecee83
Se han modificado 4 ficheros con 110 adiciones y 2 borrados
  1. 95 0
      app/service/http-util.js
  2. 7 2
      app/service/order/order.js
  3. 4 0
      config/config.default.js
  4. 4 0
      config/config.prod.js

+ 95 - 0
app/service/http-util.js

@@ -0,0 +1,95 @@
+'use strict';
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { AxiosService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const { isNullOrUndefined } = require('naf-core').Util;
+
+class UtilService extends AxiosService {
+  constructor(ctx) {
+    super(ctx, {}, {});
+  }
+
+  // 替换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 {String} project config中的项目key
+   * @param {Object} query 地址栏参数
+   * @param {Object} 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, project, data = {}, query, options) {
+    return this.toRequest(uri, project, 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'
+    );
+    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 url = this.merge(`${prefix}${uri}`, options.params);
+    let res = await this.ctx.curl(url, {
+      method: isNullOrUndefined(data) ? 'get' : 'post',
+      url,
+      data,
+      dataType: 'json',
+      headers,
+      ...options,
+    });
+    if (res.status === 200) {
+      res = res.data || {};
+      const { errcode, errmsg, details } = res;
+      if (errcode) {
+        console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`);
+        return { errcode, errmsg };
+      }
+      return res.data;
+    }
+    const { status } = res;
+    console.warn(`[${uri}] fail: ${status}-${res.data.message} `);
+  }
+}
+module.exports = UtilService;

+ 7 - 2
app/service/order/order.js

@@ -244,12 +244,17 @@ class OrderService extends CrudService {
     return '状态错误';
   }
 
+  /**
+   * 修改订单负责人
+   * @param {Object} data 订单数据
+   */
   async principalChange(data) {
     const { principal, _id } = data;
     const res = await this.model.update({ _id: ObjectId(_id) }, { principal });
     try {
-      // TODO 跨库查询该用户姓名
-      const message = '变更订单负责人';
+      // 跨库查询该用户姓名
+      const user = await this.ctx.service.httpUtil.cget(`/user/${principal}`, 'userAuth', { _tenant: 'zhwl' });
+      const message = `变更订单负责人 ${user.name}`;
       this.record(_id, { method: 'principal', message });
     } catch (error) {
       this.logger.error(`订单id:${res.id}记录创建失败:${error.toString()}`);

+ 4 - 0
config/config.default.js

@@ -40,6 +40,10 @@ module.exports = appInfo => {
     },
   };
 
+  config.project = {
+    userAuth: 'http://127.0.0.1:7003/api/role/auth',
+  };
+
   config.jwt = {
     ...jwt,
     expiresIn: '1d',

+ 4 - 0
config/config.prod.js

@@ -30,5 +30,9 @@ module.exports = () => {
     },
   };
 
+  config.project = {
+    userAuth: 'http://127.0.0.1:4000/api/role/auth',
+  };
+
   return config;
 };