DownloadController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace addons\Store\client\controllers;
  3. use common\enums\DownloadEnum;
  4. use common\enums\StatusEnum;
  5. use common\helpers\FileHelper;
  6. use common\models\common\Download;
  7. class DownloadController extends BaseController
  8. {
  9. /**
  10. * 下载页面
  11. * @return mixed|string|null
  12. */
  13. public function actionIndex()
  14. {
  15. if (\Yii::$app->request->isAjax) {
  16. if (\Yii::$app->request->isGet) {
  17. $params = \Yii::$app->request->get();
  18. $params['where'] = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'mch_id' => 0], ['<>', 'status', StatusEnum::DELETE]];
  19. if (!empty($params['name'])) {
  20. $params['where'][] = ['like', 'name', $params['name']];
  21. }
  22. if (!empty($params['start']) || !empty($params['end'])) {
  23. $params['where'][] = ['between', 'created_at', strtotime($params['start']), strtotime($params['end'])];
  24. }
  25. $params['order'] = 'id desc';
  26. $list = Download::listPage($params);
  27. $list['status_list'] = DownloadEnum::getStatusMap();
  28. $list['type_list'] = DownloadEnum::getTypeMap();
  29. return $this->success('操作成功', $list);
  30. }
  31. }
  32. return $this->render($this->action->id);
  33. }
  34. /**
  35. * 文件删除
  36. * @params id 文件id
  37. * @return mixed|void|null
  38. */
  39. public function actionDel()
  40. {
  41. $request = \Yii::$app->request;
  42. if ($request->isAjax) {
  43. if ($request->isGet) {
  44. $params = \Yii::$app->request->get();
  45. $params['mall_id'] = $this->mall_id;
  46. $params['store_id'] = $this->store_id;
  47. $params['status'] = StatusEnum::DELETE;
  48. $res = Download::setData($params);
  49. if ($res === false) return $this->error(Download::getStaticError());
  50. return $this->success('操作成功');
  51. }
  52. }
  53. }
  54. /**
  55. * 文件下载
  56. * @params download_id 文件id
  57. * @return void|\yii\web\Response
  58. */
  59. public function actionDownload()
  60. {
  61. try {
  62. $download_id = \Yii::$app->request->get('download_id');
  63. if (empty($download_id)) {
  64. return $this->error('参数错误');
  65. }
  66. ob_start();
  67. $download = Download::findOne($download_id);
  68. if (empty($download)) {
  69. $url = \Yii::$app->urlManager->createAbsoluteUrl('store/client/download/index');
  70. return $this->redirect($url, 301);
  71. }
  72. $filename = $download->name;
  73. $path_info = pathinfo($download->url);
  74. $size = readfile($download->url);
  75. header("Content-type: application/octet-stream ");
  76. header("Accept-Ranges: bytes ");
  77. header("Content-Disposition: attachment; filename= {$filename}." . $path_info['extension']);
  78. header("Accept-Length: " . $size);
  79. } catch (\Exception $e) {
  80. $url = \Yii::$app->urlManager->createAbsoluteUrl('store/client/download/index');
  81. return $this->redirect($url, 301);
  82. }
  83. }
  84. /**
  85. * 文件批量导出
  86. * @params download_id 文件id
  87. * @return mixed|null
  88. */
  89. public function actionDownloadAll()
  90. {
  91. if (!\Yii::$app->request->isGet) {
  92. return $this->error('请求方式错误');
  93. }
  94. $download_id = \Yii::$app->request->get('download_id');
  95. if (empty($download_id)) {
  96. return $this->error('参数错误');
  97. }
  98. $download_ids = explode(',', $download_id);
  99. $downloads = Download::findAll(['id' => $download_ids]);
  100. if (count($download_ids) != count($downloads)) {
  101. return $this->error('选择的文件中有不能下载的文件,请重新选择');
  102. }
  103. if (empty($downloads)) {
  104. return $this->error('选择的文件不存在,请重新选择');
  105. }
  106. $files = [];
  107. foreach ($downloads as $down) {
  108. $files[] = trim($down->url, '/');
  109. }
  110. $filename = FileHelper::packFiles($files, '导出文件');
  111. ob_start();
  112. $path_info = pathinfo($filename);
  113. $size = readfile($filename);
  114. header("Content-type: application/octet-stream ");
  115. header("Accept-Ranges: bytes ");
  116. header("Content-Disposition: attachment; filename= {$path_info['filename']}." . $path_info['extension']);
  117. header("Accept-Length: " . $size);
  118. return $this->success('操作成功');
  119. }
  120. }