thinkphp批量生成小程序二维码并且打包下载

public function download($city){ 
        if(!empty($city)){
            $where = [
                'chengshi'  => $city,
                'mobile'    => ['<>',''],
            ];
        }else{
            $where = [
                'mobile'    => ['<>',''],
            ];
        }
        $rows = Db::table('tp_jingxiaoshangs')->where($where)->select();

        $path = ROOT_PATH . 'public' . DS . 'uploads'. DS .'qrcode'. DS.date('YmdHis').DS;
        if(!is_dir($path)){
            mkdir($path,777);
        }

        foreach ($rows as $key=>$val) {
            $types = Db::table('tp_card')->distinct('true')->field('member_type')->where('store', $val['id'])->select();
            if(!empty($types)) {
                $types = $types->toArray();
                $types = array_column($types,'member_type');
                foreach ($types as $k=>$v){
                    $this->saveQrcode($path,$val['id'],$val['gongsi'],$v);
                }
            }
        }

        $this->zipfile($path);

    }


    /**
     * 批量生成二维码
     * @param $path
     * @param $dealer_id
     * @param $gongsi
     * @param $type
     */
    function saveQrcode($path,$dealer_id,$gongsi,$type){
        $typename = $type;
        $type = findNum($type);
        $pathfFileName = $path.$gongsi.$typename.'.png';
        $pathfFileName = iconv('UTF-8', 'GBK', $pathfFileName);
        $binCode = WxHelper::getQrCode(
            'pages/offline_receive_card/receive_card/index',
            ['dealer_id' => $dealer_id, 'type'=>$type],
            1280
        );
        file_put_contents($pathfFileName, $binCode);
    }



    function zipfile($path){

        // 打包下载
        $handler = opendir($path);    //$cur_file 文件所在目录
        $download_file = array();
        $i = 0;
        while( ($filename = readdir($handler)) !== false ) {
            if($filename != '.' && $filename != '..') { 
                $download_file[$i++] = $filename;
            }
        }
        closedir($handler);

        
        $filename = 'qrcode.zip';
        $zip_file = $path . $filename; 
        $zip = new \ZipArchive;
        //打开一个zip文件;\ZipArchive::OVERWRITE覆盖,\ZipArchive::CREATE創建
        $zip->open($zip_file, \ZipArchive::OVERWRITE|\ZipArchive::CREATE);
        //把图片一张一张加进去压缩
        foreach ($download_file as $key => $value) {
            //将文件添加到压缩包;第一個參數将文件写入zip,第二個參數是文件的重命名(同时防止多级目录出现)
            $pathFilename = $path.$value;
            $zip->addFile($pathFilename,$value);
        }
        //关闭ziparchive
        $zip->close();
        //或者输出下载
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename='.basename($zip_file)); //获取文件名;basename:返回路径中的文件名部分,
        header("Content-Type: application/force-download");//强制下载
        header("Content-Transfer-Encoding: binary");//二进制传输
        header('Content-Length: '. filesize($zip_file)); //告诉浏览器,文件大小
        readfile($zip_file);
        
    }

 

posted @ 2020-12-28 10:11  Abner3721  阅读(705)  评论(0编辑  收藏  举报