abp & ng-alain 改造前端 六 —— 用户信
2018-06-30 本文已影响56人
诸葛_小亮
介绍
ABP 是 ASP.NET Boilerplate Project(Asp.net 样板项目)的简称,网址:http://aspnetboilerplate.com/。
ng-alain 是基于 antd 中后台前端解决方案,网址:https://ng-alain.com/。
官方网页下载的项目的angular项目是基于(AdminBSB:https://github.com/gurayyarar/AdminBSBMaterialDesign)
目标
用户登录之后,在头部和左侧标板,显示用户信息,并且提供退出功能
修改头部用户信息
ng-alain
用的是SettingsService
来保存的用户名显示的用户信息,该用户信息是在程序启动时候设置的,见代码
![](https://img.haomeiwen.com/i1397128/008751b768c5ed1a.png)
保持该代码不变,修改user.component.ts
![](https://img.haomeiwen.com/i1397128/171823bf4c9482f8.png)
import { Component, Inject,OnInit,Injector } from '@angular/core';
import { Router } from '@angular/router';
import { SettingsService } from '@delon/theme';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
import { AppComponentBase } from '@shared/app-component-base';
import { AppAuthService } from '@shared/auth/app-auth.service';
@Component({
selector: 'header-user',
template: `
<nz-dropdown nzPlacement="bottomRight">
<div class="item d-flex align-items-center px-sm" nz-dropdown>
<nz-avatar [nzSrc]="'/assets/images/user.png'" nzSize="small" class="mr-sm"></nz-avatar>
{{shownLoginName}}
</div>
<div nz-menu class="width-sm">
<div nz-menu-item [nzDisabled]="true"><i class="anticon anticon-user mr-sm"></i>个人中心</div>
<div nz-menu-item [nzDisabled]="true"><i class="anticon anticon-setting mr-sm"></i>设置</div>
<li nz-menu-divider></li>
<div nz-menu-item (click)="logout()"><i class="anticon anticon-setting mr-sm"></i>退出登录</div>
</div>
</nz-dropdown>
`,
})
export class HeaderUserComponent extends AppComponentBase implements OnInit{
shownLoginName: string = "";
constructor(
injector: Injector,
private _authService: AppAuthService,
public settings: SettingsService,
private router: Router,
@Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
) {
super(injector);
}
ngOnInit() {
this.shownLoginName = this.appSession.getShownLoginName();
}
logout() {
this._authService.logout();
}
}
使用ABP提供的类AppSessionService
获取显示登陆者的信息
退出功能
ng-alain
使用如下代码实现退出
this.tokenService.clear();
this.router.navigateByUrl(this.tokenService.login_url);
修改后的代码如下图
![](https://img.haomeiwen.com/i1397128/79c5418c0e6a98a3.png)
使用
AppAuthService logout
方法退出.
左侧面板的用户信息和退出
同上,使用 AppSessionService
获取需要显示的用户信息,使用 AppAuthService logout
方法退出当前登录信息
效果
![](https://img.haomeiwen.com/i1397128/a503d4f696019a6e.png)
![](https://img.haomeiwen.com/i1397128/65aaf41c7bd0bd94.png)
我的公众号
![](https://img.haomeiwen.com/i1397128/9a9b8a692b08f2be.jpg)