import { store } from '@/store' ; import Config from '@/config/index' ; import Util from '@/lin/utils/util' ; import { IAppState , IUserType , ISideBarListItem , IOriginalPermissions , IStoreState , } from '@/types/store' ; import { IRouterItem } from '@/types/project' ; const name = Symbol ( ) ; interface IStageMap { [ name ] : IRouterItem ; } interface DeepTravelDefault < T > { children ? : T [ ] ; name ? : symbol | string | null ; } export function deepTravel < T extends DeepTravelDefault < T > > ( obj , fuc ) { if ( Array . isArray ( obj ) ) { obj . forEach ( item => { deepTravel ( item , fuc ) ; } ) ; return ; } if ( obj && obj . children ) { fuc ( obj ) ; deepTravel ( obj . children , fuc ) ; return ; } if ( obj . name ) { fuc ( obj ) ; } } let stageMap = { } as IStageMap ; function IterationDelateMenuChildren ( arr ) { if ( arr . length ) { for ( const i in arr ) { const children = arr [ i ] . children ; if ( children && ! children . length ) { delete arr [ i ] ; } else if ( children && children . length ) { IterationDelateMenuChildren ( children ) ; } } } return arr ; } function permissionShaking ( stageConfig , permissions , user ) { const shookConfig = stageConfig . filter ( route => { if ( Util . hasPermission ( permissions , route , user ) ) { if ( route . children && route . children . length ) { route . children = permissionShaking ( route . children , permissions , user ) ; } return true ; } return false ; } ) ; return IterationDelateMenuChildren ( shookConfig ) ; } export const permissionStageConfig = ( ) : IRouterItem [ ] => { const { stageConfig , permissions , user , } = ( store . getState ( ) as IStoreState ) . app ; const tempStageConfig = Util . deepClone ( stageConfig ) ; const shookConfig = permissionShaking ( tempStageConfig , permissions , user ) ; const list = { } as IStageMap ; deepTravel < IRouterItem > ( shookConfig , item => { if ( item . name ) { list [ item . name ] = item ; } } ) ; stageMap = list ; return shookConfig ; } ; export const getSideBarList = ( ) : ISideBarListItem [ ] => { const { sideBarLevel } = ( store . getState ( ) as IStoreState ) . app ; function deepGetSideBar ( target , level = 3 ) { if ( Array . isArray ( target ) ) { const acc = target . map ( item => deepGetSideBar ( item , level - 1 ) ) ; return acc . filter ( item => item !== null ) ; } if ( ! target . inNav ) { return null ; } if ( target . type === 'folder' && level !== 0 ) { const sideConfig = { } as ISideBarListItem ; sideConfig . name = target . name ; sideConfig . title = target . title ; sideConfig . icon = target . icon ; sideConfig . path = target . route || Util . getRandomStr ( 6 ) ; const { children } = target ; if ( children ) { sideConfig . children = children . map ( item => deepGetSideBar ( item , level - 1 ) ) ; sideConfig . children = sideConfig . children . filter ( item => item !== null ) ; } return sideConfig ; } if ( target . type === 'view' ) { const sideConfig = { } as ISideBarListItem ; sideConfig . name = target . name ; sideConfig . title = target . title ; sideConfig . icon = target . icon ; sideConfig . path = target . route ; return sideConfig ; } if ( target . type === 'tab' ) { const sideConfig = { } as ISideBarListItem ; sideConfig . name = target . name ; sideConfig . title = target . title ; sideConfig . icon = target . icon ; sideConfig . type = target . type ; sideConfig . path = target . route ; if ( ! sideConfig . path ) { if ( target . children && target . children . length > 0 && target . children [ 0 ] . route ) { sideConfig . path = target . children [ 0 ] . route ; } } return sideConfig ; } if ( level <= 0 ) { const sideConfig = { } as ISideBarListItem ; sideConfig . name = target . name ; sideConfig . title = target . title ; sideConfig . icon = target . icon ; sideConfig . path = Util . getRandomStr ( 6 ) ; const { children } = target ; if ( children && children . length > 0 && children [ 0 ] . route ) { sideConfig . path = children [ 0 ] . route ; } return sideConfig ; } return null ; } const sideBar = deepGetSideBar ( permissionStageConfig ( ) , sideBarLevel ) ; return sideBar ; } ; export const getStageByName = ( name ) : IRouterItem => { const target = stageMap [ name ] ; return target ; } ; export const getStageByRoute = ( path ) : IRouterItem => { const result = Object . getOwnPropertySymbols ( stageMap ) . find ( key => stageMap [ key ] . route === path ) || Symbol ( '__no_result_flag__' ) ; const target = stageMap [ result ] ; return target ; } ; export const getStageList = ( ) => stageMap ; export const getStageInfo = ( state ) : ( ( name : string ) => IRouterItem [ ] ) => { const { stageConfig } = state ; const cache = { } as IRouterItem [ ] ; const findStage = ( stages , name ) : IRouterItem [ ] | null => { let result = [ ] ; if ( Array . isArray ( stages ) ) { for ( let i = 0 ; i < stages . length ; i += 1 ) { result = findStage ( stages [ i ] , name ) ; if ( result ) { break ; } } return result ; } if ( stages . children ) { result = findStage ( stages . children , name ) ; if ( result ) { result . unshift ( stages ) ; } return result ; } if ( stages . route === name ) { return [ stages ] ; } return null ; } ; return ( name ) => { if ( cache [ name ] ) { return cache [ name ] ; } let stageInfo = findStage ( stageConfig , name ) ; if ( stageInfo ) { cache [ name ] = stageInfo ; } else { stageInfo = [ ] ; } return stageInfo ; } ; } ; export function handlePermissions ( permissions ) { const _permissions = [ ] ; for ( let i = 0 ; i < permissions . length ; i ++ ) { for ( const key in permissions [ i ] ) { for ( let j = 0 ; j < permissions [ i ] [ key ] . length ; j ++ ) { _permissions . push ( permissions [ i ] [ key ] [ j ] . permission ) ; } } } return _permissions ; } export function handleAvatar ( avatar ) { return avatar ?. startsWith ( 'http' ) ? avatar : `${ Config . baseURL }/assets/${ avatar }` ; }