<?php

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class AppController extends ThinkController {
	const model_name = 'App';

    public function lists(){
				
				$this->m_title = 'APP助手上传';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'App/lists','status'=>1])->find());
				
    	parent::lists(self::model_name,$_GET["p"]);
    }

    public function add($value='')
    {
    	if(IS_POST){
            if(empty($_POST['file_name'])){
                $this->error('未上传游戏原包');
            }
            $d = D('App')->find();
            $source = A('App','Event');
            if(empty($d)){
                $source->add_source();
            }
            else{
            $this->error('游戏已存在原包',U('App/lists'));
            }
    	}
    	else{
            $this->meta_title = '新增游戏原包';
						
						$this->m_title = 'APP助手上传';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'App/lists','status'=>1])->find());
				
						
    		$this->display();
    	}
    	
    }
    
    public function del($model = null, $ids=null){
        if ( empty($ids) ) {
            $this->error("请选择数据");
        }
        $souce=M("App","tab_");
        $map['id']=array("in",$ids);
        $list=$souce->where($map)->select();
        foreach ($list as $key => $value) {
            @unlink($value['file_url']);
        }
        if($souce->where($map)->delete()){
            $this->success("删除成功!");
        } else {
            $this->error('删除失败!');
        }
    }

    public function edit($id){
         $map['id']=$id;
        if(IS_POST){
            if(empty($_POST['file_name'])){
                $this->error('未上传游戏原包');
            }
						
						if($_POST['id'] == 4 || $_POST['id'] == 6) {
							
							if(empty($_POST['bao_name'])) {
								$this->error('包名不能为空');
							}
							
						}
						
            $d = D('App')->where($map)->find();
            $source = A('App','Event');
            if(empty($d)){
                $source->add_source();
            }
            else{
                $source->update_source($d['id'],$d['file_name']);
            }
        }
        else{
            $d = M('App',"tab_")->where($map)->find();
            $this->meta_title = '更新APP注释';
						
						$remark = implode('@@@',json_decode($d['remark']));
						
						$d['remark'] = str_replace("@@@","\r\n",$remark);
						
						
            $this->assign("data",$d);
						
						$this->m_title = 'APP助手上传';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'App/lists','status'=>1])->find());
				
						
            $this->display();
        }
        
    }
		
		
		public function app_package($id=0) {
			
			if (!is_numeric($id) || $id<1) {$this->error('参数错误');}
			
			$data = D('AppApply')->field('id')->where(['app_id'=>$id])->select();
			
			$ids = array_column($data,'id');
			
			R('Apply/app_package',array('ids'=>$ids));
			
		}											
		
		
		
		
		
		
		public function get_app_pack($source_url='',$sdk_version=1){

        //获取ios版本
        if($source_url==''||!file_exists($source_url)){
            $this->ajaxReturn('app上传地址无效,请刷新页面重新上传');
        }
        if($sdk_version==1){
            $newname = "and_app_package_for_version.apk";
            $versionpath = '';
            $newvpath = '';
        }else{
            $newname = "ios_app_package_for_version.ipa";
            $versionpath = 'Payload/XiGuMobileGame.app/Info.Plist';
            $newvpath = 'ios_version';
        }
        $to = "./Uploads/App/" . $newname;
        $cp = copy($source_url, $to);
        if($cp==false){
            $newname = $source_url;
        }else{
            @unlink($source_url);
        }
        $jsonreturn['status'] = 0;
        $jsonreturn['data'] = '';
        if(!$cp){
            $jsonreturn['info'] = 'app复制失败,请刷新页面重新上传';
            $this->ajaxReturn($jsonreturn);
        }
        //安卓不自动获取版本号
        if($sdk_version==1){
            $jsonreturn['status'] = 1;
            $jsonreturn['info'] = '获取成功';
            $jsonreturn['newpath'] = $newname;
            $this->ajaxReturn($jsonreturn);
        }
        $zip = new \ZipArchive;
        $open = $zip->open($to, \ZipArchive::CREATE);
        if(!$open){
            $jsonreturn['info'] = 'app打开失败,请刷新页面重新上传';
            $this->ajaxReturn($jsonreturn);
        }else{
            for($i = 0; $i < $zip->numFiles; $i++) {
                $path = $zip->getNameIndex($i);
                if($path==$versionpath){
                    $jieya = $zip->extractTo('Uploads/App/'.$newvpath, array($zip->getNameIndex($i)));
                    $zip->close();
                    $content = file_get_contents('Uploads/App/'.$newvpath.'/'.$path);
                    if(empty($content)){
                        $jsonreturn['info'] = 'app获取信息失败,请确认app正确,并刷新重新上传';
                        $this->ajaxReturn($jsonreturn);
                    }else{
                        $xmla = "/<!-[\s\S]*?-->/si";//去除xml注释
                        $xml= preg_replace($xmla ,'', $content);
                        $xxml = preg_replace("/\s+/", " ", $xml);
                        $BundleVersion = '/CFBundleVersion<\/key> <string>(.*?)<\/string>/si';//BundleVersion
                        preg_match_all($BundleVersion , $xxml , $user_permission);
                        if(!$user_permission[1][0]){
                            $jsonreturn['info'] = 'app版本获取失败,请确认app正确,并刷新重新上传';
                            $this->ajaxReturn($jsonreturn);
                        }else{
                            $jsonreturn['status'] = 1;
                            $jsonreturn['info'] = '获取成功';
                            $jsonreturn['data'] = $user_permission[1][0];
                            $jsonreturn['newpath'] = $newname;
                            $this->ajaxReturn($jsonreturn);
                        }
                    }
                }
            }
            $jsonreturn['info'] = 'app获取信息失败,请确认app正确,并刷新重新上传';
            $this->ajaxReturn($jsonreturn);
        }
    }

    public  function hex_to_str($hex_str='') {
        if(empty($hex_str)) {return '';}
        $str = '';
        for($i=0;$i<strlen($hex_str)-1;$i+=2) {
            $str.=chr(hexdec($hex_str[$i].$hex_str[$i+1]));
        }
        return $str;
    }
		

		
		}