import { SettingsService , _HttpClient } from '@delon/theme' ; import { Component , OnDestroy , Inject , Optional } from '@angular/core' ; import { Router } from '@angular/router' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { NzMessageService , NzModalService } from 'ng-zorro-antd' ; import { SocialService , SocialOpenType , TokenService , DA_SERVICE_TOKEN , } from '@delon/auth' ; import { ReuseTabService } from '@delon/abc' ; import { environment } from '@env/environment' ; import { StartupService } from '@core/startup/startup.service' ; @ Component ( { selector : 'passport-login' , templateUrl : './login.component.html' , styleUrls : [ './login.component.less' ] , providers : [ SocialService ] , } ) export class UserLoginComponent implements OnDestroy { form : FormGroup ; error = '' ; type = 0 ; loading = false ; constructor ( fb , private router , public msg , private modalSrv , private settingsService , private socialService , @ Optional ( ) @ Inject ( ReuseTabService ) private reuseTabService , @ Inject ( DA_SERVICE_TOKEN ) private tokenService , private startupSrv , private http ) { this . form = fb . group ( { userName : [ null , [ Validators . required , Validators . minLength ( 5 ) ] ] , password : [ null , Validators . required ] , mobile : [ null , [ Validators . required , Validators . pattern ( /^1\d{10}$/ ) ] ] , captcha : [ null , [ Validators . required ] ] , remember : [ true ] , } ) ; modalSrv . closeAll ( ) ; } get userName ( ) { return this . form . controls . userName ; } get password ( ) { return this . form . controls . password ; } get mobile ( ) { return this . form . controls . mobile ; } get captcha ( ) { return this . form . controls . captcha ; } switch ( ret ) { this . type = ret . index ; } count = 0 ; interval$ : any ; getCaptcha ( ) { this . count = 59 ; this . interval$ = setInterval ( ( ) => { this . count -= 1 ; if ( this . count <= 0 ) clearInterval ( this . interval$ ) ; } , 1000 ) ; } submit ( ) { this . error = '' ; if ( this . type === 0 ) { this . userName . markAsDirty ( ) ; this . userName . updateValueAndValidity ( ) ; this . password . markAsDirty ( ) ; this . password . updateValueAndValidity ( ) ; if ( this . userName . invalid || this . password . invalid ) return ; } else { this . mobile . markAsDirty ( ) ; this . mobile . updateValueAndValidity ( ) ; this . captcha . markAsDirty ( ) ; this . captcha . updateValueAndValidity ( ) ; if ( this . mobile . invalid || this . captcha . invalid ) return ; } this . loading = true ; this . http . post ( 'passport/login' , { email : 'cipchk@qq.com' , password : 'wodemima' } ) . subscribe ( ( res ) => { this . loading = false ; this . reuseTabService . clear ( ) ; this . tokenService . set ( { token : res . data . token , } ) ; this . startupSrv . load ( ) . then ( ( ) => this . router . navigate ( [ '/' ] ) ) ; } ) ; } open ( type , openType = 'href' ) { let url = `` ; let callback = `` ; if ( environment . production ) callback = 'https://cipchk.github.io/ng-alain/callback/' + type ; else callback = 'http://localhost:4200/callback/' + type ; switch ( type ) { case 'auth0' : url = `//cipchk.auth0.com/login?client=8gcNydIDzGBYxzqV0Vm1CX_RXH-wsWo5&redirect_uri=${ decodeURIComponent ( callback ) }` ; break ; case 'github' : url = `//github.com/login/oauth/authorize?client_id=9d6baae4b04a23fcafa2&response_type=code&redirect_uri=${ decodeURIComponent ( callback ) }` ; break ; case 'weibo' : url = `https://api.weibo.com/oauth2/authorize?client_id=1239507802&response_type=code&redirect_uri=${ decodeURIComponent ( callback ) }` ; break ; } if ( openType === 'window' ) { this . socialService . login ( url , '/' , { type : 'window' , } ) . subscribe ( res => { if ( res ) { this . settingsService . setUser ( res ) ; this . router . navigateByUrl ( '/' ) ; } } ) ; } else { this . socialService . login ( url , '/' , { type : 'href' , } ) ; } } ngOnDestroy ( ) { if ( this . interval$ ) clearInterval ( this . interval$ ) ; } }