guhongwei 4 年之前
父节点
当前提交
219e2380ae
共有 4 个文件被更改,包括 92 次插入0 次删除
  1. 38 0
      app/controller/.user.js
  2. 16 0
      app/controller/user.js
  3. 22 0
      app/model/user.js
  4. 16 0
      app/service/user.js

+ 38 - 0
app/controller/.user.js

@@ -0,0 +1,38 @@
+module.exports = {
+  create: {
+    requestBody: ["!name", "mobile", "password", "dept_id", "gender", "remark"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["!name", "mobile", "password", "dept_id", "gender", "remark"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        name: "name",
+        mobile: "mobile",
+        password: "password",
+        dept_id: "dept_id",
+        gender: "gender",
+        remark: "remark",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 16 - 0
app/controller/user.js

@@ -0,0 +1,16 @@
+'use strict';
+
+// const _ = require('lodash');
+const meta = require('./.user.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 用户表
+class UserController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.user;
+  }
+}
+
+module.exports = CrudController(UserController, meta);

+ 22 - 0
app/model/user.js

@@ -0,0 +1,22 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { Secret } = require('naf-framework-mongoose/lib/model/schema');
+// 用户表
+const UserSchema = {
+  name: { type: String, required: true, maxLength: 200 }, // 用户名称
+  mobile: { type: String, required: true, maxLength: 200 }, // 手机号
+  password: { type: Secret, select: false }, // 密码
+  dept_id: { type: String, required: true, maxLength: 200 }, // 部门id
+  gender: { type: String, required: true, maxLength: 200 }, // 性别
+  remark: { type: String, required: true, maxLength: 200 }, // 备注
+};
+
+const schema = new Schema(UserSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('User', schema, 'user');
+};

+ 16 - 0
app/service/user.js

@@ -0,0 +1,16 @@
+'use strict';
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { UserError, ErrorCode } = require('naf-core').Error;
+
+class UserService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'user');
+    this.model = this.ctx.model.User;
+  }
+}
+
+module.exports = UserService;