import { ALL , Body , Get , Inject , Post , Provide , Query , Validate , Controller , } from '@midwayjs/decorator' ; import { CreateApiDoc } from '@midwayjs/swagger' ; import { flattenDeep , isEmpty } from 'lodash' ; import { res } from '../../../common/utils' ; import { CreateMenuDto , DeleteMenuDto , InfoMenuDto , UpdateMenuDto , } from '../../../dto/admin/sys/menu' ; import { ResOp } from '../../../interface' ; import { AdminSysMenuService } from '../../../service/admin/sys/menu' ; import { BaseController , ADMIN_PREFIX_URL } from '../../base' ; import { GetMenuInfoExample , GetMenuListExample } from '../swagger' ; @ Provide ( ) @ Controller ( `${ ADMIN_PREFIX_URL }/sys/menu` , { tagName : 'AdminSysMenu' , description : '后台菜单控制器' , } ) export class AdminSysMenuController extends BaseController { @ Inject ( ) adminSysMenuService : AdminSysMenuService ; @ ( CreateApiDoc ( ) . summary ( '获取对应权限的菜单列表' ) . respond ( 200 , '' , 'json' , { example : GetMenuListExample , } ) . build ( ) ) @ Get ( '/list' ) async list ( ) { return res ( { data : await this . adminSysMenuService . getMenus ( this . ctx . admin . uid ) , } ) ; } @ ( CreateApiDoc ( ) . summary ( '新增菜单或权限' ) . respond ( 200 , '' , 'json' , { example : res ( ) , } ) . build ( ) ) @ Post ( '/add' ) @ Validate ( ) async add ( @ Body ( ALL ) dto ) { if ( dto . type === 2 && dto . parentId === - 1 ) { return res ( { code : 10005 } ) ; } if ( dto . type === 1 && dto . parentId !== - 1 ) { const parent = await this . adminSysMenuService . getMenuItemInfo ( dto . parentId ) ; if ( isEmpty ( parent ) ) { return res ( { code : 10014 } ) ; } if ( parent && parent . type === 1 ) { return res ( { code : 10006 } ) ; } } if ( dto . parentId === - 1 ) { dto . parentId = undefined ; } await this . adminSysMenuService . save ( dto ) ; if ( dto . type === 2 ) { await this . adminSysMenuService . refreshOnlineUserPerms ( ) ; } return res ( ) ; } @ ( CreateApiDoc ( ) . summary ( '更新菜单或权限' ) . respond ( 200 , '' , 'json' , { example : res ( ) , } ) . build ( ) ) @ Post ( '/update' ) @ Validate ( ) async update ( @ Body ( ALL ) dto ) { if ( dto . type === 2 && dto . parentId === - 1 ) { return res ( { code : 10005 } ) ; } if ( dto . type === 1 && dto . parentId !== - 1 ) { const parent = await this . adminSysMenuService . getMenuItemInfo ( dto . parentId ) ; if ( isEmpty ( parent ) ) { return res ( { code : 10014 } ) ; } if ( parent && parent . type === 1 ) { return res ( { code : 10006 } ) ; } } if ( dto . parentId === - 1 ) { dto . parentId = null ; } const insertData = { ... dto , id : dto . menuId , } ; await this . adminSysMenuService . save ( insertData ) ; if ( dto . type === 2 ) { await this . adminSysMenuService . refreshOnlineUserPerms ( ) ; } return res ( ) ; } @ ( CreateApiDoc ( ) . summary ( '删除菜单或权限' ) . respond ( 200 , '' , 'json' , { example : res ( ) , } ) . build ( ) ) @ Post ( '/delete' ) @ Validate ( ) async delete ( @ Body ( ALL ) dto ) { if ( dto . menuId <= 68 ) { return res ( { code : 10016 } ) ; } const childMenus = await this . adminSysMenuService . findChildMenus ( dto . menuId ) ; await this . adminSysMenuService . deleteMenuItem ( flattenDeep ( [ dto . menuId , childMenus ] ) ) ; await this . adminSysMenuService . refreshOnlineUserPerms ( ) ; return res ( ) ; } @ ( CreateApiDoc ( ) . summary ( '根据ID菜单或权限信息' ) . respond ( 200 , '' , 'json' , { example : GetMenuInfoExample , } ) . build ( ) ) @ Get ( '/info' ) @ Validate ( ) async info ( @ Query ( ALL ) dto ) { return res ( { data : await this . adminSysMenuService . getMenuItemAndParentInfo ( dto . menuId ) , } ) ; } }