startup.service.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { Injectable, Inject } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { HttpClient } from '@angular/common/http';
  4. import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
  5. import { ALAIN_I18N_TOKEN, MenuService, SettingsService, TitleService } from '@delon/theme';
  6. import { ACLService } from '@delon/acl';
  7. import { I18NService } from '../i18n/i18n.service';
  8. import { Observable, zip, of } from 'rxjs';
  9. import { catchError, map } from 'rxjs/operators';
  10. import type { NzSafeAny } from 'ng-zorro-antd/core/types';
  11. import { NzIconService } from 'ng-zorro-antd/icon';
  12. import { ICONS } from '../../../style-icons';
  13. import { ICONS_AUTO } from '../../../style-icons-auto';
  14. /**
  15. * Used for application startup
  16. * Generally used to get the basic data of the application, like: Menu Data, User Data, etc.
  17. */
  18. @Injectable()
  19. export class StartupService {
  20. constructor(
  21. iconSrv: NzIconService,
  22. private menuService: MenuService,
  23. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  24. private settingService: SettingsService,
  25. private aclService: ACLService,
  26. private titleService: TitleService,
  27. @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
  28. private httpClient: HttpClient,
  29. private router: Router
  30. ) {
  31. iconSrv.addIcon(...ICONS_AUTO, ...ICONS);
  32. }
  33. private viaHttp(): Observable<void> {
  34. const defaultLang = this.i18n.defaultLang;
  35. return zip(this.i18n.loadLangData(defaultLang), this.httpClient.get('http://localhost:4200/assets/tmp/app-data.json')).pipe(
  36. catchError((res: NzSafeAny) => {
  37. console.warn(`StartupService.load: Network request failed`, res);
  38. setTimeout(() => this.router.navigateByUrl(`/exception/500`));
  39. return [];
  40. }),
  41. map(([langData, appData]: [Record<string, string>, NzSafeAny]) => {
  42. // setting language data
  43. this.i18n.use(defaultLang, langData);
  44. // Application data
  45. // Application information: including site name, description, year
  46. this.settingService.setApp(appData.app);
  47. // User information: including name, avatar, email address
  48. // this.settingService.setUser(appData.user);
  49. // ACL: Set the permissions to full, https://ng-alain.com/acl/getting-started
  50. this.aclService.setFull(true);
  51. // Menu data, https://ng-alain.com/theme/menu
  52. this.menuService.add(appData.menu);
  53. // Can be set page suffix title, https://ng-alain.com/theme/title
  54. this.titleService.suffix = appData.app.name;
  55. })
  56. );
  57. }
  58. private viaMockI18n(): Observable<void> {
  59. const defaultLang = this.i18n.defaultLang;
  60. return this.i18n.loadLangData(defaultLang).pipe(
  61. map((langData: NzSafeAny) => {
  62. this.i18n.use(defaultLang, langData);
  63. this.viaMock();
  64. })
  65. );
  66. }
  67. private viaMock(): Observable<void> {
  68. // const tokenData = this.tokenService.get();
  69. // if (!tokenData.token) {
  70. // this.router.navigateByUrl(this.tokenService.login_url!);
  71. // return;
  72. // }
  73. // mock
  74. const app: any = {
  75. name: `HertzBeat`,
  76. description: `面向开发者,易用友好的高性能监控云服务`
  77. };
  78. const user: any = {
  79. name: 'Admin',
  80. avatar: './assets/tmp/img/avatar.svg',
  81. email: 'tomsun28@outlook.com',
  82. token: '123456789'
  83. };
  84. // Application information: including site name, description, year
  85. this.settingService.setApp(app);
  86. // User information: including name, avatar, email address
  87. this.settingService.setUser(user);
  88. // ACL: Set the permissions to full, https://ng-alain.com/acl/getting-started
  89. this.aclService.setFull(true);
  90. // Menu data, https://ng-alain.com/theme/menu
  91. this.menuService.add([
  92. {
  93. text: 'Main',
  94. group: true,
  95. children: [
  96. {
  97. text: 'Dashboard',
  98. link: '/dashboard',
  99. icon: { type: 'icon', value: 'appstore' }
  100. }
  101. ]
  102. }
  103. ]);
  104. // Can be set page suffix title, https://ng-alain.com/theme/title
  105. this.titleService.suffix = app.name;
  106. return of();
  107. }
  108. load(): Observable<void> {
  109. // http
  110. return this.viaHttp();
  111. // mock: Don’t use it in a production environment. ViaMock is just to simulate some data to make the scaffolding work normally
  112. // mock:请勿在生产环境中这么使用,viaMock 单纯只是为了模拟一些数据使脚手架一开始能正常运行
  113. // return this.viaMockI18n();
  114. }
  115. }