import { observable , action , autorun } from 'mobx' ; import * as Api from '@/api/common.api' ; import { ICommonStore , ICenterConfig } from './interface/common.interface' ; import { Iswagger , Itag } from '@/interface/swagger.interface' ; class Common implements ICommonStore { @ observable public collapsed : boolean = false ; @ observable public apiDataList : Iswagger [ ] = [ ] ; @ observable public center : Iswagger | null = null ; @ observable public currentTag : string = '' ; @ observable public currentCenterConfig : ICenterConfig | null = null ; @ observable public filterNav : Itag [ ] = [ ] ; @ observable public filterValue : string = '' ; @ observable public canOpen : boolean = false ; @ observable public enablePrivate : boolean = false ; @ observable public detailItem : any = null ; @ observable public swaggerApiConfig : any = [ ] ; constructor ( ) { autorun ( ( ) => { if ( this . currentCenterConfig && this . apiDataList . length > 0 ) { const findResult = this . apiDataList . find ( ( item ) => { if ( ! this . currentCenterConfig ) { return false ; } return item . configName . replace ( '/' , '' ) === this . currentCenterConfig . name ; } ) ; this . center = findResult || null ; this . filterNav = this . center ? this . center . tags : [ ] ; } } ) ; autorun ( ( ) => { if ( this . filterValue && this . center ) { this . filterNav = this . center . tags . filter ( ( v ) => v . name . toLocaleLowerCase ( ) . indexOf ( this . filterValue . toLocaleLowerCase ( ) ) !== - 1 ) ; } } ) ; } @ action public async logout ( ) { const redirectUrl = decodeURIComponent ( location . href ) ; const requestUrl = location . origin + '/erp-gateway/logout?redirectUrl=' + redirectUrl ; window . location . href = requestUrl ; return true ; } @ action public getSwaggerDatas = ( ) => { this . swaggerApiConfig . forEach ( ( item , index ) => { this . requestSwagger ( item ) ; } ) ; return true ; } ; @ action public requestSwagger = async ( item ) => { let result = null ; try { result = await Api . requestSwagger < any > ( item ) ; } catch ( e ) { return false ; } result . data . configName = item . name ; if ( ! result . data . tags ) { result . data . tags = [ ] ; } result . data . tags = this . mappingTags ( result . data ) ; this . apiDataList . push ( result . data ) ; return true ; } ; @ action public mappingTags = ( result ) => { const tags = [ ... result . tags ] ; const res = { ... result } ; Object . keys ( res . paths ) . forEach ( ( v , i , a ) => { Object . keys ( res . paths [ v ] ) . forEach ( ( item ) => { const tagObj = res . paths [ v ] [ item ] . tags . map ( ( tag ) => { return { name : tag } ; } ) ; tags . push ( ... tagObj ) ; } ) ; } ) ; const arr = [ ] ; const json = { } ; tags . forEach ( ( v ) => { if ( ! json [ v . name ] ) { arr . push ( v ) ; json [ v . name ] = v ; } } ) ; return arr ; } ; @ action public getSwaggerApiConfig = async ( ) => { let result = null ; try { result = await Api . getSwaggerApiConfig < any > ( ) ; } catch ( e ) { return false ; } this . canOpen = result . canOpen || false ; this . enablePrivate = result . enablePrivate || false ; this . swaggerApiConfig = result . data ; this . getSwaggerDatas ( ) ; let num = 3 ; if ( window [ 'scopePath' ] ) { num = 4 ; } const center = location . href . split ( '/' ) [ num ] ; console . log ( center ) ; if ( center ) { this . currentCenterConfig = this . swaggerApiConfig . filter ( ( v ) => v . name === center ) [ 0 ] ; } else { this . currentCenterConfig = this . swaggerApiConfig [ 0 ] ; if ( this . currentCenterConfig ) { try { history . replaceState ( null , 'firstapi' , `${ window [ 'scopePath' ] ? '/' + window [ 'scopePath' ] : '' }/${ this . currentCenterConfig . name }` ) ; } catch ( e ) { return true ; } } } return true ; } ; @ action public updateSwaggerApiConfig = async ( ) => { try { await Api . updateSwaggerApiConfig < any > ( this . swaggerApiConfig ) ; } catch ( e ) { return false ; } return true ; } ; } export default new Common ( ) ;