startup.service.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { HttpClient } from '@angular/common/http';
  17. import { Inject, Injectable } from '@angular/core';
  18. import { Router } from '@angular/router';
  19. import { ACLService } from '@delon/acl';
  20. import { ALAIN_I18N_TOKEN, MenuService, SettingsService, TitleService } from '@delon/theme';
  21. import { NzSafeAny } from 'ng-zorro-antd/core/types';
  22. import { NzIconService } from 'ng-zorro-antd/icon';
  23. import { Observable, zip } from 'rxjs';
  24. import { catchError, map } from 'rxjs/operators';
  25. import { ICONS } from '../../../style-icons';
  26. import { ICONS_AUTO } from '../../../style-icons-auto';
  27. import { I18NService } from '../i18n/i18n.service';
  28. /**
  29. * Used for application startup
  30. * Generally used to get the basic data of the application, like: Menu Data, User Data, etc.
  31. */
  32. @Injectable()
  33. export class StartupService {
  34. constructor(
  35. iconSrv: NzIconService,
  36. private menuService: MenuService,
  37. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  38. private settingService: SettingsService,
  39. private aclService: ACLService,
  40. private titleService: TitleService,
  41. private httpClient: HttpClient,
  42. private router: Router
  43. ) {
  44. iconSrv.addIcon(...ICONS_AUTO, ...ICONS);
  45. }
  46. load(): Observable<void> {
  47. const defaultLang = this.i18n.defaultLang;
  48. return zip(this.i18n.loadLangData(defaultLang), this.httpClient.get('./assets/app-data.json')).pipe(
  49. // 接收其他拦截器后产生的异常消息
  50. catchError(res => {
  51. console.warn(`StartupService.load: Network request failed`, res);
  52. setTimeout(() => this.router.navigateByUrl(`/exception/500`));
  53. return [];
  54. }),
  55. map(([langData, appData]: [Record<string, string>, NzSafeAny]) => {
  56. // setting language data
  57. this.i18n.use(defaultLang, langData);
  58. // 应用信息:包括站点名、描述、年份
  59. this.settingService.setApp(appData.app);
  60. // 用户信息:包括姓名、头像、邮箱地址
  61. //this.settingService.setUser(appData.user);
  62. // ACL:设置权限为全量
  63. this.aclService.setFull(false);
  64. // 初始化菜单
  65. this.menuService.add(appData.menu);
  66. // 设置页面标题的后缀
  67. this.titleService.default = '';
  68. this.titleService.suffix = appData.app.name;
  69. })
  70. );
  71. }
  72. }