Forráskód Böngészése

删除中间件,更改框架版本

lrf 3 éve
szülő
commit
bc46f522b4

+ 0 - 42
app/middleware/check-token.js

@@ -1,42 +0,0 @@
-'use strict';
-const _ = require('lodash');
-const jwt = require('jsonwebtoken');
-const { BusinessError, ErrorCode } = require('naf-core').Error;
-
-/**
- * 验证token
- * @param {Object} token token字符串
- * @param {String} secret jwt密码
- */
-const checkJwt = (token, secret) => {
-  if (!token) throw new BusinessError(ErrorCode.ACCESS_DENIED, '缺少秘钥,拒绝访问');
-  const errorList = [
-    { key: 'jwt expired', word: '秘钥已过期,请重新登陆' },
-    { key: 'invalid signature', word: '秘钥错误,请检查秘钥' },
-    { key: 'JSON at position', word: '秘钥错误,请检查秘钥' },
-    { key: 'invalid token', word: '秘钥错误,请检查秘钥' },
-  ];
-  try {
-    const r = jwt.verify(token, secret);
-    if (r) return r; // 如果过期将返回false
-    return false;
-  } catch (e) {
-    const { message } = e;
-    const r = errorList.find(f => message.includes(f.key));
-    if (r) throw new BusinessError(ErrorCode.ACCESS_DENIED, r.word);
-    else throw new BusinessError(ErrorCode.ACCESS_DENIED, '秘钥产生位置错误,检测失败');
-  }
-};
-
-
-module.exports = options => {
-  return async function checkToken(ctx, next) {
-    // token处理
-    const token = _.get(ctx.request, 'header.authorization');
-    if (token) {
-      const r = checkJwt(token, ctx.app.config.jwt.secret);
-      ctx.user = r;
-    }
-    await next();
-  };
-};

+ 0 - 12
app/middleware/password.js

@@ -1,12 +0,0 @@
-'use strict';
-module.exports = options => {
-  return async function password(ctx, next) {
-    // mongodb中secret转换为密码类型
-    if (ctx.request.method !== 'GET' && !ctx.request.url.includes('login')) {
-      const body = ctx.request.body;
-      if (body && body.password) body.password = { secret: body.password };
-    }
-
-    await next();
-  };
-};

+ 8 - 2
app/middleware/request-log.js

@@ -8,6 +8,8 @@ module.exports = ({ toMongoDB = false }) =>
       const url = request.url;
       // 请求方法
       const method = request.method;
+      // get 请求不发日志
+      if (method === 'GET') return;
       // 请求的分站标识
       const tenant = ctx.tenant || 'master';
       // 请求的用户
@@ -36,8 +38,12 @@ module.exports = ({ toMongoDB = false }) =>
       if (ctx.mq) {
         ctx.service.util.rabbitMq.sendToMqQueue(logData, 'logs');
       } else {
-        // http
-
+        // http请求
+        const httpPrefix = ctx.app.config.httpPrefix;
+        if (httpPrefix && httpPrefix.logs) {
+          const uri = `${httpPrefix.logs}/logs`;
+          ctx.service.util.httpUtil.cpost(uri, logData);
+        }
       }
     } catch (error) {
       // 没啥可输出,别中断就行

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

@@ -0,0 +1,87 @@
+'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, {}, {});
+  }
+
+
+  // 替换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 cget(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 cpost(uri, data = {}, query, options) {
+    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;
+    }
+    const headers = { 'content-type': 'application/json' };
+    console.log(uri);
+    const url = this.merge(`${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 = HttpUtilService;

+ 5 - 1
config/config.default.js

@@ -17,7 +17,7 @@ module.exports = appInfo => {
   config.keys = appInfo.name + '_1640765284662_2781';
 
   // add your middleware config here
-  config.middleware = [ 'checkToken', 'requestLog' ];
+  config.middleware = [ 'requestLog' ];
 
   // add your user config here
   const userConfig = {
@@ -45,6 +45,10 @@ module.exports = appInfo => {
   config.sendQueue = {
     logs: 'freeAdmin/server-logs',
   };
+  // http请求前缀
+  config.httpPrefix = {
+    logs: 'http://localhost:13002/freeAdminLog/api',
+  };
   // redis设置
   config.redis = {
     client: {

+ 2 - 2
package.json

@@ -8,12 +8,12 @@
   },
   "dependencies": {
     "egg": "^2.15.1",
-    "egg-scripts": "^2.11.0",
     "egg-naf-amqp": "0.0.13",
     "egg-redis": "^2.4.0",
+    "egg-scripts": "^2.11.0",
     "lodash": "^4.17.21",
     "moment": "^2.29.1",
-    "naf-framework-mongoose-free": "^0.0.10"
+    "naf-framework-mongoose-free": "^0.0.11"
   },
   "devDependencies": {
     "autod": "^3.0.1",