Browse Source

抽出用户/登陆;添加字典表;结构目录采用分层管理,注意修改controller和service,router的引用

lrf402788946 4 năm trước cách đây
mục cha
commit
43461d12b3

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

@@ -1,38 +0,0 @@
-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,
-    },
-  },
-};

+ 0 - 34
app/controller/login.js

@@ -1,34 +0,0 @@
-'use strict';
-
-const Controller = require('egg').Controller;
-
-// 登录管理
-class LoginController extends Controller {
-
-  constructor(ctx) {
-    super(ctx);
-    this.service = this.ctx.service.login;
-  }
-
-  async login() {
-    const res = await this.service.login(this.ctx.request.body);
-    this.ctx.ok({ data: res });
-  }
-
-  async token() {
-    const res = await this.service.token(this.ctx.request.body);
-    this.ctx.ok({ data: res });
-  }
-
-  async destroy() {
-    const res = await this.service.destroy(this.ctx.request.body);
-    this.ctx.ok({ data: res });
-  }
-
-  // async wxlogin() {
-  //   const res = await this.service.wxlogin(this.ctx.request.body);
-  //   this.ctx.ok({ data: res });
-  // }
-}
-
-module.exports = LoginController;

+ 51 - 0
app/controller/system/.dictionary.js

@@ -0,0 +1,51 @@
+module.exports = {
+  create: {
+    requestBody: ["categroy", "pid", "label", "value", "status", "dstatus"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["categroy", "pid", "label", "value", "status", "dstatus"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        categroy: "categroy",
+        pid: "pid",
+        label: "label",
+        value: "value",
+        status: "status",
+        dstatus: "dstatus",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+  tree: {
+    parameters: {
+      query: {
+        categroy: "categroy",
+        pid: "pid",
+        label: "label",
+        value: "value",
+        status: "status",
+        dstatus: "dstatus",
+      },
+    },
+    service: "getTree",
+  },
+};

+ 16 - 0
app/controller/system/dictionary.js

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

+ 0 - 16
app/controller/user.js

@@ -1,16 +0,0 @@
-'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);

+ 21 - 0
app/model/dictionary.js

@@ -0,0 +1,21 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+// 字典
+const Dictionary = {
+  categroy: { type: String, required: false, maxLength: 200 }, // 字典代码
+  pid: { type: String, required: false, maxLength: 200 }, // 上级
+  label: { type: String, required: false, maxLength: 200 }, // 显示内容
+  value: { type: String, required: false, maxLength: 200 }, // 存储内容
+  status: { type: String, required: true, maxLength: 200 }, // 状态:0=>使用;1禁用
+  dstatus: { type: Boolean, default: false }, // 可删除权限,true:只有开发模式和修改数据库可以进行数据本体操作,否则不允许用户使用,哪怕是用户管理员
+};
+
+const schema = new Schema(Dictionary, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Dictionary', schema, 'dictionary');
+};

+ 0 - 22
app/model/user.js

@@ -1,22 +0,0 @@
-'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: false, maxLength: 200 }, // 部门id
-  gender: { type: String, required: false, maxLength: 200 }, // 性别
-  remark: { type: String, required: false, 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');
-};

+ 4 - 10
app/router.js

@@ -9,16 +9,6 @@ module.exports = app => {
   router.resources('test', '/api/servicezhwl/test', controller.test); // index、create、show、destroy
   router.post('test', '/api/servicezhwl/test/update/:id', controller.test.update);
 
-  // 用户表
-  router.resources('user', '/api/servicezhwl/user', controller.user); // index、create、show、destroy
-  router.post('user', '/api/servicezhwl/user/update/:id', controller.user.update);
-  // 用户登录
-  router.post('/api/servicezhwl/login', controller.login.login);
-  // 根据token取得用户信息
-  router.post('/api/servicezhwl/token', controller.login.token);
-  // 用户退出登录
-  router.post('/api/servicezhwl/logout', controller.login.destroy);
-
   // 司机表
   router.resources('zhwldriver', '/api/servicezhwl/zhwldriver', controller.zhwldriver); // index、create、show、destroy
   router.post('zhwldriver', '/api/servicezhwl/zhwldriver/update/:id', controller.zhwldriver.update);
@@ -26,4 +16,8 @@ module.exports = app => {
   // 车辆表
   router.resources('zhwlcar', '/api/servicezhwl/zhwlcar', controller.zhwlcar); // index、create、show、destroy
   router.post('zhwlcar', '/api/servicezhwl/zhwlcar/update/:id', controller.zhwlcar.update);
+
+  // 系统设置路由
+  require('./router/system')(app);
+
 };

+ 20 - 0
app/router/system.js

@@ -0,0 +1,20 @@
+'use strict';
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const prefix = '/api/servicezhwl';
+  const { router, controller } = app;
+  router.get(`${prefix}/dictionary/tree`, controller.system.dictionary.tree);
+  router.resources(
+    'dictionary',
+    `${prefix}/dictionary`,
+    controller.system.dictionary
+  ); // index、create、show、destroy
+  router.post(
+    'dictionary',
+    `${prefix}/dictionary/update/:id`,
+    controller.system.dictionary.update
+  );
+
+};

+ 0 - 67
app/service/login.js

@@ -1,67 +0,0 @@
-'use strict';
-
-const assert = require('assert');
-const { ObjectId } = require('mongoose').Types;
-const { CrudService } = require('naf-framework-mongoose/lib/service');
-const { BusinessError, ErrorCode } = require('naf-core').Error;
-const jwt = require('jsonwebtoken');
-const uuid = require('uuid');
-
-class LoginService extends CrudService {
-  constructor(ctx) {
-    super(ctx, 'login');
-    this.model = this.ctx.model.User;
-  }
-
-  // 用户登录
-  async login(data) {
-    const { mobile, password } = data;
-    // 根据用户输入的手机号查询其他用户表中是否存在相应数据
-    const user = await this.model.findOne({ mobile }, '+password');
-    if (!user) {
-      throw new BusinessError(ErrorCode.USER_NOT_EXIST);
-    }
-    if (user.password.secret !== password) {
-      throw new BusinessError(ErrorCode.USER_NOT_EXIST);
-    }
-    // 取出用户的类型,根据用户类型返回相应信息
-    const state = uuid();
-    const key = `free:auth:state:${state}`;
-    const token = await this.createJwt(user);
-    await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
-    return { key };
-  }
-
-  // 创建登录Token
-  async createJwt({ id, name, mobile, dept_id, gender, remark }) {
-    const { secret, expiresIn = '1d' } = this.config.jwt;
-    const subject = mobile;
-    const res = { uid: id, name, mobile, dept_id, gender, remark };
-    const token = await jwt.sign(res, secret, { expiresIn, subject });
-    return token;
-  }
-
-  // 创建密码
-  async createJwtPwd(password) {
-    const { secret } = this.config.jwt;
-    const token = await jwt.sign(password, secret);
-    return token;
-  }
-
-  // 取得redis内token信息
-  async token({ key }) {
-    assert(key, 'key不能为空');
-    const token = await this.app.redis.get(key);
-    if (!token) {
-      throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
-    }
-    return { token };
-  }
-
-  // 删除操作
-  async destroy({ key }) {
-    const res = await this.app.redis.del(key);
-    return res;
-  }
-}
-module.exports = LoginService;

+ 73 - 0
app/service/system/dictionary.js

@@ -0,0 +1,73 @@
+'use strict';
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class DictionaryService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'dictionary');
+    this.model = this.ctx.model.Dictionary;
+  }
+
+  // async update({ id, ...data }) {
+  //   await this.model.update({ _id: ObjectId(id) }, data);
+  //   return await this.model.find({ _id: ObjectId(id) });
+  // }
+
+  async delete({ id }) {
+    let children = await this.dbFindChildren(id);
+    children = children.map(i => ObjectId(i._id));
+    children.push(ObjectId(id));
+    const res = await this.model.deleteMany({ _id: children });
+    return res;
+  }
+
+  async dbFindChildren(pid) {
+    let toReturn = [];
+    const res = await this.model.find({ pid }, '_id');
+    toReturn = [ ...toReturn, ...res ];
+    if (res.length > 0) {
+      for (const i of res) {
+        const { _id } = i;
+        const list = await this.dbFindChildren(_id);
+        toReturn = [ ...toReturn, ...list ];
+      }
+    }
+    return toReturn;
+  }
+
+  async getTree(condition) {
+    const dicList = await this.model.find(condition);
+    let list = [];
+    if (dicList && dicList.length > 0) {
+      list = await this.toFindChildren(dicList);
+    }
+    return list;
+  }
+
+  toFindChildren(list) {
+    list = JSON.parse(JSON.stringify(list));
+    const head = list.filter(f => !f.pid);
+    if (head.length <= 0) {
+      throw new BusinessError(
+        ErrorCode.DATA_INVALID,
+        '需要将根目录加入整合函数的参数中'
+      );
+    }
+    return this.findChildren(head, list);
+  }
+
+  findChildren(list, data) {
+    for (const i of list) {
+      let children = data.filter(f => f.pid === i._id);
+      if (children.length > 0) children = this.findChildren(children, data);
+      i.children = children;
+    }
+    return list;
+  }
+}
+
+module.exports = DictionaryService;

+ 0 - 37
app/service/user.js

@@ -1,37 +0,0 @@
-'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;
-const jwt = require('jsonwebtoken');
-
-class UserService extends CrudService {
-  constructor(ctx) {
-    super(ctx, 'user');
-    this.model = this.ctx.model.User;
-  }
-  // 创建登录Token
-  async createJwtPwd(password) {
-    const { secret } = this.config.jwt;
-    const token = await jwt.sign(password, secret);
-    return token;
-  }
-  async create(data) {
-    const { name, mobile, password } = data;
-    assert(name, '用户名不能为空');
-    assert(password, '密码不能为空');
-    const has_phone = await this.model.findOne({ mobile });
-    if (has_phone) {
-      throw new UserError('此身份手机号已被注册,请更换手机号');
-    } else {
-      // const pas = await this.createJwtPwd(password);
-      data.password = { secret: password };
-      const res = await this.model.create(data);
-      return res;
-    }
-  }
-}
-
-module.exports = UserService;