<?php
namespace Home\Controller;
use Common\Controller\BaseController;
class LoginController extends BaseController {
	
    //登陆验证
	public function index(){

		//判断是否已是登陆状态
		if(I('session.id') || I('session.userid')){
			$this->redirect('User/Console/index');
			exit; 
		}
			
		if(IS_POST){	
		$map = I('post.');
		
		/*已启用前端验证方法，验证码不能二个同时使用，这里先备注。
		if(check_verify(I('post.yzm'))==false){
		$this->ajaxReturn(ajax_error("验证码不正确"));
		exit;		
		}
		*/
		
		$username = trim(strtolower($map['username']));
		$password = md5($map['password']);
		$where['userid|phone|email']=$username;
		$where['password']=$password;
		
		$User= D('User');
		$res = $User->where($where)->find();
		
			if($res && $username!="" && $res['pwd']==$password){
				session('id',$res['id']);
				session('userid',$res['userid']);
				session('pwd',$res['pwd']);
				session('uname',$res['uname']);
				session('email',$res['email']);
				session('phone',$res['phone']);
				session('face',$res['face']);	
				$this->ajaxReturn(ajax_success('登陆成功！正在跳转......'));
				exit;
			}else{
				$this->ajaxReturn(ajax_error('登陆失败！请检查输入！'));
				exit;
			}	
			
		}else{
			
		$furl = $_SERVER["HTTP_REFERER"];	
		$assign=array(
		'furl'=>$furl,	
		);
		//输出来路，方便登陆后直接跳回来路继续操作。
		$this->assign($assign);	
		$this->display();
		}
    }
	
	

	//调用验证码，注册和登录共用
	public function yzm(){
	$this->Verify();
	}
	
	
	
	
	//AJAX登陆前端验证 帐号密码和验证码验证
	public function login_chk(){
	
	   if(I('get.username')&&I('get.password')==""){
			
			$map["userid|phone|email"]=I('get.username');
			$user_chk=D('User')->where($map)->find();
			if($user_chk){	
			echo "true";	
			}else{
			echo "false";		
			}
		}
		
		if(I('get.password')&&I('get.username')){
			
			$map["userid|phone|email"]=I('get.username');
			$map["pwd"]=md5(I('get.password'));
			$user_chk=D('User')->where($map)->find();
			if($user_chk){	
			echo "true";	
			}else{
			echo "false";		
			}
		}
		
		//验证码验证
		if(I('get.yzm')){
			$code = I('get.yzm');//获取传递过来的验证码
			if(check_verify($code)){
			//如果返回真
			echo "true";			
			}else{	
			//如果返回假
			echo "false";
			
			}
		}
		
	}
	
	
}