Watermark.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(['id' => ['in', $model['certificate_ids']]]);
  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. * [detail]
  76. *
  77. * @Author: Yeshihao
  78. * @DateTime: 2024/11/28 16:52
  79. * @param $id
  80. * @return array|bool|mixed
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. */
  85. public function detail($id)
  86. {
  87. // 分类详情
  88. $model = CertificateApplyModel::detail($id);
  89. if (!$this->request->isAjax()) {
  90. $category = CertificateCategoryModel::getAll();
  91. $certificateList = CertificateModel::getCertificateList(['id' => ['in', $model['certificate_ids']]]);
  92. return $this->fetch('detail', compact('model', 'category', 'certificateList'));
  93. }
  94. // 更新记录
  95. if ($model->edit($this->postData())) {
  96. return $this->renderSuccess('审核成功', url('content.certificate.watermark/index'));
  97. }
  98. return $this->renderError($model->getError() ?: '审核失败');
  99. }
  100. /**
  101. * [delete]
  102. *
  103. * @Author: Yeshihao
  104. * @DateTime: 2024/11/28 10:09
  105. * @param $id
  106. * @return array|bool
  107. */
  108. public function delete($id)
  109. {
  110. $model = CertificateApplyModel::detail($id);
  111. if ($model['status'] != 10) {
  112. return $this->renderError('取消失败');
  113. }
  114. if (!$model->setDelete()) {
  115. return $this->renderError($model->getError() ?: '取消失败');
  116. }
  117. return $this->renderSuccess('取消成功');
  118. }
  119. /**
  120. * [getCertificateList]
  121. *
  122. * @Author: Yeshihao
  123. * @DateTime: 2024/11/27 10:51
  124. * @param array $category_ids
  125. * @return array|bool
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. * @throws \think\exception\DbException
  129. */
  130. public function getCertificateList($category_ids = [])
  131. {
  132. $model = new CertificateModel;
  133. if (!$list = $model->getCertificateList(['certificate_category_id' => ['in', $category_ids]], ['id', 'name'])) {
  134. return $this->renderError($model->getError() ?: '请求失败');
  135. }
  136. return $this->renderSuccess('请求成功', '', $list);
  137. }
  138. /**
  139. * [zipDownload]
  140. * 下载zip
  141. *
  142. * @Author: Yeshihao
  143. * @DateTime: 2024/11/28 11:15
  144. * @param $id
  145. * @throws \think\exception\DbException
  146. */
  147. public function zipDownload($id)
  148. {
  149. $model = CertificateApplyModel::detail($id);
  150. if (empty($model) || empty($model['result_data'])) {
  151. return $this->renderError('查无文件');
  152. }
  153. //TP自带的类
  154. $zip = new \ZipArchive();
  155. //压缩文件名
  156. $filename = Date('Ymd').$model['project'].'.zip';
  157. $zipFilePath = RUNTIME_PATH."/temp/".$filename;
  158. if (file_exists($zipFilePath)) {
  159. unlink($zipFilePath);
  160. }
  161. //新建zip压缩包
  162. $zip->open($zipFilePath, \ZIPARCHIVE::OVERWRITE | \ZIPARCHIVE::CREATE);
  163. //循环压缩文件
  164. foreach ($model['result_data'] as $value) {
  165. $substr = mb_substr($value['file_path'], mb_strrpos($value['file_path'], "."));
  166. //查询文件名称(若之前保存文件为源文件名保存可跳过该步骤)
  167. $zip->addFile($value['file_path'], basename($value['name'].$substr));
  168. }
  169. //打包zip
  170. $zip->close();
  171. header("Cache-Control:public");
  172. header("Content-Description: File Transfer");
  173. header("Content-disposition: attachment; filename=".basename($filename));//文件名
  174. header("Content-Type:application/zip"); //格式为zip
  175. header("Content-Transfer-Encoding:binary"); //这是二进制文件
  176. header("Content-Length:".filesize($zipFilePath)); //文件大小
  177. @readfile($zipFilePath);
  178. }
  179. }