<?php
namespace Erpadmin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class LoginController extends BaseController {
	/**
	 * [index 登陆操作]
	 * @Author   L    <L@gmail.com>
	 * @DateTime 2018-09-25
	 */
    public function index(){
        if (IS_POST) {
        	$rules = array(
			     array('username','6,20','账号格式不正确',1,'length'),
			     array('password','checkPwd','密码格式不正确',1,'function'),
			);
			$User = M("admin_user"); // 实例化User对象
			if (!$User->validate($rules)->create()){
			     return_msg(400,$User->getError());
			}
			$username = I('username');
            $password = I('password');
			/*根据账号密码查找用户信息*/
            $user_find = $User->where(array('username'=>$username,'password'=>admin_pass_salt($password),'status'=>array('gt',-1)))->field('password',true)->find();
            if (!isset($user_find) || empty($user_find)) {
                return_msg(404,'用户名或密码错误！');
            }
            if ($user_find['status'] == 0) {
                return_msg(404,'该账号已被停用！');
            }
            /*登陆成功 相关信息写入Cookie Session*/
            cookie('admin_username',$user_find['username'],3600*24*7);
            session('erp_user',$user_find);
            return_msg(0,'登陆成功，跳转中。。。',array('url'=>U('index/index')));
        } else {
        	$this->assign('cookie_username',cookie('admin_username'));
        	$this->display();
        }
    }

    /**
     * [logout 退出登陆]
     * @Author   L    <L@gmail.com>
     * @DateTime 2018-09-25
     */
    public function logout()
    {
    	// 删除（当前作用域）
		session('user',null);
		$this->redirect('Login/index');
    }


}