Skip to content

Commit

Permalink
chore: fix ng add
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Nov 24, 2023
1 parent af51156 commit 1e453ff
Show file tree
Hide file tree
Showing 28 changed files with 420 additions and 513 deletions.
67 changes: 67 additions & 0 deletions schematics/application/files/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { default as ngLang } from '@angular/common/locales/zh';
import { ApplicationConfig, EnvironmentProviders, Provider } from '@angular/core';
import { provideAnimations } from '@angular/platform-browser/animations';
import { provideRouter, withComponentInputBinding, withInMemoryScrolling, withHashLocation, RouterFeatures } from '@angular/router';
import { <% if (i18n) { %>I18NService, <% } %>defaultInterceptor, provideStartup } from '@core';
import { provideCellWidgets } from '@delon/abc/cell';
import { provideSTWidgets } from '@delon/abc/st';
import { authSimpleInterceptor, provideAuth } from '@delon/auth';<% if (form) { %>
import { provideSFConfig } from '@delon/form';<% } %>
import { AlainProvideLang, provideAlain, zh_CN as delonLang } from '@delon/theme';
import { AlainConfig } from '@delon/util/config';
import { environment } from '@env/environment';
import { CELL_WIDGETS, ST_WIDGETS<% if (form) { %>, SF_WIDGETS<% } %> } from '@shared';
import { zhCN as dateLang } from 'date-fns/locale';
import { NzConfig, provideNzConfig } from 'ng-zorro-antd/core/config';
import { zh_CN as zorroLang } from 'ng-zorro-antd/i18n';
import { ICONS } from 'src/style-icons';
import { ICONS_AUTO } from 'src/style-icons-auto';

import { provideBindAuthRefresh } from './core/net';
import { routes } from './routes/routes';

const defaultLang: AlainProvideLang = {
abbr: 'zh-CN',
ng: ngLang,
zorro: zorroLang,
date: dateLang,
delon: delonLang
};

const alainConfig: AlainConfig = {
auth: { login_url: '/passport/login' }
};

const ngZorroConfig: NzConfig = {};

const routerFeatures: RouterFeatures[] = [withComponentInputBinding(), withInMemoryScrolling({ scrollPositionRestoration: 'top' })];
if (environment.useHash) routerFeatures.push(withHashLocation());

const providers: Array<Provider | EnvironmentProviders> = [
provideHttpClient(withInterceptors([...(environment.interceptorFns ?? []), authSimpleInterceptor, defaultInterceptor])),
provideAnimations(),
provideRouter(routes, ...routerFeatures),
provideAlain({ config: alainConfig, defaultLang<% if (i18n) { %>, i18nClass: I18NService<% } %>, icons: [...ICONS_AUTO, ...ICONS] }),
provideNzConfig(ngZorroConfig),
provideAuth(),
provideCellWidgets(...CELL_WIDGETS),
provideSTWidgets(...ST_WIDGETS),<% if (form) { %>
provideSFConfig({
widgets: [
...SF_WIDGETS
// withUploadWidget()
]
}),<% } %>
provideStartup(),
...(environment.providers || [])
];

// If you use `@delon/auth` to refresh the token, additional registration `provideBindAuthRefresh` is required
if (environment.api?.refreshTokenEnabled && environment.api.refreshTokenType === 'auth-refresh') {
providers.push(provideBindAuthRefresh());
}

export const appConfig: ApplicationConfig = {
providers: providers
};
114 changes: 0 additions & 114 deletions schematics/application/files/src/app/app.module.ts

This file was deleted.

15 changes: 0 additions & 15 deletions schematics/application/files/src/app/core/core.module.ts

This file was deleted.

