<?php
namespace app\admin\controller;

use app\admin\common\Base;
use think\Request;
// use app\admin\model\Admin;
use think\Session;
use think\Db;
use think\Controller;

class Login extends Controller{
    /**
     * 渲染登陆界面
     *
     * @return \think\Response
     */
    public function index()
    {
        if(!session('admin_user_id')){//没有登录信息
    		return $this->view->fetch('login');
    	}else{
    		//登录信息超时
    		$log_time=session('admin_user_login_time')-time();
    		if($log_time < 0){
    			return $this->view->fetch('login');
    		}
    	}
        return $this->view->fetch('nav');
    }
    /**
     * 验证用户身份.
     *
     * @return \think\Response
     */
    public function login(Request $request)
    {
    	//判断是否是post方法发送的数据：如果是则开始登陆
    	if(Request::instance()->isPost()){
    		$user_name=Request::instance()->param("username");
    		$password=Request::instance()->param('password');
    		if(empty($user_name) || empty($password)){
    			$result=['status'=>0,'msg'=>'用户名或密码为空'];
    			return json($result);
    		}
    		//数据查询
    		$infor=Db::name("user")
    			->field('id,user_name,user_head')
    			->where('phone',$user_name)
    			->where('password',md5($password))
    			->where("status",1)
    			->where('type',1)
    			->find();
    		if(empty($infor)){
    			$result=['status'=>0,'msg'=>'用户不存在或密码不正确'];
    		}else{
    			//Session::set('admin_user_type',$infor['role_id']);
    			Session::set('admin_user_id',$infor['id']);
    			//Session::set('admin_mechanism_id',$infor['mechanism_id']);
    			$time_out=time()+3600;
    			Session::set('admin_user_login_time',$time_out);
    			$result=['status'=>1,'msg'=>'登录成功','token'=>1];
    		}
    		return json($result);
    	}
    	//如果不是post，则返回登陆界面
    	else{
    		$result=['status'=>0,'msg'=>'未获取到参数'];
    		return json($result);
    	}
    }
    /**
     * 退出登录
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function logout(Request $request)
    {
    	Session::clear();
    	return json(['status'=>1,'msg'=>'退出成功']);
    }
    /**
     * 超时登录推出跳转页面
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function logoutJump(Request $request)
    {
    	return $this->view->fetch('logoutJump');
    }
}
