import { Config , Inject , Provide } from '@midwayjs/decorator' ; import { InjectEntityModel } from '@midwayjs/orm' ; import SysMenu from '../../../entity/admin/sys/menu' ; import { BaseService } from '../../base' ; import { Repository } from 'typeorm' ; import { includes , isEmpty , concat , uniq } from 'lodash' ; import { AdminSysRoleService } from './role' ; import { IsNull , Not , DeleteResult } from 'typeorm' ; import { IMenuItemAndParentInfoResult } from '../interface' ; import { CreateMenuDto } from '../../../dto/admin/sys/menu' ; @ Provide ( ) export class AdminSysMenuService extends BaseService { @ Inject ( ) adminSysRoleService : AdminSysRoleService ; @ InjectEntityModel ( SysMenu ) menu : Repository < SysMenu > ; @ Config ( 'rootRoleId' ) rootRoleId : number ; /**
     * 获取所有菜单
     */ async list ( ) { return await this . menu . find ( ) ; } /**
     * 保存或新增菜单
     */ async save ( menu ) { const result = await this . menu . save ( menu ) ; return result ; } /**
     * 根据角色获取所有菜单
     */ async getMenus ( uid ) { const roleIds = await this . adminSysRoleService . getRoleIdByUser ( uid ) ; let menus = [ ] ; if ( includes ( roleIds , this . rootRoleId ) ) { menus = await this . menu . find ( ) ; } else { menus = await this . menu . createQueryBuilder ( 'menu' ) . innerJoinAndSelect ( 'sys_role_menu' , 'role_menu' , 'menu.id = role_menu.menu_id' ) . andWhere ( 'role_menu.role_id IN (:...roldIds)' , { roldIds : roleIds } ) . orderBy ( 'menu.order_num' , 'DESC' ) . getMany ( ) ; } return menus ; } /**
     * 查找当前菜单下的子菜单，目录以及菜单
     */ async findChildMenus ( mid ) { const allMenus = [ ] ; const menus = await this . menu . find ( { parentId : mid } ) ; for ( let i = 0 ; i < menus . length ; i ++ ) { if ( menus [ i ] . type !== 2 ) { const c = await this . findChildMenus ( menus [ i ] . id ) ; allMenus . push ( c ) ; } allMenus . push ( menus [ i ] . id ) ; } return allMenus ; } param mid async getMenuItemInfo ( mid ) { const menu = await this . menu . findOne ( { id : mid } ) ; return menu ; } /**
     * 获取某个菜单以及关联的父菜单的信息
     */ async getMenuItemAndParentInfo ( mid ) { const menu = await this . menu . findOne ( { id : mid } ) ; let parentMenu = undefined ; if ( menu && menu . parentId ) { parentMenu = await this . menu . findOne ( { id : menu . parentId } ) ; } return { menu , parentMenu } ; } /**
     * 查找节点路由是否存在
     */ async findRouterExist ( router ) { const menus = await this . menu . findOne ( { router } ) ; return ! isEmpty ( menus ) ; } /**
     * 获取当前用户的所有权限
     */ async getPerms ( uid ) { const roleIds = await this . adminSysRoleService . getRoleIdByUser ( uid ) ; let perms = [ ] ; let result = null ; if ( includes ( roleIds , this . rootRoleId ) ) { result = await this . menu . find ( { perms : Not ( IsNull ( ) ) , type : 2 } ) ; } else { result = await this . menu . createQueryBuilder ( 'menu' ) . innerJoinAndSelect ( 'sys_role_menu' , 'role_menu' , 'menu.id = role_menu.menu_id' ) . andWhere ( 'role_menu.role_id IN (:...roldIds)' , { roldIds : roleIds } ) . andWhere ( 'menu.type = 2' ) . andWhere ( 'menu.perms IS NOT NULL' ) . getMany ( ) ; } if ( ! isEmpty ( result ) ) { result . forEach ( e => { perms = concat ( perms , e . perms . split ( ',' ) ) ; } ) ; perms = uniq ( perms ) ; } return perms ; } /**
     * 删除多项菜单
     */ async deleteMenuItem ( mids ) { return await this . menu . delete ( mids ) ; } /**
     * 刷新指定用户ID的权限
     */ async refreshPerms ( uid ) { const perms = await this . getPerms ( uid ) ; const online = await this . getAdminRedis ( ) . get ( `admin:token:${ uid }` ) ; if ( online ) { await this . getAdminRedis ( ) . set ( `admin:perms:${ uid }` , JSON . stringify ( perms ) ) ; } } /**
     * 刷新所有在线用户的权限
     */ async refreshOnlineUserPerms ( ) { const onlineUserIds = await this . getAdminRedis ( ) . keys ( 'admin:token:*' ) ; if ( onlineUserIds && onlineUserIds . length > 0 ) { for ( let i = 0 ; i < onlineUserIds . length ; i ++ ) { const uid = onlineUserIds [ i ] . split ( 'admin:token:' ) [ 1 ] ; const perms = await this . getPerms ( parseInt ( uid ) ) ; await this . getAdminRedis ( ) . set ( `admin:perms:${ uid }` , JSON . stringify ( perms ) ) ; } } } }