5 changes: 2 additions & 3 deletions schematics/application/files/src/app/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<% if (i18n) { %>export * from './i18n/i18n.service';<% } %>
export * from './module-import-guard';
export * from './net/default.interceptor';
export * from './startup/startup.service';
export * from './start-page.guard';
export * from './start-page.guard';<% if (i18n) { %>
export * from './i18n/i18n.service';<% } %>
122 changes: 56 additions & 66 deletions schematics/application/files/src/app/core/startup/startup.service.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,78 @@
import { Injectable, Inject } from '@angular/core';
import { APP_INITIALIZER, Injectable, Provider, inject } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
import { DA_SERVICE_TOKEN } from '@delon/auth';
import { ALAIN_I18N_TOKEN, MenuService, SettingsService, TitleService } from '@delon/theme';
import { ACLService } from '@delon/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';
import { NzIconService } from 'ng-zorro-antd/icon';

import { ICONS } from '../../../style-icons';
import { ICONS_AUTO } from '../../../style-icons-auto';

/**
* 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 {
constructor(
iconSrv: NzIconService,
private menuService: MenuService,<% if (i18n) { %>
@Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,<% } %>
private settingService: SettingsService,
private aclService: ACLService,
private titleService: TitleService,
@Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
private httpClient: HttpClient,
private router: Router
) {
iconSrv.addIcon(...ICONS_AUTO, ...ICONS);
private menuService = inject(MenuService);
private settingService = inject(SettingsService);
private tokenService = inject(DA_SERVICE_TOKEN);
private aclService = inject(ACLService);
private titleService = inject(TitleService);
private httpClient = inject(HttpClient);
private router = inject(Router);<% if (i18n) { %>
private i18n = inject<I18NService>(ALAIN_I18N_TOKEN);<% } %>
// If http request allows anonymous access, you need to add `ALLOW_ANONYMOUS`:
// this.httpClient.get('assets/tmp/app-data.json', { 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-alain.com/acl/getting-started
this.aclService.setFull(true);
// Menu data, https://ng-alain.com/theme/menu
this.menuService.add(res.menu);
// Can be set page suffix title, https://ng-alain.com/theme/title
this.titleService.suffix = res.app.name;
}

<% if (i18n) { %>
private viaHttp(): Observable<void> {
const defaultLang = this.i18n.defaultLang;
return zip(this.i18n.loadLangData(defaultLang), 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 [];
}),
map(([langData, appData]: [Record<string, string>, NzSafeAny]) => {
// setting language data
this.i18n.use(defaultLang, langData);
private viaHttp(): Observable<void> {
const defaultLang = this.i18n.defaultLang;
return zip(this.i18n.loadLangData(defaultLang), this.appData$).pipe(
map(([langData, appData]: [Record<string, string>, NzSafeAny]) => {
// setting language data
this.i18n.use(defaultLang, langData);

// Application data
// Application information: including site name, description, year
this.settingService.setApp(appData.app);
// User information: including name, avatar, email address
this.settingService.setUser(appData.user);
// ACL: Set the permissions to full, https://ng-alain.com/acl/getting-started
this.aclService.setFull(true);
// Menu data, https://ng-alain.com/theme/menu
this.menuService.add(appData.menu);
// Can be set page suffix title, https://ng-alain.com/theme/title
this.titleService.suffix = appData.app.name;
})
);
}
this.handleAppData(appData);
})
);
}
<% } else { %>
private viaHttp(): Observable<void> {
// If http request allows anonymous access, you need to add `ALLOW_ANONYMOUS`:
// this.httpClient.get('assets/tmp/app-data.json', { context: new HttpContext().set(ALLOW_ANONYMOUS, true) })
return 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({});
}),
map((res: NzSafeAny) => {
// 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-alain.com/acl/getting-started
this.aclService.setFull(true);
// Menu data, https://ng-alain.com/theme/menu
this.menuService.add(res.menu);
// Can be set page suffix title, https://ng-alain.com/theme/title
this.titleService.suffix = res.app.name;
})
);
}
private viaHttp(): Observable<void> {
return this.appData$.pipe(map((res: NzSafeAny) => this.handleAppData(res)));
}
<% } %>

<% if (i18n) { %>
Expand Down
Loading

0 comments on commit 1e453ff

Please sign in to comment.