lrf402788946 4 年之前
父节点
当前提交
0c52d3227c

+ 2 - 3
app/controller/.menu.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ["!project", "type", "title",'route','pid', "disabled",'sort', "params"],
+    requestBody: [ "type", "title",'route','pid', "disabled",'sort', "params"],
   },
   destroy: {
     params: ["!id"],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ["!id"],
-    requestBody: ["!project", "type", "title",'route','pid', "disabled",'sort', "params"],
+    requestBody: [ "type", "title",'route','pid', "disabled",'sort', "params"],
   },
   show: {
     parameters: {
@@ -19,7 +19,6 @@ module.exports = {
   index: {
     parameters: {
       query: {
-        project: "project",
         type: "type",
         title: "title",
         route:'route',

+ 3 - 4
app/controller/.role.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ["!project", "!name", "type", "menu", "disabled", "params"],
+    requestBody: ["!name", "type", "menu", "disabled", "params"],
   },
   destroy: {
     params: ["!id"],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ["!id"],
-    requestBody: ["!project", "!name", "type", "menu", "disabled", "params"],
+    requestBody: ["!name", "type", "menu", "disabled", "params"],
   },
   show: {
     parameters: {
@@ -19,7 +19,6 @@ module.exports = {
   index: {
     parameters: {
       query: {
-        project: "project",
         name: "name",
         type: "type",
         menu: "menu",
@@ -35,7 +34,7 @@ module.exports = {
     },
   },
   roleMenuTree: {
-    parameters: { query: { project: "project", type: "type", userid: 'userid' } },
+    parameters: { query: { type: "type", userid: 'userid' } },
     service: "getRoleMenuTree",
   },
 };

+ 5 - 0
app/controller/util.js

@@ -12,6 +12,11 @@ class UtilController extends Controller {
     const data = await this.service.findModel(this.ctx.params);
     this.ctx.ok({ data });
   }
+
+  async utilMethod() {
+    await this.service.utilMethod(this.ctx.query, this.ctx.request.body);
+    this.ctx.ok();
+  }
 }
 
 module.exports = UtilController;

+ 5 - 3
app/model/menu.js

@@ -3,7 +3,6 @@ const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 
 const Menu = {
-  project: { type: String, required: true, maxLength: 200 }, // 项目名
   title: { type: String, required: true, zh: '目录标题', row: 2 }, // 菜单标题
   route: { type: String, required: false, maxLength: 200, zh: '路由地址', row: 3 }, // 路由
   pid: { type: String, required: false, maxLength: 200, zh: '所属上级', options: { custom: true }, row: 1 }, // 上级id
@@ -11,10 +10,13 @@ const Menu = {
   sort: { type: Number, required: false, default: 0, zh: '顺序', options: { type: 'number', remark: '倒序排列,数值大的排在前面' }, row: 5 }, // 顺序,默认为0,使用降序排序,数值越大在前面
   params: { type: Object }, // 参数字段,需要就用,不需要就不用
 };
-const schema = new Schema(Menu, { toJSON: { virtuals: true } });
+const schema = new Schema(Menu, {
+  'multi-tenancy': true,
+  toJSON: { virtuals: true },
+});
 schema.index({ id: 1 });
 schema.plugin(metaPlugin);
-
+schema.index({ _tenant: 1 });
 module.exports = app => {
   const { mongoose } = app;
   return mongoose.model('Menu', schema, 'menu');

+ 4 - 2
app/model/role.js

@@ -3,14 +3,16 @@ const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 
 const Role = {
-  project: { type: String, required: true, maxLength: 200 }, // 项目名
   name: { type: String, required: true, maxLength: 200 }, // 角色名称
   type: { type: String, required: false, maxLength: 200 }, // 类型,每个项目有每个项目的设置,这里只存储,让各自项目记住自己类型对应什么
   menu: { type: Array, required: true }, // 菜单标题
   disabled: { type: Boolean, default: false }, // 使用状态
   params: { type: Object }, // 参数字段,需要就用,不需要就不用
 };
-const schema = new Schema(Role, { toJSON: { virtuals: true } });
+const schema = new Schema(Role, {
+  'multi-tenancy': true,
+  toJSON: { virtuals: true },
+});
 schema.index({ id: 1 });
 schema.plugin(metaPlugin);
 

+ 4 - 2
app/model/user-menu.js

@@ -3,12 +3,14 @@ const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 // 用户特定权限
 const UserMenu = {
-  project: { type: String, required: true, maxLength: 200 }, // 项目名
   userid: { type: String, required: true, maxLength: 200 }, // 用户id
   menu: { type: Array, required: true }, // 菜单标题
   params: { type: Object }, // 参数字段,需要就用,不需要就不用
 };
-const schema = new Schema(UserMenu, { toJSON: { virtuals: true } });
+const schema = new Schema(UserMenu, {
+  'multi-tenancy': true,
+  toJSON: { virtuals: true },
+});
 schema.index({ id: 1 });
 schema.plugin(metaPlugin);
 

+ 1 - 0
app/router.js

@@ -9,6 +9,7 @@ module.exports = app => {
   const prefix = '/api/role/auth';
   // 工具
   router.get(`${prefix}/model/:model`, controller.util.findModel);
+  router.post(`${prefix}/util`, controller.util.utilMethod);
   // menu
   require('./router/menu')(app);
   // user

+ 1 - 1
app/service/menu.js

@@ -38,7 +38,7 @@ class MenuService extends CrudService {
   async findProject(condition) {
     const { project } = condition;
     assert(project, '缺少需要查询的项目名称');
-    const res = await this.model.find(condition).sort({ sort: -1 });
+    const res = await this.model.find({ _tenant: project }).sort({ sort: -1 });
     let list = [];
     if (res && res.length > 0) {
       list = await this.toFindChildren(res);

+ 4 - 5
app/service/role.js

@@ -26,17 +26,16 @@ class RoleService extends CrudService {
     return res;
 
   }
-  async getRoleMenuTree({ project, type, userid }) {
-    assert(project, '缺少需要查找的项目信息');
+  async getRoleMenuTree({ type, userid }) {
     assert(type, '缺少需要查找角色代码');
     // 找到这个角色的所有权限
-    const res = await this.model.findOne({ project, type });
+    const res = await this.model.findOne({ type });
     if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定角色');
     let { menu } = res;
     // console.log(res);
     const object = {};
     // // 找到用户的角色对应的权限
-    let roleMenu = await this.Menumodel.find({ project }).sort({ sort: -1 });
+    let roleMenu = await this.Menumodel.find().sort({ sort: -1 });
     if (roleMenu.length > 0) {
       // 将用户角色部分得到权限限制住,不允许改,必须有
       roleMenu = JSON.parse(JSON.stringify(roleMenu));
@@ -52,7 +51,7 @@ class RoleService extends CrudService {
     object.menu = roleMenu;
     // 如果有userid,说明还需要找到这个用户的特有的权限,合并到selected中
     if (userid) {
-      const umRes = await this.ctx.service.userMenu.findUserMenu({ project, userid });
+      const umRes = await this.ctx.service.userMenu.findUserMenu({ userid });
       if (umRes) {
         const { menu: uMenu } = umRes;
         menu = [ ...menu, ...uMenu ];

+ 9 - 11
app/service/user-menu.js

@@ -16,39 +16,37 @@ class UserMenuService extends CrudService {
   }
 
   async create(data) {
-    const { project, userid } = data;
+    const { userid } = data;
     let res;
-    res = await this.model.findOne({ project, userid });
+    res = await this.model.findOne({ userid });
     if (res) {
-      res = await this.model.update({ project, userid }, data);
+      res = await this.model.update({ userid }, data);
     } else {
       res = await this.model.create(data);
     }
     return res;
   }
 
-  async findUserMenu({ project, userid }) {
-    assert(project, '缺少需要查找的项目信息');
+  async findUserMenu({ userid }) {
     assert(userid, '缺少用户信息');
-    const res = await this.model.findOne({ project, userid });
+    const res = await this.model.findOne({ userid });
     return res;
   }
 
-  async getUserMenu({ project, type, userid }) {
-    assert(project, '缺少需要查找的项目信息');
+  async getUserMenu({ type, userid }) {
     assert(type, '缺少需要查找角色代码');
     assert(userid, '缺少需要查找用户信息');
     // 找到这个角色的权限,找到用户权限,合并在一起
     // 找到这个角色的所有权限
     const menuids = [];
-    const res = await this.Rolemodel.findOne({ project, type });
+    const res = await this.Rolemodel.findOne({ type });
     if (!res) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定角色'); }
     const { menu: roleMenu, params } = res;
     // roleMenu只存id
     menuids.push(...roleMenu);
     // 找到用户的特殊权限
     if (userid !== 'dev') {
-      const umidsRes = await this.ctx.service.userMenu.findUserMenu({ project, userid });
+      const umidsRes = await this.ctx.service.userMenu.findUserMenu({ userid });
       if (umidsRes) {
         const { menu: uMenuids } = umidsRes;
         // uMenuids只存id
@@ -61,7 +59,7 @@ class UserMenuService extends CrudService {
     parentIds = _.compact(parentIds);
     menuids.push(...parentIds);
     // 找到该项目的所有的权限
-    const allMenu = await this.Menumodel.find({ project, disabled: false }).sort({ sort: -1 });
+    const allMenu = await this.Menumodel.find({ disabled: false }).sort({ sort: -1 });
     // 需要将根目录整理出来,将
     const dup = allMenu.filter(f => menuids.find(um => ObjectId(um).equals(f._id)));
     const userMenu = await this.ctx.service.menu.toFindChildren(dup);

+ 4 - 0
app/service/util.js

@@ -26,5 +26,9 @@ class UtilService extends CrudService {
     res = _.orderBy(res, [ 'row' ], [ 'asc' ]);
     return res;
   }
+
+  async utilMethod() {
+    console.log('in function:');
+  }
 }
 module.exports = UtilService;