PackageService.php 4.8 KB

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