Watermark.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\controller\content\certificate;
  10. use app\store\controller\Controller;
  11. use app\store\model\CertificateCategory as CertificateCategoryModel;
  12. use app\store\model\Certificate as CertificateModel;
  13. use app\store\model\CertificateApply as CertificateApplyModel;
  14. use ZipArchive;
  15. /**
  16. * 文章分类
  17. * Class Category
  18. *
  19. * @package app\store\controller\content
  20. */
  21. class Watermark extends Controller
  22. {
  23. /**
  24. * 文章分类列表
  25. *
  26. * @return mixed
  27. */
  28. public function index()
  29. {
  30. $model = new CertificateApplyModel;
  31. $list = $model->getList();
  32. return $this->fetch('index', compact('list'));
  33. }
  34. /**
  35. * 添加文章分类
  36. *
  37. * @return array|mixed
  38. */
  39. public function add()
  40. {
  41. $model = new CertificateApplyModel;
  42. if (!$this->request->isAjax()) {
  43. $category = CertificateCategoryModel::getAll();
  44. return $this->fetch('add', compact('category'));
  45. }
  46. // 新增记录
  47. if ($model->add($this->postData())) {
  48. return $this->renderSuccess('添加成功', url('content.certificate.watermark/index'));
  49. }
  50. return $this->renderError($model->getError() ?: '添加失败');
  51. }
  52. /**
  53. * 编辑文章分类
  54. *
  55. * @param $id
  56. * @return array|mixed
  57. * @throws \think\exception\DbException
  58. */
  59. public function edit($id)
  60. {
  61. // 分类详情
  62. $model = CertificateApplyModel::detail($id);
  63. if (!$this->request->isAjax()) {
  64. $category = CertificateCategoryModel::getAll();
  65. $certificateList = CertificateModel::getCertificateList(['certificate_category_id' => ['in', $model['category_ids']]], ['id', 'name']);
  66. return $this->fetch('edit', compact('model', 'category', 'certificateList'));
  67. }
  68. // 更新记录
  69. if ($model->edit($this->postData())) {
  70. return $this->renderSuccess('审核成功', url('content.certificate.watermark/index'));
  71. }
  72. return $this->renderError($model->getError() ?: '审核失败');
  73. }
  74. /**
  75. * [delete]
  76. *
  77. * @Author: Yeshihao
  78. * @DateTime: 2024/11/28 10:09
  79. * @param $id
  80. * @return array|bool
  81. */
  82. public function delete($id)
  83. {
  84. $model = CertificateApplyModel::detail($id);
  85. if (!$model->setDelete()) {
  86. return $this->renderError($model->getError() ?: '取消失败');
  87. }
  88. return $this->renderSuccess('取消成功');
  89. }
  90. /**
  91. * [getCertificateList]
  92. *
  93. * @Author: Yeshihao
  94. * @DateTime: 2024/11/27 10:51
  95. * @param array $category_ids
  96. * @return array|bool
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. * @throws \think\exception\DbException
  100. */
  101. public function getCertificateList($category_ids = [])
  102. {
  103. $model = new CertificateModel;
  104. if (!$list = $model->getCertificateList(['certificate_category_id' => ['in', $category_ids]], ['id', 'name'])) {
  105. return $this->renderError($model->getError() ?: '请求失败');
  106. }
  107. return $this->renderSuccess('请求成功', '', $list);
  108. }
  109. /**
  110. * [zipDownload]
  111. * 下载zip
  112. * @Author: Yeshihao
  113. * @DateTime: 2024/11/28 11:15
  114. * @param $id
  115. * @throws \think\exception\DbException
  116. */
  117. public function zipDownload($id)
  118. {
  119. $model = CertificateApplyModel::detail($id);
  120. if (empty($model) || empty($model['result_data'])) {
  121. return $this->renderError('查无文件');
  122. }
  123. //TP自带的类
  124. $zip = new \ZipArchive();
  125. //压缩文件名
  126. $filename = Date('Ymd').$model['project'].'.zip';
  127. $zipFilePath = RUNTIME_PATH."/temp/".$filename;
  128. if (file_exists($zipFilePath)) {
  129. unlink($zipFilePath);
  130. }
  131. //新建zip压缩包
  132. $zip->open($zipFilePath, \ZIPARCHIVE::OVERWRITE | \ZIPARCHIVE::CREATE);
  133. //循环压缩文件
  134. foreach ($model['result_data'] as $value) {
  135. $substr = mb_substr($value['file_path'], mb_strpos($value['file_path'], "."));
  136. //查询文件名称(若之前保存文件为源文件名保存可跳过该步骤)
  137. $zip->addFile($value['file_path'], basename($value['name'].$substr));
  138. }
  139. //打包zip
  140. $zip->close();
  141. header("Cache-Control:public");
  142. header("Content-Description: File Transfer");
  143. header("Content-disposition: attachment; filename=".basename($filename));//文件名
  144. header("Content-Type:application/zip"); //格式为zip
  145. header("Content-Transfer-Encoding:binary"); //这是二进制文件
  146. header("Content-Length:".filesize($zipFilePath)); //文件大小
  147. @readfile($zipFilePath);
  148. }
  149. }