From 3a2689db18a97f4da54163435aafbe6ac694be89 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Tue, 8 Sep 2020 14:10:27 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/excel/UserExport.php | 10 +++ extend/catcher/CatchUpload.php | 30 ++++----- extend/catcher/Utils.php | 12 ++++ extend/catcher/base/CatchRequest.php | 2 +- extend/catcher/library/excel/Excel.php | 61 +++++++++++++++---- .../middlewares/JsonResponseMiddleware.php | 2 - 6 files changed, 87 insertions(+), 30 deletions(-) create mode 100644 catch/permissions/excel/UserExport.php diff --git a/catch/permissions/excel/UserExport.php b/catch/permissions/excel/UserExport.php new file mode 100644 index 0000000..ef90e0a --- /dev/null +++ b/catch/permissions/excel/UserExport.php @@ -0,0 +1,10 @@ +initUploadConfig(); @@ -186,12 +186,12 @@ class CatchUpload return $this; } - /** - * - * @time 2020年01月25日 - * @param UploadedFile $file - * @return array - */ + /** + * + * @time 2020年01月25日 + * @param UploadedFile $file + * @return array + */ protected function data(UploadedFile $file) { return [ @@ -346,4 +346,4 @@ class CatchUpload throw new FailedException(sprintf('Driver [%s] Not Supported.', $driver)); } } -} +} \ No newline at end of file diff --git a/extend/catcher/Utils.php b/extend/catcher/Utils.php index 84fbd69..e1b126f 100644 --- a/extend/catcher/Utils.php +++ b/extend/catcher/Utils.php @@ -142,4 +142,16 @@ class Utils { return Config::where('key', $key)->value('value'); } + + /** + * public path + * + * @param string $path + * @time 2020年09月08日 + * @return string + */ + public static function publicPath($path = '') + { + return root_path($path ? 'public/'. $path : 'public'); + } } diff --git a/extend/catcher/base/CatchRequest.php b/extend/catcher/base/CatchRequest.php index 1ef7562..46f0fd8 100644 --- a/extend/catcher/base/CatchRequest.php +++ b/extend/catcher/base/CatchRequest.php @@ -86,7 +86,7 @@ class CatchRequest extends Request // 设置默认参数 if ($this->needCreatorId) { $this->param['creator_id'] = $this->user()->id; - $this->post['creator'] = $this->user()->id; + $this->post['creator_id'] = $this->user()->id; } return true; diff --git a/extend/catcher/library/excel/Excel.php b/extend/catcher/library/excel/Excel.php index a06823e..bcfae9b 100644 --- a/extend/catcher/library/excel/Excel.php +++ b/extend/catcher/library/excel/Excel.php @@ -3,10 +3,12 @@ namespace catcher\library\excel; use catcher\CatchUpload; use catcher\exceptions\FailedException; +use catcher\Utils; use PhpOffice\PhpSpreadsheet\Exception; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use think\file\UploadedFile; +use think\helper\Str; class Excel { @@ -21,35 +23,54 @@ class Excel protected $spreadsheet = null; + protected $extension = 'xlsx'; + /** * save * * @time 2020年05月25日 * @param ExcelContract $excel * @param $path - * @param null $disk - * @return bool + * @param string $disk + * @return mixed * @throws Exception */ - public function save(ExcelContract $excel, $path, $disk = null) + public function save(ExcelContract $excel, $path, $disk = 'local') { $this->excel = $excel; $this->init(); - Factory::make(pathinfo($path, PATHINFO_EXTENSION)) - ->setSpreadsheet($this->spreadsheet) - ->save($path); + !is_dir($path) && mkdir($path, 0777, true); - if (!file_exists($path)) { - throw new FailedException($path . ' generate failed'); + $file = $path . date('YmdHis').Str::random(6) . '.' .$this->extension; + Factory::make($this->extension) + ->setSpreadsheet($this->spreadsheet) + ->save($file); + + if (!file_exists($file)) { + throw new FailedException($file . ' generate failed'); } - if ($disk) { - $path = $this->upload($disk, $path); - } + if ($disk) { + $file = $this->upload($disk, $file); + } - return $path; + return ['url' => $file]; + } + + /** + * set extension + * + * @time 2020年09月08日 + * @param $extension + * @return $this + */ + public function setExtension($extension) + { + $this->extension = $extension; + + return $this; } /** @@ -219,11 +240,27 @@ class Excel */ protected function upload($disk, $path) { + if ($disk == 'local') { + return $this->local($path); + } $upload = new CatchUpload; return ($disk ? $upload->setDriver($disk) : $upload)->upload($this->uploadedFile($path)); } + /** + * 返回本地下载地址 + * + * @param $path + * @time 2020年09月08日 + * @return mixed + */ + protected function local($path) + { + return \config('filesystem.disks.local')['domain'] . '/' . + + str_replace('\\', '/', str_replace(Utils::publicPath(), '', $path)); + } /** * get uploaded file diff --git a/extend/catcher/middlewares/JsonResponseMiddleware.php b/extend/catcher/middlewares/JsonResponseMiddleware.php index a3e42d8..adbf073 100644 --- a/extend/catcher/middlewares/JsonResponseMiddleware.php +++ b/extend/catcher/middlewares/JsonResponseMiddleware.php @@ -20,8 +20,6 @@ // +---------------------------------------------------------------------- namespace catcher\middlewares; -use catcher\exceptions\FailedException; -use catcher\exceptions\PermissionForbiddenException; use think\Middleware; use think\Request;