| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace addons\Store\client\controllers;
- use common\enums\DownloadEnum;
- use common\enums\StatusEnum;
- use common\helpers\FileHelper;
- use common\models\common\Download;
- class DownloadController extends BaseController
- {
- /**
- * 下载页面
- * @return mixed|string|null
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $params['where'] = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'mch_id' => 0], ['<>', 'status', StatusEnum::DELETE]];
- if (!empty($params['name'])) {
- $params['where'][] = ['like', 'name', $params['name']];
- }
- if (!empty($params['start']) || !empty($params['end'])) {
- $params['where'][] = ['between', 'created_at', strtotime($params['start']), strtotime($params['end'])];
- }
- $params['order'] = 'id desc';
- $list = Download::listPage($params);
- $list['status_list'] = DownloadEnum::getStatusMap();
- $list['type_list'] = DownloadEnum::getTypeMap();
- return $this->success('操作成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * 文件删除
- * @params id 文件id
- * @return mixed|void|null
- */
- public function actionDel()
- {
- $request = \Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $params = \Yii::$app->request->get();
- $params['mall_id'] = $this->mall_id;
- $params['store_id'] = $this->store_id;
- $params['status'] = StatusEnum::DELETE;
- $res = Download::setData($params);
- if ($res === false) return $this->error(Download::getStaticError());
- return $this->success('操作成功');
- }
- }
- }
- /**
- * 文件下载
- * @params download_id 文件id
- * @return void|\yii\web\Response
- */
- public function actionDownload()
- {
- try {
- $download_id = \Yii::$app->request->get('download_id');
- if (empty($download_id)) {
- return $this->error('参数错误');
- }
- ob_start();
- $download = Download::findOne($download_id);
- if (empty($download)) {
- $url = \Yii::$app->urlManager->createAbsoluteUrl('store/client/download/index');
- return $this->redirect($url, 301);
- }
- $filename = $download->name;
- $path_info = pathinfo($download->url);
- $size = readfile($download->url);
- header("Content-type: application/octet-stream ");
- header("Accept-Ranges: bytes ");
- header("Content-Disposition: attachment; filename= {$filename}." . $path_info['extension']);
- header("Accept-Length: " . $size);
- } catch (\Exception $e) {
- $url = \Yii::$app->urlManager->createAbsoluteUrl('store/client/download/index');
- return $this->redirect($url, 301);
- }
- }
- /**
- * 文件批量导出
- * @params download_id 文件id
- * @return mixed|null
- */
- public function actionDownloadAll()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $download_id = \Yii::$app->request->get('download_id');
- if (empty($download_id)) {
- return $this->error('参数错误');
- }
- $download_ids = explode(',', $download_id);
- $downloads = Download::findAll(['id' => $download_ids]);
- if (count($download_ids) != count($downloads)) {
- return $this->error('选择的文件中有不能下载的文件,请重新选择');
- }
- if (empty($downloads)) {
- return $this->error('选择的文件不存在,请重新选择');
- }
- $files = [];
- foreach ($downloads as $down) {
- $files[] = trim($down->url, '/');
- }
- $filename = FileHelper::packFiles($files, '导出文件');
- ob_start();
- $path_info = pathinfo($filename);
- $size = readfile($filename);
- header("Content-type: application/octet-stream ");
- header("Accept-Ranges: bytes ");
- header("Content-Disposition: attachment; filename= {$path_info['filename']}." . $path_info['extension']);
- header("Accept-Length: " . $size);
- return $this->success('操作成功');
- }
- }
|