import { Injectable , Inject } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { catchError } from 'rxjs/operators' ; import { MenuService , SettingsService , TitleService , ALAIN_I18N_TOKEN } from '@delon/theme' ; import { ACLService } from '@delon/acl' ; import { TranslateService } from '@ngx-translate/core' ; import { I18NService } from '@core/i18n/i18n.service' ; import { CoreService } from 'generated' ; import { zip } from 'rxjs' ; import { NzIconService } from 'ng-zorro-antd' ; import { ICONS_AUTO } from '../../../style-icons-auto' ; import { ICONS } from '../../../style-icons' ; import { ArrayService } from '@delon/util' ; /**
 * 用于应用启动时
 * 一般用来获取应用所需要的基础数据等
 */ @ Injectable ( ) export class StartupService { constructor ( iconSrv , private menuService , private translate , @ Inject ( ALAIN_I18N_TOKEN ) private i18n , private settingService , private aclService , private titleService , private httpClient , private arrayService , private coreService ) { iconSrv . addIcon ( ... ICONS_AUTO , ... ICONS ) ; } load ( ) { return new Promise ( resolve => { zip ( this . httpClient . get ( `assets/i18n/${ this . i18n . defaultLang }.json` ) , this . coreService . settingsGetSettingsByName ( 'main' ) , this . coreService . menusGetUserMenus ( ) ) . pipe ( catchError ( ( [ langData , settingsData , menuData ] ) => { resolve ( null ) ; return [ langData , settingsData , menuData ] ; } ) ) . subscribe ( ( [ langData , settings , menuData ] ) => { this . translate . setTranslation ( this . i18n . defaultLang , langData ) ; this . translate . setDefaultLang ( this . i18n . defaultLang ) ; this . settingService . setApp ( settings ) ; this . aclService . setFull ( true ) ; this . titleService . suffix = settings . name ; if ( menuData && Array . isArray ( menuData ) ) { const menus = menuData . map ( item => { return { id : item . id , text : item . name , group : item . group , icon : item . icon , link : item . link , parent : item . parent } ; } ) ; const tree = this . arrayService . arrToTree ( menus , { idMapName : 'id' , parentIdMapName : 'parent' } ) ; this . menuService . add ( [ { text : '主导航' , group : true , children : tree } ] ) ; } } , ( ) => { } , ( ) => { resolve ( null ) ; } ) ; } ) ; } }