<?php
namespace Common\Controller;
use Think\Controller;
use Think\Verify;
/**
 * Base基类控制器
 * 全局公共类申明
 * BY 江西捌零网络  Musto
 **/
class BaseController extends Controller{
	
	
    /**
     * 全局初始化方法
     */
    public function _initialize(){
	
	//如果含有推广链接，则开始获取推广码及对应的推荐身份。
	if(I('get.tg_from')!=""){	
	$tgmap['tg_code']=I('get.tg_from');
	$tguser=D('User')->where($tgmap)->find();
	//如果有推广码对应的用户，则开始COOKIE赋值。时间为三个月有效
	if($tguser){cookie('tg_from',$tguser['id'],3600*24*90);	}
	}
	
	
	
	
	//获取用户信息
	//$user数组获取用户相关信息
	$where=array('id'=>I('session.id'));
	$user = D("User")->where($where)->find();
	
	
	//获取用户关联的级别全局调用
	//$user_status 数组获取用户的级别，优惠折扣等
	$map = array('id'=>$user['status']);
	$user_status =D('Status')->where($map)->find();
	
	//推广链接
	$url_http = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
	$tg_url=$url_http.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?".$user['tg_code'];
	
	$assign=array(
		'user'=>$user,//全局初始化当前会员信息
		'inpay'=>$inpay,
		'outpay'=>$outpay,
		'user_status'=>$user_status,//用户级别数组变量
		'tg_url'=>$tg_url,
		);
	$this->assign($assign);
		
    }
	
	
	
	
	//图片上传
	public function upload() {
    	/*
    	$config = array(
    		'maxSize'=>3145728,
    		'exts'=>array('jpg', 'jpeg', 'gif', 'png'),
    		'savePath'=>'./',
    	);
    	*/
    	//实例化上传类
		$config = array(
		'savePath'  =>  './Public/',
		'maxSize' => 2000,
    	'xts' => array('jpg', 'jpeg', 'gif', 'png'),
		//'saveName' => '123',
    	//'replace' => true,
    	//'autoSub' => false,
    	//'subName' => 'abc',
    	//'subName' => array('date', 'Ymd'),
    	//'subName' => 'get_user_id',
		);
    	$upload = new Upload($config);
    	$upload->hash = false;
    	$info = $upload->upload();
    	if (!$info) {
    		$this->error($upload->getError());
    	} else {
    		//$this->success('上传成功！');
    		foreach ($info as $file) {
    			
    			//echo $file['savepath'].$file['savename'];
    			
    			echo $file['key'].'<br />';
    			echo $file['savepath'].'<br />';
    			echo $file['name'].'<br />';
    			echo $file['savename'].'<br />';
    			echo $file['size'].'<br />';
    			echo $file['type'].'<br />';
    			echo $file['ext'].'<br />';
    			echo $file['md5'].'<br />';
    			echo $file['sha1'].'<br />';
    			
    		}
    	}
    }
	
	//验证码类
	
	public function  Verify(){
	
	$Verify = new Verify();
	$Verify->fontSize = 20;
	$Verify->length   = 4;
	$Verify->useNoise = true;
	$Verify->imageH=46;
	$Verify->useCurve=false;
	$Verify->bg=array(255, 255, 255);
	$Verify->entry();
	}
	
	
	//定义左侧导航当前位置类
	//@$nav_b  父菜单
	//@$nav_m  子菜单
	public function  left_nav($nav_b,$nav_m){
		
		$nav = array(
    		$nav_b => "active",
    		$nav_m => "class='active'",
    	);
		$now_nav = $this->assign("nav",$nav);
		return $now_nav;
		
	}
	
	
	//调用阿里支付
	public function alipay(){

        $data=array(
            'out_trade_no'=>time(),//时间自动生成
            'price'=>I("post.price"),//商品价格/充值金额
            'subject'=>I("post.goods"),//商品名/充值原因
            );

        alipay($data);
    }
	
	//公共调用显示用户等级和客服人员
  	public  function  user_sk(){
  	//显示用户等级
    	$s_data = D("Status")->order("paixi desc")->select();
    	$this->assign("s_data",$s_data);
    	
    	//显示客服人员
    	$k_data = D("Service")->order("id desc")->select();
    	$this->assign("k_data",$k_data);
  	}
	
	
	/*
	*验证User表用户帐号/手机/邮箱等信息的唯一性
	*全局调用，免身份验证
	*/
	public  function user_chk(){
	
	if(I('get.email')){
			
	$chk['email']=I("get.email");
	$chk['id']=array('neq',I("get.id"));
	$chkuser = D("User")->where($chk)->find();
	if($chkuser){		
	echo "false";
	}else{
	echo "true";	
	}
	
	}
		
	if(I('get.phone')){
		
	$chk['phone']=I("get.phone");
	$chk['id']=array('neq',I("get.id"));
	$chkuser = D("User")->where($chk)->find();
	if($chkuser){		
	echo "false";
	}else{
	echo "true";	
	}
	
	}
	
	}
	
	
	/////
	///上下班，值班，休假时间判断
	
	public  function   times(){
	
	$s1_time = C("S1_TIME");//上午上班时间
	$e1_time = C("E1_TIME");//上午下班时间
	
	$s2_time = C("S2_TIME");//下午上班时间
	$e2_time = C("E2_TIME");//下午下班时间
	
	$s3_time = C("S3_TIME");//值班开始时间
	$e3_time = C("E3_TIME");//值班结束时间
	
	$s4_time = C("S4_TIME");//开始放假时间
	$e4_time = C("E4_TIME");//放假结束时间
	
	$n_time = date("H:i:s",time());//当前时间
	
	$n_date = date("Y-m-d H:i:s");//当前日期
	
	$w=date("w");
	
	//如果当前日期时间>=开始放假时间并且<=放假结束时间，则在放假期内
	if ( $n_date >= $s4_time && $n_date <= $e4_time){$time_fj="yes";}
	//上班时间
	if($n_time >= $s1_time  &&  $n_time <= $e1_time  ){$time_work="yes";}
	if($n_time >= $s2_time  &&  $n_time <= $e2_time ){$time_work="yes";}
	
	//判断是否显示非工作时间公告的条件
	if( C('TIME_SX')=="1" and ($w == "0" or  $w == "6")){$time_work="no";}
	if(C('MSG_NOWORK')==""){$time_work="yes";}
	
	//如果是值班时间是否显示值班公告条件
	if($n_time >= $s3_time && $n_time <= $e3_time && C('MSG_ZB')!=""){$time_zb="yes";}
	if(C('MSG_ZB')==""){$time_zb="no";}
	
	$assign=array(
            'time_fj'=>$time_fj,
	    'time_work'=>$time_work,
	    'time_zb'=>$time_zb,
            );
			
        $this->assign($assign);
	
	}

}
