quanwei
2025-11-28 3ea53e61cc23fdb3ddf8b38a199ca60a6da8c407
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
 
namespace app\common\service\qrcode;
 
use app\common\model\supplier\Supplier as SupplierModel;
/**
 * 二维码
 */
class SupplierService extends Base
{
    private $id;
    private $source;
 
    /**
     * 构造方法
     */
    public function __construct($id, $source)
    {
        parent::__construct();
        $this->id = $id;
        $this->source = $source;
    }
 
    /**
     * 获取小程序码
     */
    public function getImage()
    {
        $detail = SupplierModel::detail($this->id);
        // 保存目录
        $savePath = $this->getPosterPath($detail['app_id']);
        // 删除目录下的文件
        if(!$this->is_empty_dir($savePath)) {
            $this->deleteDir(substr($savePath, 0, -1));
        }
 
        mkdir($savePath, 0755, true);
        if($this->source == 'wx'){
            // 下载小程序码
            $this->saveSupplierQrcodeToDir($detail['app_id'], 'pages/index/index', $savePath, $this->id);
        }else if($this->source == 'mp' || $this->source == 'h5'){
            $this->saveSupplierMpQrcodeToDir('h5/pages/index/index',$savePath, $this->id, $detail['app_id']);
        }else if($this->source == 'group'){
            $this->saveSupplierQrcodeToDir($detail['app_id'], 'pages/shop/qywx_qrcode', $savePath, $this->id);
        }
 
        $zipNameUrl = $this->getZipPath($detail['app_id']);
 
        $zip = new \ZipArchive();
        if($zip->open($zipNameUrl, \ZipArchive::OVERWRITE) !== TRUE){
            //OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
            if ($zip->open($zipNameUrl, \ZipArchive::CREATE) !== true) {
                // 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
                return  '下载失败,文件夹不存在';
            }
        }
 
        $this->addFileToZip($savePath, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
 
        $zip->close(); //关闭处理的zip文件
 
        $zipName = $this->source . '.zip';
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename='.$zipName);
        readfile($zipNameUrl);
        header('Content-Length: ' .filesize($zipNameUrl));
    }
 
 
    /**
     * 二维码文件路径
     */
    private function getPosterPath($app_id)
    {
        // 保存路径
        $tempPath = root_path('public') . 'temp' . '/' . $app_id . '/supplier-' . $this->id . '/' . $this->source. '/';
        return $tempPath;
    }
 
    /**
     * 二维码文件路径
     */
    private function getZipPath($app_id)
    {
        // 保存路径
        $tempPath = root_path('public') . 'temp' . '/' . $app_id . '/supplier-' . $this->id . '/' . $this->source. '.zip';
        return $tempPath;
    }
 
    /**
     * 删除当前目录及其目录下的所有目录和文件
     * @param string $path 待删除的目录
     * @note  $path路径结尾不要有斜杠/(例如:正确[$path='./static/image'],错误[$path='./static/image/'])
     */
    private function deleteDir($path) {
        if (is_dir($path)) {
            //扫描一个目录内的所有目录和文件并返回数组
            $dirs = scandir($path);
            foreach ($dirs as $dir) {
                //排除目录中的当前目录(.)和上一级目录(..)
                if ($dir != '.' && $dir != '..') {
                    //如果是目录则递归子目录,继续操作
                    $sonDir = $path.'/'.$dir;
                    if (is_dir($sonDir)) {
                        //递归删除
                        $this->deleteDir($sonDir);
                        //目录内的子目录和文件删除后删除空目录
                        @rmdir($sonDir);
                    } else {
                        //如果是文件直接删除
                        @unlink($sonDir);
                    }
                }
            }
            @rmdir($path);
        }
    }
 
    /**
     * 打包文件夹
     */
    private function addFileToZip($path, $zip){
        $handler = opendir($path);
        while(($filename=readdir($handler))!==false){
            if($filename != "." && $filename != ".."){
                if(is_dir($path."/".$filename)){
                    $this->addFileToZip($path."/".$filename, $zip);
                }else{ //将文件加入zip对象
                    $zip->addFile($path."/".$filename);
                    $zip->renameName($path."/".$filename, $filename);
                }
            }
        }
        @closedir($path);
    }
 
    private function is_empty_dir($fp)
    {
        if(!file_exists($fp)){
            return false;
        }
        $H = @ opendir($fp);
        $i=0;
        while($_file=readdir($H)){
            $i++;
        }
        closedir($H);
        if($i>2){
            return false;
        }else{
            return true;
        }
    }
}