<?php

	/***************************************************************\
	|																|
	| 开单大师（专业的房产ERP管理系统）								|
	|																|
	| © 2018 http://www.kaidandashi.com All rights reserved.		|
	|																|
	| 本系统由淮南市银泰科技软件有限公司提供技术支持				|
	|																|
	| QQ号：1264399051												|
	|																|
	\***************************************************************/

	namespace Admin\Controller;
	use Think\Controller;
	class LoginController extends Controller {
		public function index(){
			$this->jcpz=M('jcpz')->where('gongsiid=10000')->find();
			$this->nwlogo2 = M('nwlogo')->order('rand()')->limit(1)->getField('image');
			$this->display(); 
		}
		public function VerifyImg(){
			$verify = new \Think\Verify();
			//$verify->useZh = true;  //使用中文验证码
			$verify->fontSize = 35; //验证码字体大小
			$verify->useNoise = false;  //关闭验证码干扰项
			$verify->length = 2; 
			$verify->imageW = 220;
			$verify->imageH= 70;
			ob_end_clean();
			$verify->entry();
		}
		public function Login(){
			if (!IS_POST)$this->error('页面不存在');
			//验证码判断
			$verify = new \Think\Verify();
			$code= trim(I('yzm'));
			if($verify->check($code) === false){
				$this->error('验证码错误');
			}
			$db=M('yonghu');
			if(I('username') != C('RBAC_SUPERADMIN')){
				$gongsi=$db->where(array('gongsiid' =>I('gongsiid')))->find();
				if (!$gongsi) {
					$this->error('公司账号错误');
				}
			}
			$user=$db->where(array('dianhua' =>I('username'),'gongsiid'=>I('gongsiid')))->find();
			if (!$user || $user['mima'] != I('password','','md5')) {
				$this->error('账号或密码错误');
			}
			if ($user['lock1']==1) {
				$this->error('账号已被停用');
			}
			if ($user['status']==0) {
				$this->error('账号或密码错误');
			}
			$ipdz=get_client_ip();
			$wldz = findCityByIp($ipdz);
			if ($wldz) {
				$zsdz=$wldz;
			} else {
				$zsdz='未知';
			}
			$data = array(
				'id'=>$user['id'],
				'logintime'=>time(),
				'loginip'=>$ipdz
			);
			$db->save($data);
			$data1 = array(
				'shijian'=>time(),
				'ipdizhi'=>$ipdz,
				'zdbianhao'=>$zsdz,
				'yonghu'=>$user['ygmingcheng'],
				'uid'=>$user['id'],
				'bumen'=>$user['bumen'],
			);
			M('dlrizhi')->add($data1);
			$role_id=M('role_user')->where(array('user_id' =>$user['id']))->getField('role_id');
			session('uid',$user['id']);
			session('role',$role_id);
			session('bumen',$user['bumen']);
			session('ygmingcheng',$user['ygmingcheng']);
			session('gongsiid',$user['gongsiid']);
			session('username',$user['dianhua']);
			session('logintime',date('Y-m-d H:i:s',$user['logintime']));
			session('loginip',$user['loginip']);
			if($user['dianhua'] == C('RBAC_SUPERADMIN')){
				session(C('ADMIN_AUTH_KEY'),true);
			}
			\Org\Util\Rbac::saveAccessList();
			$this->success('登录成功','Index/index');
		}
		public function Logout(){
			session_unset();
			session_destroy();
			$this->redirect('Login/index');
		}
	}