public function index()
    {
        require_once('Public/phpqrcode.php');

        $this->assign('TITLE','应用下载 - '.$this->WEB_NAME);   
        $id = I('id',0,'intval');
        $mod = M('apps');
        $row = $mod->where('id='.$id)->find();
        if(!$row)
        {
            $this->error('');
        }

        $list = $mod->where('parentid='.$row['parentid'])->order('updatetime desc')->select();

        $this->assign('list',$list);

        // 二维码数据
        $url = $this->WEB_URL.'/'.$row['filepath'].$row['filename'];
        $platTitle = 'ANDROID';
        //IOS APP
        if(isIOSApp($row['platform']))
        {
            $url = 'https://'.$url;
            //itms-services://?action=download-manifest&url=https://www.xxx.com/app/xxx.plist
            $url = str_replace('.ipa','.plist',$url);
            $ecodeurl = urlencode($url);
            $url = 'itms-services://?action=download-manifest&url='.$ecodeurl;
            $platTitle = 'IOS';
        }
        else
        {
            $url = DEFAULT_PROTOCOL.$url;
        }
        // 生成的文件名 
        $filename = $row['filepath'].'qrcode_'.$row['icon']; 
        // 纠错级别:L、M、Q、H 
        $errorCorrectionLevel = 'L';  
        // 点的大小:1到10 
        $matrixPointSize = 5;  
        //创建一个二维码文件 
        \QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 10);
        //输入二维码到浏览器
        //$qrcode = \QRcode::png($url,false,QR_ECLEVEL_L,10);   

        $this->assign('url',$url);

        $this->assign('qrcode',$filename);

        $this->assign('vo',$row);
        $this->assign('platitle',$platTitle);
        
 
        $this->display();
    }    
    public function addsave()
    {
        $path = I('post.path','','trim,strip_tags,addslashes');
        $isplugin = I('post.isplugin',0,'intval');
        $appid = I('post.appid',0,'intval');
        $pluginid = I('post.pluginid',0,'intval');

        $name = I('post.name','','trim,strip_tags,addslashes');
        $package = I('post.package','','trim,strip_tags,addslashes');
        $versioncode = I('post.versioncode',0,'intval');
        $versionname = I('post.versionname','','trim,strip_tags,addslashes');
        $desc = I('post.desc','','trim');
        $desc2 = I('post.desc2','','trim');
        $rootctrlname = I('post.rootctrlname','','trim,strip_tags,addslashes');
        $alarmtype = I('post.alarmtype','','trim,strip_tags,addslashes');

        if($alarmtype && is_array($alarmtype))
        {
            $alarmtype = implode(',', $alarmtype);
        }

        $platform = getPlatformType($path);

        $is_ios_plugin = $this->isIosPlugin($isplugin,$platform);

        if(!$name || strlen($name)>64 || (!$is_ios_plugin && !$pluginid && !$package) || (!$versioncode) || !$versionname || !$desc || ($is_ios_plugin && !$rootctrlname))
        {
            $this->error('信息填写有误,请检查');
        }

   
        if(($isplugin && !$appid))
        {
            $this->error('必要参数丢失,请重新上传');
        }

        if(!isset($_FILES['icon']) || $_FILES['icon']['size']<=0)
        {
            $this->error('请选择要上传的ICON图片');
        }

        $size = 300 * 1024;
        if($_FILES['icon']['size'] > $size)
        {
            $this->error('上传的ICON图片太大,不能超过300K');
        }
 
        $iconext = getFileExt($_FILES['icon']['name']);

        $iconname = $platform .'_'. time().'_'.mt_rand().'.'.$iconext;

        //$iconname = $pathname .'.'.$iconext;

        if($iconext!='png' && $iconext!='jpg'&& $iconext!='gif'&& $iconext!='jpeg')
        {
            $this->error('上传的ICON图片类型不对,当前只支持png/jpg/jpeg/gif');
        }

        $this->checkPackageExists($path);

        $folder = $this->upload.'Apps/';

        if(!is_readable($folder))
        {
            @$result = mkdir($folder);
            if(!$result)
            {
                $this->error('创建上传Apps目录失败,请确认Uploads目录有可写权限');
            }
        }
        
        $folder = $folder . $this->gfolder . '/';
        if(!is_readable($folder))
        {
            @$result = mkdir($folder);
            if(!$result)
            {
                $this->error('创建上传目录'.$this->gfolder.'失败,请确认Uploads目录有可写权限');
            }
        }
        $data = array(
            'name'=>$name,
            'platform'=>$platform,
            'parentid'=>$appid,
            'versioncode'=>$versioncode,
            'versionname'=>$versionname,
            'packagename'=>$package,
            'icon'=>$iconname,
            'desc'=>$desc,
            'gid'=>$this->gid,
            'uid'=>$this->loginid,
            'updatetime'=>time(),
            'filesize'=>filesize($this->upload.$path),
            'filename'=>$path,
            'downnum'=>0,
            'filepath'=>'',
            'filemd5'=>md5_file($this->upload.$path)
        );
        $mod = null;
        if($isplugin == 1)
        {
            $mod = M('plugins');
            unset($data['uid'],$data['gid']);
            $data['appid'] = $appid;
            $data['parentid'] = $pluginid;
            $data['rootctrlname'] = $rootctrlname;
            $data['alarmtype'] = $alarmtype;

            //检测android插件包名  更新,防止修改
            if(!$is_ios_plugin)
            {
                if ($pluginid > 0)
                {
                    $row = $mod->getById($pluginid);
                    $data['packagename'] = $row['packagename'];
                }
                else
                {
                    $where = array('packagename' => $data['packagename'],'parentid'=>$pluginid);
                    $row = $mod->where($where)->find();
                    if ($row)
                    {
                        $this->error('插件包名已经存在');
                    }
                }
            }

        }
        else
        {
            $mod = M('apps');
            if(!$appid)
            {
                $where = array('packagename'=>$data['packagename']);
                $row = $mod->where($where)->find();
                if($row)
                {
                    $this->error('APP包名已经存在');
                }
            }
        }

        $incid = $mod->data($data)->add();
        if(!$incid)
        {

            $this->error('添加数据失败');
        }
        // 更新app或插件原来的描述
        if($desc2)
        {
            $updatedata = array('desc'=>$desc2);
            if($isplugin)
            {
                if($pluginid>0)
                {
                    $where = array('id'=>$pluginid);
                    $mod->data($updatedata)->where($where)->save();
                }
            }
            else
            {
                if($appid>0)
                {
                    $where = array('id'=>$appid);
                    $mod->data($updatedata)->where($where)->save();
                }
            }
        }
        //新APP
        if($appid == 0)
        {
            //创建app主目录
            $folder = $folder .$incid . '/';
            @mkdir($folder);                
        }        
        else
        {
            $folder = $folder . $appid . '/';
            @mkdir($folder);  
            //上传插件
            if($isplugin)
            {
                $folder = $folder . 'plugins/';
                @mkdir($folder);  
                            
                if($pluginid == 0)
                {
                    $folder = $folder . $incid . '/';
                    @mkdir($folder); 
                }
                else
                {
                    $folder = $folder . $pluginid . '/';
                    @mkdir($folder); 

                    $folder = $folder . 'updates/';
                    @mkdir($folder);

                    $folder = $folder . $incid . '/';
                    @mkdir($folder);
                }
            }
            else
            {
                $folder = $folder . 'updates' . '/';
                @mkdir($folder);

                $folder = $folder . $incid . '/';
                @mkdir($folder);
            }
        }
        $updatedata = array('filepath'=>$folder);
        //方便分组显示,更新parentid
        if($data['parentid']==0)
        {
            $updatedata['parentid'] = $incid;
        }
        $where = array('id'=>$incid);
        $mod->data($updatedata)->where($where)->save();

        @copy($this->upload.$path,$folder.$path);
        @unlink($this->upload.$path);

        @move_uploaded_file($_FILES['icon']['tmp_name'], $folder.$iconname);

        //IOS APP自动生成plist文件,用下ios下安装
        if(!$isplugin && isIOSApp($platform))
        {
            $plistpath = dirname(dirname(dirname(dirname(__FILE__)))).'/Public/appleplist.plist';
            $body = file_get_contents($plistpath);

            $plistname = str_replace('.ipa','.plist',$path);
            $url = DEFAULT_PROTOCOL.WEB_URL.'/'.$folder.$path;
            $body = str_replace('{URL}',$url, $body);

            $logo = DEFAULT_PROTOCOL.WEB_URL . '/' . $folder.$iconname;
            $body = str_replace('{LOGO}', $logo, $body);

            $body = str_replace('{PACKAGE}', $package, $body);
            $body = str_replace('{VERSION_CODE}', $versioncode, $body);
            $body = str_replace('{TITLE}', $name, $body);

            $save = $folder . $plistname;

            file_put_contents($save, $body);
        }

        $this->redirect('Index/index');       
    }