lrf 3 年之前
父节点
当前提交
2fac2c585c
共有 12 个文件被更改,包括 86 次插入5 次删除
  1. 6 0
      .babelrc
  2. 12 2
      .eslintrc
  3. 10 0
      app.js
  4. 1 0
      app/controller/.table.js
  5. 4 0
      app/controller/table.js
  6. 18 0
      app/middleware/logDb.js
  7. 12 0
      app/public/deco.js
  8. 4 0
      app/public/options.js
  9. 6 1
      app/service/table.js
  10. 3 2
      app/z_router/table.js
  11. 6 0
      jsconfig.json
  12. 4 0
      package.json

+ 6 - 0
.babelrc

@@ -0,0 +1,6 @@
+{
+    "presets": [],
+    "plugins": [
+        "transform-decorators-legacy"
+    ]
+}

+ 12 - 2
.eslintrc

@@ -1,3 +1,13 @@
 {
-  "extends": "eslint-config-egg"
-}
+  "root": true,
+  "extends": ["prettier", "prettier/standard"],
+  "plugins": ["prettier", "standard"],
+  "rules": {
+    "prettier/prettier": "error"
+  },
+  "parser": "babel-eslint",
+  "env": {
+    "node": true,
+    "mocha": true
+  }
+}

+ 10 - 0
app.js

@@ -0,0 +1,10 @@
+'use strict';
+
+const register = require('babel-register');
+
+register({
+  plugins: ['transform-decorators-legacy'],
+});
+
+module.exports = (app) => {
+};

+ 1 - 0
app/controller/.table.js

@@ -39,5 +39,6 @@ module.exports = {
   },
   toExport: {
     requestBody: ['!ids'],
+    operation: '导出',
   },
 };

+ 4 - 0
app/controller/table.js

@@ -8,5 +8,9 @@ class TableController extends Controller {
     super(ctx);
     this.service = this.ctx.service.table;
   }
+
+  // async toExport() {
+  //   console.log(this.toExport.operation);
+  // }
 }
 module.exports = CrudController(TableController, meta);

+ 18 - 0
app/middleware/logDb.js

@@ -0,0 +1,18 @@
+'use strict';
+const _ = require('lodash');
+const getController = (url) => {
+  if (!_.isString(url)) return;
+  const arr = _.trimStart(url, '/').split('/');
+  return _.head(arr);
+};
+module.exports = (options) => {
+  return async function logdb(ctx, next) {
+    let url = _.clone(ctx.request.url);
+    url = url.replace(ctx.app.config.routePrefix, '');
+    const c = getController(url);
+    console.log(ctx.service[c].toExport.operation);
+    console.log(ctx.routerName);
+    console.log(ctx.routerPath);
+    await next();
+  };
+};

+ 12 - 0
app/public/deco.js

@@ -0,0 +1,12 @@
+'use strict';
+const _ = require('lodash');
+const log = (operation) => {
+  return (target, name, descriptor) => {
+    if (operation) {
+      descriptor.value.operation = operation;
+    }
+    return descriptor;
+  };
+};
+
+module.exports = { log };

+ 4 - 0
app/public/options.js

@@ -6,6 +6,10 @@ module.exports = {
       value: 'String',
       default: true,
     },
+    {
+      label: '布尔值',
+      value: 'Boolean',
+    },
     {
       label: '数字',
       value: 'Number',

+ 6 - 1
app/service/table.js

@@ -1,12 +1,17 @@
 'use strict';
 const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const template = require('../public/table-template');
+const { log } = require('../public/deco');
 class TableService extends CrudService {
   constructor(ctx) {
     super(ctx, 'table');
     this.model = this.ctx.model.Table;
   }
-
+  @log('测试')
+  async test(data){
+    console.log(data);
+  }
+  @log('导出')
   async toExport({ ids }) {
     const res = await this.model.find({ _id: { $in: ids } });
     const data = {};

+ 3 - 2
app/z_router/table.js

@@ -1,10 +1,11 @@
 'use strict';
 
-module.exports = app => {
+module.exports = (app) => {
   const { router, controller, config } = app;
   const target = 'table';
   const prefix = config.routePrefix;
-  router.post(target, `${prefix}/${target}/export`, controller[target].toExport);
+  const mware = app.middleware.logDb();
+  router.post(`targetExport`, `${prefix}/${target}/export`, mware, controller[target].toExport);
   router.resources(target, `${prefix}/${target}`, controller[target]); // index、create、show、destroy
   router.post(target, `${prefix}/${target}/:id`, controller[target].update);
 };

+ 6 - 0
jsconfig.json

@@ -0,0 +1,6 @@
+{
+  "include": ["**/*"],
+  "compilerOptions": {
+    "experimentalDecorators": true
+  }
+}

+ 4 - 0
package.json

@@ -16,6 +16,10 @@
   "devDependencies": {
     "autod": "^3.0.1",
     "autod-egg": "^1.1.0",
+    "babel-cli": "^6.26.0",
+    "babel-core": "^6.26.3",
+    "babel-plugin-transform-decorators-legacy": "^1.3.5",
+    "babel-register": "^6.26.0",
     "egg-bin": "^4.11.0",
     "egg-ci": "^1.11.0",
     "egg-mock": "^3.21.0",