From 8702a063e0e545f2ffc57f203b9c849599415923 Mon Sep 17 00:00:00 2001 From: fengxiang <110431245@qq.com> Date: Thu, 01 Feb 2018 15:03:59 +0800 Subject: [PATCH] 登录功能完成 --- src/app/delon.module.ts | 3 + src/app/business/business.module.ts | 4 + src/app/business/entity/token.ts | 4 + src/app/app.module.ts | 3 + src/app/business/services/http/login.service.ts | 34 +++++++++++ src/app/business/entity/data.ts | 7 ++ yarn.lock | 4 src/app/routes/routes-routing.module.ts | 2 src/app/routes/passport/login/login.component.ts | 51 ++++++++++------ src/environments/environment.ts | 2 src/app/routes/dashboard/v1/v1.component.ts | 8 ++ src/app/routes/passport/login/login.component.html | 3 src/app/app.component.ts | 4 + 13 files changed, 101 insertions(+), 28 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index c1ba478..e8afd42 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,3 +1,4 @@ +import { LoginService } from '@business/services/http/login.service'; import { Component, HostBinding, OnInit } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; import { ThemesService, SettingsService, TitleService } from '@delon/theme'; @@ -17,7 +18,8 @@ private theme: ThemesService, private settings: SettingsService, private router: Router, - private titleSrv: TitleService) { + private titleSrv: TitleService, + private loginService:LoginService) { } ngOnInit() { diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 6bcec65..b3d93f0 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -21,6 +21,7 @@ import { TranslateHttpLoader } from '@ngx-translate/http-loader'; import { ALAIN_I18N_TOKEN } from '@delon/theme'; import { I18NService } from '@core/i18n/i18n.service'; +import { LoginService } from '@business/services/http/login.service'; // AoT requires an exported function for factories export function HttpLoaderFactory(http: HttpClient) { @@ -54,6 +55,8 @@ }) ], providers: [ + // loginservice ������������token��������������� + LoginService, { provide: LOCALE_ID, useValue: 'zh-Hans' }, { provide: HTTP_INTERCEPTORS, useClass: SimpleInterceptor, multi: true}, { provide: HTTP_INTERCEPTORS, useClass: DefaultInterceptor, multi: true}, diff --git a/src/app/business/business.module.ts b/src/app/business/business.module.ts index 5f8177b..a0e0997 100644 --- a/src/app/business/business.module.ts +++ b/src/app/business/business.module.ts @@ -1,5 +1,6 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; +import { HttpModule } from '@angular/http'; // Statics import 'rxjs/add/observable/throw'; @@ -14,7 +15,8 @@ @NgModule({ imports: [ - CommonModule + CommonModule, + HttpModule ] }) export class BusinessModule { } diff --git a/src/app/business/entity/data.ts b/src/app/business/entity/data.ts index 2aa99c6..f56e676 100644 --- a/src/app/business/entity/data.ts +++ b/src/app/business/entity/data.ts @@ -125,4 +125,11 @@ address:string; describe:string; name:string; +} + +export interface UserContext { + authorities: any[]; + mode: string; + organizationId: number; + username: string; } \ No newline at end of file diff --git a/src/app/business/entity/token.ts b/src/app/business/entity/token.ts new file mode 100644 index 0000000..44cf4b1 --- /dev/null +++ b/src/app/business/entity/token.ts @@ -0,0 +1,4 @@ +export interface Authorization{ + token:string; + refreshToken:string; +} \ No newline at end of file diff --git a/src/app/business/services/http/login.service.ts b/src/app/business/services/http/login.service.ts new file mode 100644 index 0000000..d84f47a --- /dev/null +++ b/src/app/business/services/http/login.service.ts @@ -0,0 +1,34 @@ +import { Authorization } from '@business/entity/token'; +import { HttpClient } from '@angular/common/http'; +import { environment } from 'environments/environment'; +import { Observable } from 'rxjs/Observable'; +import { Injectable } from '@angular/core'; +import { UserContext } from '@business/entity/data'; +import { error } from 'selenium-webdriver'; + +@Injectable() +export class LoginService { + public authorization:Authorization; + public userContext:UserContext; + private urls = { + login:environment.SERVER_BASH_URL+'/auth/login', + userContext:environment.SERVER_BASH_URL+'/user-context', + }; + constructor(private http:HttpClient) { } + public validate(username:string,password:string):Observable<Authorization>{ + return this.http.post(this.urls.login,{username:username,password:password,mode:"Web"}).map( + (res:any) => { + this.authorization = res; + return res; + } + ); + } + public loadUserContext():void{ + this.http.get(this.urls.userContext).subscribe( + (res:UserContext) => { + this.userContext = res; + return res; + } + ); + } +} diff --git a/src/app/delon.module.ts b/src/app/delon.module.ts index 26ab5ac..c268107 100644 --- a/src/app/delon.module.ts +++ b/src/app/delon.module.ts @@ -186,6 +186,9 @@ // auth AlainAuthModule.forRoot({ // ignores: [ `\\/login`, `assets\\/` ], + token_send_key : 'X-Authorization', + token_send_template : 'Bearer ${token}', + token_send_place : 'header', login_url: `/passport/login`, allow_anonymous_key: `_allow_anonymous` }), diff --git a/src/app/routes/dashboard/v1/v1.component.ts b/src/app/routes/dashboard/v1/v1.component.ts index 388ae8a..e8e6596 100644 --- a/src/app/routes/dashboard/v1/v1.component.ts +++ b/src/app/routes/dashboard/v1/v1.component.ts @@ -1,3 +1,4 @@ +import { LoginService } from '@business/services/http/login.service'; import { NzMessageService } from 'ng-zorro-antd'; import { Component, OnInit } from '@angular/core'; import { _HttpClient } from '@delon/theme'; @@ -8,7 +9,8 @@ }) export class DashboardV1Component implements OnInit { - constructor(private http: _HttpClient, public msg: NzMessageService) { } + constructor(private http: _HttpClient, public msg: NzMessageService, + public loginService:LoginService) { } todoData: any[] = [ { completed: true, avatar: '1', name: '���������', content: `������������������������������������������` }, @@ -26,6 +28,10 @@ offlineChartData: any[] = []; ngOnInit() { + // ������������������,������������������������ + setTimeout(() => { + this.loginService.loadUserContext(); + }, 1); // this.http.get('/chart').subscribe((res: any) => { // this.webSite = res.visitData.slice(0, 10); // this.salesData = res.salesData; diff --git a/src/app/routes/passport/login/login.component.html b/src/app/routes/passport/login/login.component.html index 76283cd..1b6d4aa 100644 --- a/src/app/routes/passport/login/login.component.html +++ b/src/app/routes/passport/login/login.component.html @@ -10,7 +10,7 @@ <ng-container *ngIf="userName.dirty || userName.touched"> <p nz-form-explain *ngIf="userName.errors?.required">���������������������</p> <p nz-form-explain *ngIf="userName.errors?.minlength">������������������</p> - </ng-container> + </ng-container> </div> </div> <div nz-form-item> @@ -21,6 +21,7 @@ </ng-template> </nz-input> <div nz-form-explain *ngIf="(password.dirty || password.touched) && password.errors?.required">������������������</div> + <p nz-form-explain [ngStyle]="{'color': 'red'}" *ngIf="validateError">������������������������������������</p> </div> </div> <div nz-form-item nz-row> diff --git a/src/app/routes/passport/login/login.component.ts b/src/app/routes/passport/login/login.component.ts index 678152b..41331e2 100644 --- a/src/app/routes/passport/login/login.component.ts +++ b/src/app/routes/passport/login/login.component.ts @@ -1,3 +1,5 @@ +import { Authorization } from '@business/entity/token'; +import { LoginService } from './../../../business/services/http/login.service'; import { SettingsService } from '@delon/theme'; import { Component, OnDestroy, Inject } from '@angular/core'; import { Router } from '@angular/router'; @@ -5,12 +7,11 @@ import { NzMessageService } from 'ng-zorro-antd'; import { SocialService, SocialOpenType, ITokenService, DA_SERVICE_TOKEN } from '@delon/auth'; import { environment } from '@env/environment'; - @Component({ selector: 'passport-login', templateUrl: './login.component.html', styleUrls: [ './login.component.less' ], - providers: [ SocialService ] + providers: [ SocialService] }) export class UserLoginComponent implements OnDestroy { @@ -25,6 +26,7 @@ public msg: NzMessageService, private settingsService: SettingsService, private socialService: SocialService, + private loginService:LoginService, @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService) { this.form = fb.group({ userName: [null, [Validators.required, Validators.minLength(5)]], @@ -75,28 +77,37 @@ this.captcha.markAsDirty(); if (this.mobile.invalid || this.captcha.invalid) return; } - // mock http this.loading = true; - setTimeout(() => { - this.loading = false; - if (this.type === 0) { - if (this.userName.value !== 'admin' || this.password.value !== '123456') { - this.error = `���������������������`; - return; + this.loginService.validate(this.userName.value,this.password.value).subscribe( + (res:Authorization) => { + if(res.token!=null){ + this.tokenService.set({ + token: res.token, + name: this.userName.value, + time: +new Date + }); + this.validateError = true; + this.router.navigate(['/']); + } + }, + (err) => { + console.log(err); + if(err instanceof ProgressEvent){ + const error = <ProgressEvent>err; + let xmlHttp = error.target; + if(xmlHttp instanceof XMLHttpRequest){ + const xmlHttpRequest = <XMLHttpRequest> xmlHttp; + if(xmlHttpRequest.status == 401){ + this.validateError = true; + this.loading = false; + } + } + } } - } + ); - this.tokenService.set({ - token: '123456789', - name: this.userName.value, - email: `cipchk@qq.com`, - id: 10000, - time: +new Date - }); - this.router.navigate(['/']); - }, 1000); } - + public validateError:boolean; // region: social open(type: string, openType: SocialOpenType = 'href') { diff --git a/src/app/routes/routes-routing.module.ts b/src/app/routes/routes-routing.module.ts index 918d7de..9138cfb 100644 --- a/src/app/routes/routes-routing.module.ts +++ b/src/app/routes/routes-routing.module.ts @@ -38,7 +38,7 @@ { path: 'login', component: UserLoginComponent } ] }, - { path: '**', redirectTo: 'dashboard' } + { path: '**', redirectTo: 'passport/login' } ]; @NgModule({ diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 6163163..2a6fb94 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -9,5 +9,5 @@ production: false, hmr: false, useHash: true, - SERVER_BASH_URL: `http://47.96.171.62:8080/screen_api_v2/` + SERVER_BASH_URL: `http://127.0.0.1:8001/` }; diff --git a/yarn.lock b/yarn.lock index 5fa8eed..1484f9d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7866,9 +7866,9 @@ semver "^5.3.0" xml2js "^0.4.17" -webpack-bundle-analyzer@^2.9.0: +webpack-bundle-analyzer@^2.9.2: version "2.9.2" - resolved "http://registry.npm.taobao.org/webpack-bundle-analyzer/download/webpack-bundle-analyzer-2.9.2.tgz#63ed86eb71cc4cda86f68e685a84530ba0126449" + resolved "https://registry.yarnpkg.com/webpack-bundle-analyzer/-/webpack-bundle-analyzer-2.9.2.tgz#63ed86eb71cc4cda86f68e685a84530ba0126449" dependencies: acorn "^5.1.1" chalk "^1.1.3" -- Gitblit v1.8.0