DownloadController.php 5.4 KB

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