| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace addons\Mch\client\controllers;
- use common\enums\DownloadEnum;
- use common\models\common\Download;
- use common\enums\StatusEnum;
- use common\helpers\csv\CsvExportHelper;
- use common\helpers\FileHelper;
- use Yii;
- /**
- *
- * Class DownloadController
- * @Author: hua
- * @DateTime: 2021/10/11 15:46
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @package backend\modules\mall\controllers
- */
- class DownloadController extends BaseController
- {
- /**
- *
- * @return mixed|string|void
- */
- 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,'mch_id'=>$this->mch_id],['<>','status',StatusEnum::DELETE]];
- if(!empty($params['name'])){
- $params['where'][] = ['like','name',$params['name']];
- }
- if(!empty($params['start'])){
- $params['where'][] = ['>=','created_at',strtotime($params['start'])];
- }
- if(!empty($params['end'])){
- $params['where'][] = ['<=','created_at',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);
- }
- /**
- * 删除
- * @Author: hua
- * @DateTime: 2021/10/11 16:05
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- 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['status'] = StatusEnum::DELETE;
- $res = Download::setData($params);
- if ($res === false) return $this->error(Download::getStaticError());
- return $this->success('操作成功');
- }
- }
- }
- /**
- * 下载
- * @Author: hua
- * @DateTime: 2021/10/13 14:53
- * @Copyright: copyright (c) 2021 广东七件事集团
- */
- public function actionDown(){
- try{
- $download_id = Yii::$app->request->get('download_id');
- ob_start();
- $download = Download::findOne($download_id);
- if (empty($download)) {
- $url = \Yii::$app->urlManager->createAbsoluteUrl('mch/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('mch/client/download/index');
- return $this->redirect($url, 301);
- }
- }
- /**
- * @name:
- * @msg: 重新生成下载文件
- * @param {*}
- * @return {*}
- */
- public function actionRegenerate()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $download_id = Yii::$app->request->get('download_id');
- $res = CsvExportHelper::regenerateDownloadFile($download_id);
- if ($res) {
- return $this->success('操作成功');
- } else {
- return $this->error('操作失败');
- }
- }
- /**
- * @name:
- * @msg: 批量下载导出文件
- * @param {*}
- * @return {*}
- */
- public function actionMultipleDownload()
- {
- 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, '/');
- }
- // dd($files);
- $filename = FileHelper::packFiles($files, '导出文件');
- // dd($filename);
- 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('操作成功');
- }
- }
|