2018-01-03

home.ts文件

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';

import { AlertController } from 'ionic-angular/components/alert/alert-controller';

import { LoadingController } from 'ionic-angular/components/loading/loading-controller';

import { ToastController } from 'ionic-angular/components/toast/toast-controller';

import { MainPage } from '../main/main';

import { Storage } from '@ionic/storage';

import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({

  selector: 'page-home',

  templateUrl: 'home.html'

})

export class HomePage {

  loginForm: FormGroup;

  username: any;

  password: any;

  constructor(public navCtrl: NavController,

              public alertCtrl: AlertController,

              public loadingCtrl: LoadingController,

              public toastCtrl: ToastController,

              public storage: Storage,

              private formBuilder: FormBuilder

  ) {

    this.loginForm = formBuilder.group({

      username: ['', Validators.compose([Validators.minLength(11), Validators.maxLength(11), Validators.required, Validators.pattern("^(13[0-9]|15[012356789]|17[03678]|18[0-9]|14[57])[0-9]{8}$")])],

      password: ['', Validators.compose([Validators.required, Validators.minLength(6)])]

    })

    this.username = this.loginForm.controls['username'];

    this.password = this.loginForm.controls['password'];

  }

  login(username:HTMLInputElement,password:HTMLInputElement){

    this.storage.get('usernamedata').then((userName)=>{

        //从注册的文件中取出密码userpassword,将它的值放到passWord中

        this.storage.get('userpassword').then((passWord)=>{

        if(username.value.length==0){

          let alert=this.alertCtrl.create({

            title:'error!',

            message:'请输入用户名!',

            buttons:['确定']

          });

          alert.present();

        }

        else if(password.value.length==0){

          let alert=this.alertCtrl.create({

            title:'error!',

            message:'请输入密码!',

            buttons:['确定!']

          });

          alert.present();

        }

        else if(username.value=="15709610748"&&password.value=="123456"){

          let loader=this.loadingCtrl.create({

            content:"请稍等...",

            duration:1000

          });

          let toast=this.toastCtrl.create({

            message:'欢迎登录,用户:'+username.value,

            position:'top',

            duration:2500

          });

          loader.present();

          setTimeout(()=>{

            this.navCtrl.setRoot(MainPage,{username1:username.value});

            toast.present();

          },1000);

        }

        else{

          let alert=this.alertCtrl.create({

            title:'请重新登陆!',

            message:'用户名或密码错误!'

          });

          setTimeout(()=>{

            alert.present();

          },150);

          setTimeout(()=>{

            alert.dismiss();

          },2000);

        }

      });

    });

  }

}

home.html


2018-01-03_第1张图片

你可能感兴趣的:(2018-01-03)