import { Component } from '@angular/core' ; import { Platform , NavController , Loading , Toast , Storage , LocalStorage } from 'ionic-angular' ; import { TabsPage } from '../tabs/tabs' ; import { RegisterPage } from '../login/register' ; import { Http , Headers , RequestOptions } from '@angular/http' ; import { Helper } from '../helper/helper' ; @ Component ( { templateUrl : 'build/pages/login/login.html' } ) export class LoginPage { public username : string = "15685412820" ; public password : string = "123456" ; public loginUrl : string = "http://119.29.140.85/index.php/user/login" ; public userInfo : any ; local : Storage ; loading = Loading . create ( { content : '' , showBackdrop : false } ) ; helper : Helper ; constructor ( private nav , public platform , private http ) { this . local = new Storage ( LocalStorage ) ; this . helper = new Helper ( nav ) ; } login ( ) { let headers = new Headers ( { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' } ) ; let options = new RequestOptions ( { headers : headers } ) ; if ( this . username != "" && this . password != "" ) { this . nav . present ( this . loading ) ; this . http . post ( this . loginUrl , this . headleAjaxBodyParam ( { "phone" : this . username , "password" : this . password } ) , options ) . subscribe ( ( data ) => { this . loading . dismiss ( ) ; let myData = data . json ( ) ; if ( myData . status ) { let jsonData = myData . data ; this . saveUserInfoToLocalStorage ( jsonData ) ; this . nav . setPages ( [ { page : TabsPage } ] , { animate : true } ) ; } else { this . loading . dismiss ( ) ; this . presentToast ( "用户名或密码错误" ) ; } } , ( err ) => { this . loading . dismiss ( ) ; } ) ; } else { this . presentToast ( "用户名或密码不能为空" ) ; } } saveUserInfoToLocalStorage ( data ) { this . local . set ( "id" , data . id ) ; this . local . set ( "phone" , data . phone ) ; this . local . set ( "password" , data . password ) ; this . local . set ( "nick" , data . nick ) ; this . local . set ( "head_pic_url" , data . head_pic_url ) ; this . local . set ( "rank" , data . rank ) ; this . local . set ( "identity_status" , data . identity_status ) ; this . local . set ( "register_time" , data . register_time ) ; this . local . set ( "school_name" , data . school_name ) ; this . local . set ( "school_code" , data . school_code ) ; this . local . set ( "floor_address" , data . floor_address ) ; } goToRegister ( ) { this . nav . push ( RegisterPage ) ; } headleAjaxBodyParam ( param ) { let bodyStr = [ ] ; for ( let item in param ) { bodyStr . push ( item + '=' + param [ item ] ) ; } return bodyStr . join ( '&' ) ; } presentToast ( text ) { let toast = Toast . create ( { message : text , duration : 2000 , position : 'bottom' } ) ; toast . onDismiss ( ( ) => { } ) ; this . nav . present ( toast ) ; } forgetPassword ( ) { } }