<?php
namespace Site\Controller;

use Think\Controller;

class BaseController extends SiteController{

    protected function _initialize() {
			
      parent::_initialize();
			
			
			
			if ($_REQUEST['account'] && strtolower($_REQUEST['account']) != 'index') {
				
				$promote = D('promote')->detail($_REQUEST['account']);
				
				define('PID', $promote['id']);
				
        define('PROMOTE_ACCOUNT',$promote['account']);
				
				$sitebase = D('SiteBase')->detail(PID);
				
				$this->assign('sitebase',$sitebase);	
				
			} else {
				
		        $url = 'http://'.$_SERVER['HTTP_HOST'];
				$data = M('site_apply','tab_')->where(array('site_url'=>$url,'status'=>1))->find();
				if($data){
					$promote = D('promote')->detail($data['promote_id'],false);
					define('PID', $promote['id']);
	                define('PROMOTE_ACCOUNT',$promote['account']);
					$sitebase = D('SiteBase')->detail(PID);
					$this->assign('sitebase',$sitebase);
				}else{
					define('PID', '');
	                define('PROMOTE_ACCOUNT','');
					$this->assign('sitebase','');	
				}
				
			}
			
    }
    
		
		public function qrcode($url,$level=3,$size=4){
        Vendor('phpqrcode.phpqrcode');
        $errorCorrectionLevel =intval($level) ;//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        //生成二维码图片
        $url = base64_decode(base64_decode($url));
        $object = new \QRcode();
        ob_clean();
        echo $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }
    
		
		public function page($data) {

			$count = $data['count'];
			
			$row = $data['row'];
			
			if($count > $row){
				$page = new \Think\Page($count, $row);
				$page->setConfig('prev','<');
				$page->setConfig('next','>');
				$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
				$this->assign('_page', $page->show());
			}
			
			$this->assign('lists',$data['data']);
			
			$this->assign('count',$count);
			
		}
		
		
		public function down_game($name='',$source='',$pid) {
			
			$type = get_device_type();
			
			$sdk_version='';
			
			if ($type == 'ios') {
				
				$sdk_version = 2;
				
			} elseif ($type == 'android') {
				
				$sdk_version = 1;
				
			}
			
			$game = D('SiteGame')->down($name,$source,$sdk_version,$pid);
			
			if ($source == 2) {
				
				D('SiteGame')->where(['game_name'=>$name,'game_source'=>2,'promote_id'=>$pid])->setInc('download_number',1,60);
				
				header('Location:'.$game['game_dow_url']);
				
			} else {
			
				A('Down')->down_file($game['game_id'],PID,$sdk_version);
				
			}
			
		}


    
    
}