<?php
class UserAction extends PublicAction {

    // 用户首页 查看信息

        public function index() {          
        $this->checkUser();
		$User	 =	 M("User");
		$userinfo	=	$User->getById($_SESSION[C('USER_AUTH_KEY')]);
		$this->assign('userinfo',$userinfo);
		$this->display();
        }
	/**
     +----------------------------------------------------------
	 * 取得操作成功后要返回的URL地址
	 * 默认返回当前模块的默认操作
	 * 可以在action控制器中重载
     +----------------------------------------------------------
	 * @access public
     +----------------------------------------------------------
	 * @return string
     +----------------------------------------------------------
	 * @throws ThinkExecption
     +----------------------------------------------------------
	 */
	function getReturnUrl() {
		return __URL__ . '?' . C ( 'VAR_MODULE' ) . '=' . MODULE_NAME . '&' . C ( 'VAR_ACTION' ) . '=' . C ( 'DEFAULT_ACTION' );
	}

	/**
     +----------------------------------------------------------
	 * 根据表单生成查询条件
	 * 进行列表过滤
     +----------------------------------------------------------
	 * @access protected
     +----------------------------------------------------------
	 * @param string $name 数据对象名称
     +----------------------------------------------------------
	 * @return HashMap
     +----------------------------------------------------------
	 * @throws ThinkExecption
     +----------------------------------------------------------
	 */
	protected function _search($name = '') {
		//生成查询条件
		if (empty ( $name )) {
			$name = $this->getActionName();
		}
		$name=$this->getActionName();
		$model = D ( $name );
		$map = array ();
		foreach ( $model->getDbFields () as $key => $val ) {
			if (isset ( $_REQUEST [$val] ) && $_REQUEST [$val] != '') {
				$map [$val] = $_REQUEST [$val];
			}
		}
		return $map;

	}
 


	function read() {
		$this->edit ();
	}

   // 更换密码
    public function changePwd()
    {
		$this->checkUser();
        //对表单提交处理进行处理或者增加非表单数据
		if(md5($_POST['verify'])	!= $_SESSION['verify']) {
			$this->error('验证码错误！');
		}
		$map	=	array();
        $map['password']= pwdHash($_POST['oldpassword']);
        if(isset($_POST['account'])) {
            $map['account']	 =	 $_POST['account'];
        }elseif(isset($_SESSION[C('USER_AUTH_KEY')])) {
            $map['id']		=	$_SESSION[C('USER_AUTH_KEY')];
        }
        //检查用户
        $User    =   M("User");
        if(!$User->where($map)->field('id')->find()) {
            $this->error('旧密码不符或者用户名错误！');
        }else {
			$User->password	=	pwdHash($_POST['password']);
			$User->save();
			$this->success('密码修改成功！');
         }
    }
   public function profile() {
		$this->checkUser();
		$User	 =	 M("User");
		$profilevo	=	$User->getById($_SESSION[C('USER_AUTH_KEY')]);
		$this->assign('profilevo',$profilevo);
		$this->display();
	}

// 修改资料
	public function change() {
		$this->checkUser();
		$User	 =	 D("User");
		if(!$User->create()) {
			$this->error($User->getError());
		}
		$result	=	$User->save();
		if(false !== $result) {
			$this->success('资料修改成功！');
		}else{
			$this->error('资料修改失败!');
		}
	}
	


// 后台用户模块


	function _filter(&$map){
		  $map['id'] = array('egt',2);
		$map['account'] = array('like',"%".$_POST['account']."%");
	}
	// 检查帐号
	public function checkAccount() {
        if(!preg_match('/^[a-z]\w{4,}$/i',$_GET['nc'])) {
            $this->error( '用户名必须是字母，且5位以上！');
        }
		$User = M("User");
        // 检测用户名是否冲突
        $name  =  $_REQUEST['nc'];
        $result  =  $User->getByAccount($name);
        if($result) {
        	$this->error('该用户名已经存在！');
        }else {
           	$this->success('该用户名可以使用！');
        }
    }

    //重置密码
    public function resetPwd()
    {
    	$id  =  $_POST['id'];
        $password = $_POST['password'];
        if(''== trim($password)) {
        	$this->error('密码不能为空！');
        }
        $User = M('User');
		$User->password	=	md5($password);
		$User->id			=	$id;
		$result	=	$User->save();
        if(false !== $result) {
            $this->success("密码修改为$password");
        }else {
        	$this->error('重置密码失败！');
        }
    }

    	public function agreereg(){
    	
		if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
			$this->display();
		}else{
			$this->error('您已是注册用户!');
		}	
		
	}


	public function savereg(){
		
		if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
					
		   $User	=	D("User");
		   if($User->create()) {
			  if(false !==$User->add()) {
		   	      $this->assign("jumpUrl",__APP__);
    			  $this->success('恭喜，注册成功!');
               }else{
                  $this->error('数据写入错误');
               }
		      }else{
			      header("Content-Type:text/html; charset=utf-8");
			      exit($User->getError('数据写入错误').' [ <A HREF="javascript:history.back()">返 回</A> ]');
		     }		
		  }else{
			$this->error('您已是注册用户!');
		}	
		
	}


	public function mypwd(){
	  
	  $account=$_POST[account];
	  $userDao = D('User');
	  $info = $userDao->where("account='$account'")->find();
	  $this->assign('info',$info);
	  $this->display();
	}
//重置密码
	public function showpwd(){
    
        $account=$_POST[account];
	    $answer=$_POST[answer];
	    $userDao = D('User');
	    $info = $userDao->where("account='$account'")->find();
	    $this->assign('answer',$answer);
	    $this->assign('info',$info);

	    $this->display();
      
	}
	public function newpwd(){
	
		$map	=	array();
        $map['idcard']= $_POST['idcard'];
        if(isset($_POST['idcard'])) {
            $map['idcard']	 =	 $_POST['idcard'];
        }elseif(isset($_SESSION[C('USER_AUTH_KEY')])) {
            $map['id']		=	$_SESSION[C('USER_AUTH_KEY')];
        }
        //检查用户
        $User    =   M("User");
        if(!$User->where($map)->field('id')->find()) {
            $this->error('身份证号码不符或者用户名错误！');
        }else {
			$User->password	=	pwdHash($_POST['password']);
			$User->save();
			$this->assign("jumpUrl",__APP__);
			$this->success('密码修改成功！');
			
         }
	  } 
	
}
?>