import { Component , OnInit } from '@angular/core' ; import { LoginService } from './login.service' ; import { Router } from '@angular/router' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { LoginServiceImpl } from 'app/providers/login.service-impl' ; import { loginConfig } from 'config/global-config' ; import { SessionService } from 'app/base/shared/session.service' ; import { Session } from 'app/base/shared/model/session' ; import { HttpResult } from 'app/base/shared/model/http-result' ; import { FormUtil } from 'app/base/shared/form-util' ; @ Component ( { moduleId : module . id , selector : 'app-login' , styleUrls : [ 'login.component.css' ] , templateUrl : 'login.component.html' } ) export class LoginComponent implements OnInit { formErrors = { 'userName' : '' , 'password' : '' , } ; validationMessages = { 'userName' : { 'required' : '代码不能为空' } , 'password' : { 'required' : '名称不能为空' } } ; backgroundImage : string = loginConfig . background_image ; formTitle : string = loginConfig . form_title ; validateForm : FormGroup ; userName : string ; password : string ; _logining = false ; constructor ( private fb , private router , private sessionService , private loginService ) { } ngOnInit ( ) { this . validateForm = this . fb . group ( { userName : [ this . userName , [ Validators . required ] ] , password : [ this . password , [ Validators . required ] ] , } ) ; this . validateForm . valueChanges . subscribe ( ( value ) => this . checkForm ( ) ) ; } private checkForm ( ) { FormUtil . valid ( this . validateForm , this . formErrors , this . validationMessages ) ; } submitForm ( ) { if ( this . validateForm . invalid ) { FormUtil . markAsDirtyDeep ( this . validateForm ) ; this . checkForm ( ) ; return ; } const loginData = { login : this . validateForm . get ( 'userName' ) . value , password : this . validateForm . get ( 'password' ) . value } ; this . _logining = true ; this . loginService . login ( loginData ) . subscribe ( result => { this . _logining = false ; this . sessionService . putSession ( result ) ; this . router . navigateByUrl ( '/frame' ) ; } , error => { this . _logining = false ; window . alert ( '登录失败：' + error ) ; } ) ; } }