DownloadController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace backend\modules\mall\controllers;
  3. use common\enums\DownloadEnum;
  4. use common\models\common\Download;
  5. use backend\controllers\MallController;
  6. use common\enums\StatusEnum;
  7. use common\helpers\csv\CsvExportHelper;
  8. use common\helpers\FileHelper;
  9. use Yii;
  10. use yii\web\ForbiddenHttpException;
  11. use function GuzzleHttp\Psr7\str;
  12. /**
  13. *
  14. * Class DownloadController
  15. * @Author: hua
  16. * @DateTime: 2021/10/11 15:46
  17. * @Copyright: copyright (c) 2021 广东七件事集团
  18. * @package backend\modules\mall\controllers
  19. */
  20. class DownloadController extends MallController
  21. {
  22. /**
  23. *
  24. * @return mixed|string|void
  25. */
  26. public function actionIndex()
  27. {
  28. if (\Yii::$app->request->isAjax) {
  29. if (\Yii::$app->request->isGet) {
  30. $params = Yii::$app->request->get();
  31. $params['where'] = [['mall_id'=>$this->mall_id,'store_id' => 0, 'mch_id' => 0],['<>','status',StatusEnum::DELETE]];
  32. if(!empty($params['name'])){
  33. $params['where'][] = ['like','name',$params['name']];
  34. }
  35. if(!empty($params['datetime'][0])){
  36. $params['where'][] = ['>=','created_at',strtotime($params['datetime'][0])];
  37. }
  38. if(!empty($params['datetime'][1])){
  39. $params['where'][] = ['<=','created_at',strtotime($params['datetime'][1])];
  40. }
  41. $params['order'] = 'id desc';
  42. $list = Download::listPage($params);
  43. $list['status_list']=DownloadEnum::getStatusMap();
  44. $list['type_list']=DownloadEnum::getTypeMap();
  45. return $this->success('操作成功', $list);
  46. }
  47. }
  48. return $this->render($this->action->id);
  49. }
  50. /**
  51. * 删除
  52. * @Author: hua
  53. * @DateTime: 2021/10/11 16:05
  54. * @Copyright: copyright (c) 2021 广东七件事集团
  55. * @return mixed|void
  56. */
  57. public function actionDel()
  58. {
  59. $request = Yii::$app->request;
  60. if ($request->isAjax) {
  61. if ($request->isGet) {
  62. $params = Yii::$app->request->get();
  63. $params['mall_id'] = $this->mall_id;
  64. $params['status'] = StatusEnum::DELETE;
  65. $res = Download::setData($params);
  66. if ($res === false) return $this->error(Download::getStaticError());
  67. return $this->success('操作成功');
  68. }
  69. }
  70. }
  71. /**
  72. * 下载
  73. * @Author: hua
  74. * @DateTime: 2021/10/13 14:53
  75. * @Copyright: copyright (c) 2021 广东七件事集团
  76. */
  77. public function actionDown(){
  78. try{
  79. $download_id = Yii::$app->request->get('download_id');
  80. ob_start();
  81. $download = Download::findOne($download_id);
  82. if (empty($download)) {
  83. $url = \Yii::$app->urlManager->createAbsoluteUrl('mall/download/index');
  84. return $this->redirect($url, 301);
  85. }
  86. $filename= $download->name;
  87. $path_info = pathinfo($download->url);
  88. $size=readfile($download->url);
  89. header( "Content-type: application/octet-stream ");
  90. header( "Accept-Ranges: bytes ");
  91. header( "Content-Disposition: attachment; filename= {$filename}.".$path_info['extension']);
  92. header( "Accept-Length: " .$size);
  93. }catch (\Exception $e){
  94. $url = \Yii::$app->urlManager->createAbsoluteUrl('mall/download/index');
  95. return $this->redirect($url, 301);
  96. }
  97. }
  98. /**
  99. * @name:
  100. * @msg: 重新生成下载文件
  101. * @param {*}
  102. * @return {*}
  103. */
  104. public function actionRegenerate()
  105. {
  106. if (!\Yii::$app->request->isGet) {
  107. return $this->error('请求方式错误');
  108. }
  109. $download_id = Yii::$app->request->get('download_id');
  110. $res = CsvExportHelper::regenerateDownloadFile($download_id);
  111. if ($res) {
  112. return $this->success('操作成功');
  113. } else {
  114. return $this->error('操作失败');
  115. }
  116. }
  117. /**
  118. * @name:
  119. * @msg: 批量下载导出文件
  120. * @param {*}
  121. * @return {*}
  122. */
  123. public function actionMultipleDownload()
  124. {
  125. if (!\Yii::$app->request->isGet) {
  126. return $this->error('请求方式错误');
  127. }
  128. $download_id = \Yii::$app->request->get('download_id');
  129. if (empty($download_id)) {
  130. return $this->error('参数错误');
  131. }
  132. $download_ids = explode(',', $download_id);
  133. $downloads = Download::findAll(['id' => $download_ids]);
  134. if (count($download_ids) != count($downloads)) {
  135. return $this->error('选择的文件中有不能下载的文件,请重新选择');
  136. }
  137. if (empty($downloads)) {
  138. return $this->error('选择的文件不存在,请重新选择');
  139. }
  140. $files = [];
  141. foreach ($downloads as $down) {
  142. $files[] = trim($down->url, '/');
  143. }
  144. // dd($files);
  145. $filename = FileHelper::packFiles($files, '导出文件');
  146. // dd($filename);
  147. ob_start();
  148. $path_info = pathinfo($filename);
  149. $size=readfile($filename);
  150. header( "Content-type: application/octet-stream ");
  151. header( "Accept-Ranges: bytes ");
  152. header( "Content-Disposition: attachment; filename= {$path_info['filename']}." . $path_info['extension']);
  153. header( "Accept-Length: " .$size);
  154. return $this->success('操作成功');
  155. }
  156. }