<?php

class userModel extends Model{
		function __construct(){
			parent:: __construct();
			$this->tabName = TAB_PREFIX."user";
			$this->fieldList=$this->get_fields($this->tabName,'list');

		}
		function login($uname, $pwd) {

			$sql = "SELECT id FROM {$this->tabName} WHERE userName = '{$uname}' AND userPwd = MD5('{$pwd}')";

			$result=$this->pdo->query($sql);
			$data =$result->fetch();
			if(!empty($data)) {	//登录成功
				$_SESSION['isLoginH'] = true;
				$_SESSION['uid']     = $data['id'];
				$_SESSION['uname']   = $uname;
				return 1;  //登录成功

			}else{
				return 0;//登录失败
			}
		}


		function validateForm($status=1){
			$result=true;
			if(!Validate::required($_POST['userName'])) {
				$this->messList[] = "用户名称不能为空.";
				$result=false;
			}
			if(!Validate::checkLength($_POST['userName'], 20)) {
				$this->messList[] = "用户名称的长度不能大于20.";
				$result=false;
			}
			if($status){
				if(!Validate::required($_POST['userPwd'])) {
					$this->messList[] = "用户密码不能为空.";
					$result=false;
				}
			}
			if($_POST['userPwd']!=$_POST['userpwdok']) {
				$this->messList[] = "两次密码输入不一致.";
				$result=false;
			}
			if(!Validate::required($_POST['email'])) {
				$this->messList[] = "用户电子邮件不能为空.";
				$result=false;
			}
			if(!Validate::match($_POST['email'], "/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/")) {
				$this->messList[] = "不是正确的电子邮件格式.";
				$result=false;
			}

			return  $result;
		}

		function existsUser($username){
			return $this->total("where userName='{$username}'");
		}


}
?>