import { APP_INITIALIZER, Injectable, Provider, inject } from '@angular/core'; import { Router } from '@angular/router'; import { HttpClient } from '@angular/common/http'; import { YA_SERVICE_TOKEN } from '@yelon/auth'; import { YUNZAI_I18N_TOKEN, MenuService, SettingsService, TitleService } from '@yelon/theme'; import { ACLService } from '@yelon/acl';<% if (i18n) { %> import { I18NService } from '../i18n/i18n.service';<% } %> import { Observable, zip, of, catchError, map } from 'rxjs'; import type { NzSafeAny } from 'ng-zorro-antd/core/types'; /** * Used for application startup * Generally used to get the basic data of the application, like: Menu Data, User Data, etc. */ export function provideStartup(): Provider[] { return [ StartupService, { provide: APP_INITIALIZER, useFactory: (startupService: StartupService) => () => startupService.load(), deps: [StartupService], multi: true } ]; } @Injectable() export class StartupService { private menuService = inject(MenuService); private settingService = inject(SettingsService); private tokenService = inject(YA_SERVICE_TOKEN); private aclService = inject(ACLService); private titleService = inject(TitleService); private httpClient = inject(HttpClient); private router = inject(Router);<% if (i18n) { %> private i18n = inject(YUNZAI_I18N_TOKEN);<% } %> // If http request allows anonymous access, you need to add `ALLOW_ANONYMOUS`: // this.httpClient.get('/app', { context: new HttpContext().set(ALLOW_ANONYMOUS, true) }) private appData$ = this.httpClient.get('./assets/tmp/app-data.json').pipe( catchError((res: NzSafeAny) => { console.warn(`StartupService.load: Network request failed`, res); setTimeout(() => this.router.navigateByUrl(`/exception/500`)); return of({}); }) ); private handleAppData(res: NzSafeAny): void { // Application information: including site name, description, year this.settingService.setApp(res.app); // User information: including name, avatar, email address this.settingService.setUser(res.user); // ACL: Set the permissions to full, https://ng.yunzainfo.com/acl/getting-started this.aclService.setFull(true); // Menu data, https://ng.yunzainfo.com/theme/menu this.menuService.add(res.menu ?? []); // Can be set page suffix title, https://ng.yunzainfo.com/theme/title this.titleService.suffix = res.app?.name; } <% if (i18n) { %> private viaHttp(): Observable { const defaultLang = this.i18n.defaultLang; return zip(this.i18n.loadLangData(defaultLang), this.appData$).pipe( map(([langData, appData]: [Record, NzSafeAny]) => { // setting language data this.i18n.use(defaultLang, langData); this.handleAppData(appData); }) ); } <% } else { %> private viaHttp(): Observable { return this.appData$.pipe(map((res: NzSafeAny) => this.handleAppData(res))); } <% } %> <% if (i18n) { %> private viaMockI18n(): Observable { const defaultLang = this.i18n.defaultLang; return this.i18n.loadLangData(defaultLang).pipe( map((langData: NzSafeAny) => { this.i18n.use(defaultLang, langData); this.viaMock(); }) ); } <% } %> private viaMock(): Observable { // const tokenData = this.tokenService.get(); // if (!tokenData.token) { // this.router.navigateByUrl(this.tokenService.login_url!); // return; // } // mock const app: any = { name: `NG-YUNZAI`, description: `NG-ZORRO admin panel front-end framework` }; const user: any = { name: 'Admin', avatar: './assets/tmp/img/avatar.jpg', email: 'cipchk@qq.com', token: '123456789' }; // Application information: including site name, description, year this.settingService.setApp(app); // User information: including name, avatar, email address this.settingService.setUser(user); // ACL: Set the permissions to full, https://ng.yunzainfo.com/acl/getting-started this.aclService.setFull(true); // Menu data, https://ng.yunzainfo.com/theme/menu this.menuService.add([ { text: 'Main', group: true, children: [ { text: 'Dashboard', link: '/dashboard', icon: { type: 'icon', value: 'appstore' } } ] } ]); // Can be set page suffix title, https://ng.yunzainfo.com/theme/title this.titleService.suffix = app.name; return of(void 0); } load(): Observable { // http // return this.viaHttp(); // mock: Don’t use it in a production environment. ViaMock is just to simulate some data to make the scaffolding work normally // mock:请勿在生产环境中这么使用,viaMock 单纯只是为了模拟一些数据使脚手架一开始能正常运行 return <% if (i18n) { %>this.viaMockI18n();<% } else { %>this.viaMock();<% } %> } }