<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_Wang extends MY_Controller
{

	function __construct()
	{
		parent::__construct();

		$this->load->model("user_model");
		$this->load->library("user_class");
		$this->load->helper("template_helper");
		$this->user_id = checklogin();
	}

    //登录页面
	function index()
	{
		$data['title'] = '用户登录';
		if($_POST)
		{
			$method	= $this->input->post('method');
			$telno	= $this->input->post('telno');
			$password	= $this->input->post('password');
			$telCode	= $this->input->post('telCode');
			$loginkind	= $this->input->post('loginkind');
			$password2	= $this->input->post('password2');
            $name 	= $this->input->post('name');
            $action	= $this->input->post('action');
            $refer	= $this->input->post('refer');
            $refer1	= $this->input->post('refer1');
            
            $ajaxyz	= $this->input->post('ajaxyz');

            //隐藏表单的提交
            if($this->input->post('telnotj'))
            {
              $telno = $this->input->post('telnotj');
            }
            if($this->input->post('passwordtj'))
            {
              $password2 = $this->input->post('passwordtj');
            }
            if($this->input->post('telcodetj'))
            {
              $telCode = $this->input->post('telcodetj');
            }
            if($this->input->post('loginkindtj'))
            {
              $loginkind = $this->input->post('loginkindtj');
            }

			if( $loginkind == 1 )
			{
					//验证是否有此手机用户
					$result = $this->user_model->yztelno($telno);

					if( $result == 0 )
					{
						echo 'N_USER';
					    exit;
					}
                   
					//动态密码登录
					$user_isset = $this->user_model->yzlogindt($telno,$telCode);
                    $result = $user_isset['id'];
                    $password = $user_isset['password'];

					if( $result == 0 )
					{
				        echo 'N_PASS';
					    exit;
					}elseif($ajaxyz==1 && $result>0){
						/********* xue add 2015-02-10动态登录时登录全站个人会员中心START***********/
						//查出密码，
                        $esf_password = $this->user_model->getEsfPassword($telno);
						//到全站会员中心注册
                        $ucenter_result = $this->user_model->ucenterLogin($telno,$esf_password);
						/********* xue add 2015-02-10END***********/
                        echo 'Y';
					    exit;   
                    }
				
			}
			else
			{
                
                if($ajaxyz==1)
                { 
                   $telno = trim(iconv('UTF-8', 'gbk', $telno));
				   /********* xue add 2015-02-10普通登录时先登录全站个人会员中心START***********/
				   //在第一遍验证的时候登录全站
				   $ucenter_result = $this->user_model->ucenterLogin($telno,md5($password2));
				
			  
				  // var_dump($ucenter_result);exit;
				  
				   /********* xue add 2015-02-10普通登录时先登录全站个人会员中心END***********/
                }                

				$esf_result	= $this->user_model->yzlogin($telno,$password2);
				/********* xue add 2015-02-10普通登录时二手房和全站个人会员中心处理START***********/
				if($ajaxyz==1){

					if($ucenter_result['result']==1 && $esf_result>0){
				         echo 'Y';
						 exit();
				     }elseif($ucenter_result['result']==1 && $esf_result==0){
						// echo "1111";exit;
				         $esfCheckTel = $this->user_model->yztelno($telno);
						
						 if($esfCheckTel==0){
                              //无此用户向二手房库插入一条用户信息
							  $passport_uid = $ucenter_result['data']['passport_uid'];
                              $password = md5($password2);
							  $telno = $telno;
                              $city = $ucenter_result['data']['city'];
							  $email = $ucenter_result['data']['email'];
							  $username = $ucenter_result['data']['username'];
							  $nickname = $ucenter_result['data']['nickname'];
							  $avatar = $ucenter_result['data']['avatar'];
							  $ip = $ucenter_result['data']['ip'];							
						      $this->user_model->addUserFromUcenter($passport_uid,$password,$telno,$city,$email,$username,$nickname,$avatar,$ip);
						 }else{
						    //密码不同,修改二手房密码
                              $this->user_model->updateEsfPassowrd($telno,$password2);
                              //记录日志
							  $msg = date('Y-m-d H:i:s').":".$telno." \n";
							  @file_put_contents('./password_err',$msg,FILE_APPEND); 		

						 }	
						 echo 'Y';
						 exit();
				
				     }elseif($ucenter_result['result']==0 && $esf_result>0){
					      //二手房有，全站会员中心无,记录日志
						  $msg = date('Y-m-d H:i:s').":".$telno." \n";
						  @file_put_contents('./ucenter_err',$msg,FILE_APPEND); 
						  echo 'Y';
						  exit();
						  					 
					 }else{
					 
					     echo 'N_USER';
						 exit();
					 
					 }				
				
				}
				
                /********* xue add 2015-02-10普通登录时二手房和全站个人会员中心处理END***********/


				/*
					1、调取用户中心登录;是否登录成功	$ucenter_result 
					2、执行二手房判断	$esf_result

					if( $ucenter_result == $esf_result > 0 ) {
						continue;
					} else if ( $ucenter_result > 0 AND  $esf_result == 0 ) {
						if( 你没有值 ) {
							插入数据，做日志1
						} else {
							//密码不一样，更新你的密码
						}
					} else if ( $ucenter_result == 0 AND  $esf_result > 0 ) {
						//正常登录，做日志2
					} else if ( $ucenter_result == 0 AND  $esf_result == 0 ) {
						echo 'N_USER';
					}
					
				*/
				//$result	= $this->user_model->yzlogin($telno,$password2);
 
                $password = md5($password2);
                $name = $telno;
                /***********xue add 2015-02-10 START************/
				$result = $esf_result;
				/***********xue add 2015-02-10 END************/

                /*
   				if( $result == 0 )
				{
  					echo 'N_USER';
					exit;
				}elseif($ajaxyz==1 && $result>0){
                 	echo 'Y';
					exit;               
                }*/
			}

			if( $result > 0 && $action!='loginyz' && $ajaxyz!='1')
			{
                
				 $this->user_model->updatelogintime($result);

				 $this->user_class->user_login($result,$password,$telno,$name);

                 //如果上一页是登陆页面，就直接跳首页
                 if((!empty($refer1) or !empty($refer)) && $refer!=USER_CENTER.'user/logout' && $refer1!=USER_CENTER.'user/logout')
                 {
                   //jumptotk(USER_CENTER.'user',"您已登录,进入会员中心首页!",3000,'tips');
                   if(!empty($refer)){
                   jumpto($refer);
                   }else{
                   jumpto($refer1);
                   }
                 }else{
                   //jumptotk($_SERVER['HTTP_REFERER'],"登录成功,页面将跳转到之前的页面。",3000,'tips');
                   jumpto(USER_CENTER.'user');
                 }
                 exit;
			}
		}else{
        if( $this->user_id > 0 )
		{
          //jumptotk(USER_CENTER.'user',"您已登录,进入会员中心首页!",3000,'tips');
          jumpto(USER_CENTER.'user');
		}
        }

		$this->load->view('login_wang',$data);
	}

	//iframe_login_ajax
	function iframe_login_ajax()
	{
		$telno	= $this->input->get('telno');
		$password	= $this->input->get('password');
		$tel_code	= $this->input->get('tel_code');
		$is_code_login	= $this->input->get('is_code_login');
 
		$telno = trim(iconv('UTF-8', 'gbk', $telno));

		if( $is_code_login == 1 )
		{       
			//动态密码登录
			$user_isset = $this->user_model->yzlogindt($telno,$tel_code);
            $result = $user_isset['id'];
			$password = $user_isset['password'];
			if( $result )
			{
				$this->user_model->updatelogintime($result);
				$this->user_class->user_login($result,$password, $telno);

				//隐藏手机号部分数字
				$pattern = "/(1\d{1,2})\d\d(\d{0,3})/";
				$replacement = "\$1*****\$3";
				$telno2 = preg_replace($pattern, $replacement, trim($telno));
				$showname = $telno2 ? $telno2 : "" ;
				$esfmesnum =  $this->user_model->getmessagenumbyuidwd($result);
				$password_encrypt = passport_encrypt($password);

				$ajax_return['error'] = "0";
				$ajax_return['result'] = $result;
				$ajax_return['showname'] = $showname;
				$ajax_return['esfmesnum'] = $esfmesnum;
				$ajax_return['password_encrypt'] = $password_encrypt;

			}else{
				$ajax_return['error'] = "1";            
			}
		}else{
			$result	= $this->user_model->yzlogin($telno,$password);
			if( $result )
			{
				$this->user_model->updatelogintime($result);
				$this->user_class->user_login($result, md5($password), $telno);

				//隐藏手机号部分数字
				$pattern = "/(1\d{1,2})\d\d(\d{0,3})/";
				$replacement = "\$1*****\$3";
				$telno2 = preg_replace($pattern, $replacement, trim($telno));
				$showname = $telno2 ? $telno2 : "" ;
				$esfmesnum =  $this->user_model->getmessagenumbyuidwd($result);
				$password_encrypt = passport_encrypt(md5($password));

				$ajax_return['error'] = "0";
				$ajax_return['result'] = $result;
				$ajax_return['showname'] = $showname;
				$ajax_return['esfmesnum'] = $esfmesnum;
				$ajax_return['password_encrypt'] = $password_encrypt;

			}else{
				$ajax_return['error'] = "1";            
			}
		}
		
		echo serialize($ajax_return);
		exit;
	}

	/**
	* 用户登录接口
	* @author cxf
	*/
	function api_login()
	{

		$telno = $this->input->get('telno');
		$result = $this->user_model->yztelno($telno);

		if( $result > 0 )
		{
			$this->user_model->updatelogintime($result);
			$return	= $this->user_class->user_login($result,$password,$telno);
		}
		else
		{
			$password	= $this->user_model->getyzmpassword($telno);
			$result	= $this->user_model->adduser($telno,$paasword);
			$this->user_model->updatelogintime($result);
			$this->user_class->user_login($result,$password,$telno);
		}
	}
	
    /**
	* 用户链接判断
	* @author cxf
	*/
	function login_judge()
	{
      $this->load->view('login_judge');
	}
	
	/**
	* 用户链接判断
	* @author cxf
	*/
	function login_js()
	{
      $this->load->view('login_js');
	}

    /**
	* 用户链接判断
	* @author cxf
	*/
	function login_js_fenzhan()
	{
      $this->load->view('login_fenzhan_js');
	}

    /**
	* 外地站老头部
	* @author cxf
	*/
	function login_js_oldfenzhan()
	{
      $this->load->view('login_oldfenzhan_js');
	}

}