liuyu 5 rokov pred
rodič
commit
892151fd3f
3 zmenil súbory, kde vykonal 31 pridanie a 0 odobranie
  1. 22 0
      app/controller/common.js
  2. 2 0
      app/router.js
  3. 7 0
      app/service/util.js

+ 22 - 0
app/controller/common.js

@@ -0,0 +1,22 @@
+'use strict';
+
+const _ = require('lodash');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 共通方法管理
+class CommonController extends Controller {
+
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.util;
+  }
+
+  async findone() {
+    const data = await this.service.findone(this.ctx.params, this.ctx.request.query);
+    this.ctx.ok({ data });
+  }
+
+}
+
+module.exports = CommonController;

+ 2 - 0
app/router.js

@@ -6,6 +6,8 @@
 module.exports = app => {
   const { router, controller } = app;
   router.get('/', controller.home.index);
+  // 共通查询单条记录方法
+  router.get('/api/train/common/findone/:modelname', controller.common.findone);
 
   // 科目表设置路由
   router.resources('subject', '/api/train/subject', controller.subject); // index、create、show、destroy

+ 7 - 0
app/service/util.js

@@ -45,6 +45,13 @@ class UtilService extends CrudService {
       return false;
     }
   }
+
+  async findone({ modelname }, data) {
+    // 查询单条
+    const _model = _.capitalize(modelname);
+    const res = await this.ctx.model[_model].findOne({ ...data });
+    return res;
+  }
 }
 
 module.exports = UtilService;