InvitationService.php 4.6 KB

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