<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Login extends Controller {

/* 登录页面 */
	public function index($username = "", $password = "", $verify = "",$type=1){
		 
		if($_POST){ //登录验证
            $username =safe_replace($username);//过滤
			$map = array();
		switch ($type) {
			case 1:
				$map['username'] = $username;
				break;
			case 2:
				$map['email'] = $username;
				break;
			case 3:
				$map['mobile'] = $username;
				break;
			case 4:
				$map['id'] = $username;
				break;
			default:
				return 0; //参数错误
		}
	  if(!$username){
		  $this->error("用户名不能为空！");
	  }
	  if(!$password){
		  $this->error("密码不能为空！");
	  }
       $map["password"]=encode($password);
	  
		/* 获取用户数据 */
		$user =db('user')->where($map)->find();
		if($user){
			  
			
				$User = new \app\admin\model\User;
				if($User->login($user)){ //登录用户
					//TODO:跳转到登录前页面            
					 $url= url('index/index');
				     $data['url']=$url;
                     $data['code']=1;
				    return $data;
			       
				
				} else {
					$this->error("密码或账号错误");
				}
		}
 else {
					$this->error("密码或账号错误");
				}
		

		} else {  	
		      $meta_title = '会员登录';	
			 
			 //显示登录表单
			  return $this->fetch();
		}
	}
    /* 退出登录 */
    public function logout(){
        if(is_login()){
           session("user_auth", null);
           session("user_auth_sign", null);
		   cookie('password',null);
		   cookie('username',null);
            session('[destroy]');
            $this->success('退出成功！', U('index'));
        } else {
            $this->redirect('login/index');
        }
    }

    public function verify(){
        $verify = new \think\Verify();
        $verify->entry(1);
    }

}
