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('操作成功'); } }