<?php /** * Created by PhpStorm. */ namespace Admin\Model; use Think\Model; class UserModel extends Model { //自動驗證
protected $_validate=array( //array(驗證字段,驗證規則,錯誤提示,驗證條件,附加規則,驗證時間) 或者單獨留給注冊用,登陸單獨寫規則
array('user_name','','帳號名稱已經存在!',0,'unique',1),
array('user_name','require','用戶名不能為空',self::EXISTS_VALIDATE),
array('user_name', '5,20', '用戶名長度5-20位!', self::EXISTS_VALIDATE,'length'),
array('password','require','密碼不能為空',self::EXISTS_VALIDATE),
array('code','require','驗證碼不能為空',self::EXISTS_VALIDATE),
array('password', '5,30', '密碼長度不合法!', self::EXISTS_VALIDATE,'length'),
array('repassword', 'password', '倆次輸入密碼不一致!', self::EXISTS_VALIDATE,'confirm'),
array('code', '4', '請輸入4位驗證碼', self::EXISTS_VALIDATE,'length'), ); //使用create方法創建數據對象的時候,執行Add()方法時,不在$insertFields定義范圍內的字段將直接丟棄即只允許新增定義內的字段
protected $insertFields = array('id','user_name','password','user_byname','reg_time','reg_ip','login_time','login_count','role_id','status','unqi_id'); //使用create方法更新數據對象的時候,執行sava()方法時,不在$updateFields定義范圍內的字段將直接丟棄
protected $updateFields = array('id','user_name','password','user_byname','reg_time','reg_ip','login_time','login_count','role_id','status','unqi_id'); //自動完成
protected $_auto=array( array('password', 'sha1', self::MODEL_INSERT, 'function'),
array('login_time', 'time', self::MODEL_INSERT, 'function'),
array('reg_time','time',self::MODEL_INSERT,'function'),
array('reg_ip','get_client_ip',self::MODEL_INSERT,'function'),
array('login_ip','get_client_ip',self::MODEL_BOTH,'function'),
array('login_time','time',self::MODEL_BOTH,'function'),
array('login_count',0,self::MODEL_INSERT),
array('status',1,self::MODEL_INSERT),
array('password','',self::MODEL_UPDATE,'ignore'),//修改時空白則忽略
); //檢帳帳戶是否存在或者停用狀態
public function checkStatus($username){ $map=array(); $map['user_name']=$username; $map['status']=array('gt',0); return $authInfo=$this->where($map)->find(); } //驗證登陸
public function checkLogin($username,$password,$code){ $data=array( 'user_name'=>$username,
'password'=>$password,
'code'=>$code, ); if(!$this->create($data)) return $this->getError(); //檢測用戶名是否存在或狀態
if($authInfo=$this->checkStatus($username)){ return $authInfo; } } //登陸后更新數據庫登陸時間和次數級ip
public function update($user_id,$login_count){ $data = array( 'login_time'=>time(),
'login_ip'=>get_client_ip(),
'login_count'=>$login_count+1 ); return $this-> where('id='.$user_id )->setField($data); } //注冊管理員
public function register($username,$re_name,$byname,$password){ $data=array( 're_name'=>$re_name,
'user_name'=>$username,
'user_byname'=>$byname,
'password'=>$password, ); if(!$this->create($data)){ return $this->getError(); }else{ P($data); echo $this->add(); } } }