|
@@ -0,0 +1,70 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const _ = require('lodash');
|
|
|
+const moment = require('moment');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+
|
|
|
+class UserMenuService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'usermenu');
|
|
|
+ this.model = this.ctx.model.UserMenu;
|
|
|
+ this.Rolemodel = this.ctx.model.Role;
|
|
|
+ this.Menumodel = this.ctx.model.Menu;
|
|
|
+ }
|
|
|
+
|
|
|
+ async create(data) {
|
|
|
+ const { project, userid } = data;
|
|
|
+ let res;
|
|
|
+ res = await this.model.findOne({ project, userid });
|
|
|
+ if (res) {
|
|
|
+ res = await this.model.update({ project, userid }, data);
|
|
|
+ } else {
|
|
|
+ res = await this.model.create(data);
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ async findUserMenu({ project, userid }) {
|
|
|
+ assert(project, '缺少需要查找的项目信息');
|
|
|
+ assert(userid, '缺少用户信息');
|
|
|
+ const res = await this.model.findOne({ project, userid });
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ async getUserMenu({ project, type, userid }) {
|
|
|
+ assert(project, '缺少需要查找的项目信息');
|
|
|
+ assert(type, '缺少需要查找角色代码');
|
|
|
+ assert(userid, '缺少需要查找用户信息');
|
|
|
+ // 找到这个角色的权限,找到用户权限,合并在一起
|
|
|
+ // 找到这个角色的所有权限
|
|
|
+ const menuids = [];
|
|
|
+ const res = await this.Rolemodel.findOne({ project, type });
|
|
|
+ if (!res) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定角色'); }
|
|
|
+ const { menu: roleMenu } = res;
|
|
|
+ // roleMenu只存id
|
|
|
+ menuids.push(...roleMenu);
|
|
|
+ // 找到用户的特殊权限
|
|
|
+ const umidsRes = await this.ctx.service.userMenu.findUserMenu({ project, userid });
|
|
|
+ if (umidsRes) {
|
|
|
+ const { menu: uMenuids } = umidsRes;
|
|
|
+ // uMenuids只存id
|
|
|
+ menuids.push(...uMenuids);
|
|
|
+ }
|
|
|
+ // 找到所有菜单的父级,下面过滤用,把父级目录拿出来(不单纯是根目录)
|
|
|
+ const parentIdsRes = await this.Menumodel.find({ _id: menuids.map(i => ObjectId(i)) }, 'pid');
|
|
|
+ let parentIds = parentIdsRes.map(i => i.pid);
|
|
|
+ parentIds = _.compact(parentIds);
|
|
|
+ menuids.push(...parentIds);
|
|
|
+ // 找到该项目的所有的权限
|
|
|
+ const allMenu = await this.Menumodel.find({ project, 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);
|
|
|
+ return userMenu;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = UserMenuService;
|