<?php
class loginAction extends Action {
    public function index(){
		$this->display('login'); 
	}
	public function check(){
		if(IS_POST){
			$username = $this->_post("username");
			$password = $this->_post("password");
			$result = M("admin")->where("username='$username'")->find();
			$pass = md5(md5($password).md5($result['salt']));
			if($pass == $result['password']){
				cookie('username',$username,array('expire'=>C('logint'),'prefix'=>'feimao_'));
				M('admin_token')->where("username = '$username'")->delete();
				$insert_data['username'] = $username;
				$insert_data['token'] = md5(md5($username).md5($result['salt']));
				$insert_data['logintime'] = time();
				$g = M('admin_token')->add($insert_data);
				if($g) {
					echo (int)1;//登陆成功
				}else{
					echo (int)2;//登陆失败
				}
			}
		}else{
			echo false;//参数错误
		}
	}
	public function loginpass(){
		if($_POST['dosubmit']){
			$username = $this->_post('username');
			$password = $this->_post('password');
			$u_data = M('admin')->where("username = '$username'")->find();
			$pass = md5(md5($password).md5($u_data['salt']));
			if($pass != $u_data['password']) {
				$this->error('密码不正确');
			}else{
				cookie('username',$username,array('expire'=>C('logint'),'prefix'=>'feimao_'));
				M('admin_token')->where("username = '$username'")->delete();
				$insert_data['username'] = $username;
				$ip = get_client_ip();
				$insert_data['token'] = md5(md5($username).md5($ip).md5($u_data['salt']));
				$insert_data['logintime'] = time();
				$g = M('admin_token')->add($insert_data);
				if($g) {
					//$update_data['salt'] = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
					//M('admin')->where("username = '$username'")->save($update_data);
					$this->success('登录成功',U('/admin/Index'));
				}else{
					$this->error('登录失败');
				}
			}
		}
	}
	public function logout(){
		$m_model = new MemberModel();
		$m_model->logout();
	}
}