<?php
// +----------------------------------------------------------------------
// | 基础类，加密验证 Login类没有继承
// +----------------------------------------------------------------------
// | Author: 自己人
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;

class BaseController extends Controller{
	
     protected function _initialize(){
					
					$sig=I("sig");
					$sigsess=cookie('sig_auth');
					$FromSiteName=explode('.',$_SERVER[HTTP_HOST]);
					$this->FromSiteName=$FromSiteName[0]; //二级域名
					$shopType=strtoupper(substr($this->FromSiteName,0,2)); //商城类型
					$shopDealer=substr($this->FromSiteName,2,4); //代理ID

					$this->shopType=$shopType;   //继承商城类型
					
					switch ($shopType) {
						case 'IF':
							$this->shopUrl='SiteShop'; //进口食品
							break;
						case 'LV':
							$this->shopUrl='LoveShop'; //全店热恋
							break;
						case 'ST':
							$this->shopUrl='StarShop'; //明星同款
							break;
					}

					$map['DealerSiteId']  =  array('eq',$shopDealer);
					$sqlx=M('dealer_main')->where($map)->find();
					if(($FromSiteName[0]!='www')&&(!$sqlx)){
						//header('Location: http://www.suanok.com/Home/'.$this->shopUrl.'/');
					}else{
						$this->DealerInfo=$sqlx; //代理商信息
					}
	 				
		 			if((!isset($sig)||$sig=='')&&(!isset($sigsess)||$sigsess=='')){
		 			 		$tmpkey='15999770';
							$userid='sadf32432sd';
							$keytime=time().'000';
							$sig=md5($tmpkey.$userid.$keytime).'.'.$userid.'.'.$keytime; //临时sig
							//echo $sig;
		 			}
	 			 
					if(isset($sig)&&$sig!=''){
							$checktag=$this->checksig($sig,0);
							if(!$checktag){
								$this->redirect('Login/exitsystem');
								exit();
							}
					}elseif(!$sigsess||$sigsess==''){
								$this->redirect('Login/exitsystem');
								exit();
					}else{
							$checktag=$this->checksig($sigsess,1);
							if(!$checktag){
							 	 	 	$this->redirect('Login/exitsystem');
								 	 	exit();
							}
					}
					if($sig!=''){
							$tmparr=explode(".",$sig);
							$selfuid=$tmparr[1];
					}elseif($sigsess!=''){
							$tmparr=explode(".",$sigsess);
							$selfuid=$tmparr[1];
					}
					if($selfuid!=''){
							$this->selfuid=$selfuid;
							$ActName=ACTION_NAME; //当前操作类，用来确认底部菜单的选中状态
							$this->assign(compact('selfuid','ActName','shopUrl'));
							//写下日志记录
							$tmptmk=date('Y-m-d H:i:s');
							$ip = get_client_ip();
							$url =$_SERVER['HTTP_HOST'].__SELF__;
							$agent=$_SERVER['HTTP_USER_AGENT'];
							$this->genrequstlog($selfuid,$tmptmk,$ip,$agent,$url);
					}else{
							$this->redirect('Login/exitsystem');
							exit();
					}
     }

    //签名验证
	public function checksig($tmpsig,$sessionFlag){
				 if($tmpsig!=''){
					$tmparr=explode(".",$tmpsig);
					switch($tmparr[1]){
						case 'virgindevice':
							$tmpkey='87654321';
							break;
						case 'login':
							$tmpkey='12345678';			
							break;
						case 'sadf32432sd': //浏览器标示
							$tmpkey='15999770';			
							break;
						default:
							$sqlx=M('coin_balance','zjr_','DB_ZJR_BANK')->where("OkFlag='0' AND UserId='".$tmparr[1]."'")->find();
							$tmpkey=$sqlx["SecKey"];
							break;
					}
					if($tmparr[2]<(date('U')-60*5)*1000 && $sessionFlag==0){
						return false;
					}
					if($tmparr[0]==md5($tmpkey.$tmparr[1].$tmparr[2])){
						$keytime=time().'000';
						$tmp=md5($tmpkey.$tmparr[1].$keytime).'.'.$tmparr[1].'.'.$keytime;
						session('sig_auth',$tmp);
						return true;
					}
				}
				return false;
	}
	
	//写入APP使用LOG
	public function genrequstlog($tmpuid,$tmptmk,$ip,$agent,$FromSiteName){
		$PFX='./Data/LOG/'.date("Ymd").".log";
		$tmpfx=fopen($PFX,"a");
		$tmps=$tmpuid.'@'.$tmptmk.'@'.$ip.'@'.$FromSiteName.'@'.$agent."\r\n";
		fputs($tmpfx,$tmps);
		fclose($tmpfx);
	}

}