fengxiang
2018-02-01 8702a063e0e545f2ffc57f203b9c849599415923
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') {