From c3b36013d7323cdb75afe9370d95b81e99c074d2 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Mon, 1 Mar 2021 21:01:37 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=E4=BF=AE=E5=A4=8D=E9=99=84=E4=BB=B6?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/system/controller/Attachments.php | 20 +----- catch/system/model/Attachments.php | 35 ++++++++++ catch/system/model/search/DeveloperSearch.php | 2 +- extend/catcher/CatchUpload.php | 70 +------------------ extend/catcher/Utils.php | 60 ++++++++++++++++ 5 files changed, 99 insertions(+), 88 deletions(-) diff --git a/catch/system/controller/Attachments.php b/catch/system/controller/Attachments.php index d47f5e2..1fe40c0 100644 --- a/catch/system/controller/Attachments.php +++ b/catch/system/controller/Attachments.php @@ -4,8 +4,6 @@ namespace catchAdmin\system\controller; use catcher\base\CatchController; use catcher\CatchResponse; use catchAdmin\system\model\Attachments as AttachmentsModel; -use catcher\Utils; -use catcher\facade\FileSystem; class Attachments extends CatchController { @@ -35,22 +33,6 @@ class Attachments extends CatchController */ public function delete($id, AttachmentsModel $model) { - $attachments = $model->whereIn('id', Utils::stringToArrayBy($id))->select(); - - if ($model->deleteBy($id)) { - foreach ($attachments as $attachment) { - if ($attachment->driver == 'local') { - $localPath = config('filesystem.disks.local.root') . DIRECTORY_SEPARATOR; - $path = $localPath . str_replace('\\','\/', $attachment->path); - if (FileSystem::exists($path)) { - Filesystem::delete($path); - } - } else { - Filesystem::delete($attachment->path); - } - } - } - - return CatchResponse::success(); + return CatchResponse::success($model->deletes($id)); } } diff --git a/catch/system/model/Attachments.php b/catch/system/model/Attachments.php index 88cbd31..000b03e 100644 --- a/catch/system/model/Attachments.php +++ b/catch/system/model/Attachments.php @@ -3,8 +3,10 @@ namespace catchAdmin\system\model; use catchAdmin\system\model\search\AttachmentsSearch; use catcher\base\CatchModel; +use catcher\Utils; use think\file\UploadedFile; use think\Model; +use think\facade\Filesystem; class Attachments extends CatchModel { @@ -54,4 +56,37 @@ class Attachments extends CatchModel 'path' => $data['path'] ]); } + + /** + * 批量删除 + * + * @time 2021年03月01日 + * @param $id + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @return bool + */ + public function deletes($id): bool + { + Utils::setFilesystemConfig(); + + $this->whereIn('id', Utils::stringToArrayBy($id)) + ->select() + ->each(function ($attachment){ + if ($attachment->delete()) { + if ($attachment->driver == 'local') { + $localPath = config('filesystem.disks.local.root') . DIRECTORY_SEPARATOR; + $path = $localPath . str_replace('\\','\/', $attachment->path); + if (file_exists($path)) { + Filesystem::delete($path); + } + } else { + Filesystem::delete($attachment->path); + } + } + }); + + return true; + } } diff --git a/catch/system/model/search/DeveloperSearch.php b/catch/system/model/search/DeveloperSearch.php index 79516d9..21c9904 100644 --- a/catch/system/model/search/DeveloperSearch.php +++ b/catch/system/model/search/DeveloperSearch.php @@ -24,6 +24,6 @@ trait DeveloperSearch public function searchStatusAttr($query, $value, $data) { - return $query->where('driver', $value); + return $query->where('status', $value); } } \ No newline at end of file diff --git a/extend/catcher/CatchUpload.php b/extend/catcher/CatchUpload.php index ca4bc16..eede7ed 100644 --- a/extend/catcher/CatchUpload.php +++ b/extend/catcher/CatchUpload.php @@ -47,11 +47,6 @@ class CatchUpload */ protected $path = ''; - public function __construct() - { - $this->initDriver(); - } - /** * upload files * @@ -259,70 +254,9 @@ class CatchUpload * @time 2020年06月01日 * @return void */ - protected function initUploadConfig() + public function initUploadConfig() { - $configModel = app(Config::class); - - $upload = $configModel->where('key', 'upload')->find(); - - if ($upload) { - $disk = app()->config->get('filesystem.disks'); - - $uploadConfigs = $configModel->getConfig($upload->component); - - if (!empty($uploadConfigs)) { - // 读取上传可配置数据 - foreach ($uploadConfigs as $key => &$config) { - // $disk[$key]['type'] = $key; - // 腾讯云配置处理 - if (strtolower($key) == 'qcloud') { - $config['credentials'] = [ - 'appId' => $config['app_id'] ?? '', - 'secretKey' => $config['secret_key'] ?? '', - 'secretId' => $config['secret_id'] ?? '', - ]; - $readFromCdn = $config['read_from_cdn'] ?? 0; - $config['read_from_cdn'] = intval($readFromCdn) == 1; - } - // OSS 配置 - if (strtolower($key) == 'oss') { - $isCname = $config['is_cname'] ?? 0; - $config['is_cname'] = intval($isCname) == 1; - } - } - - // 合并数组 - array_walk($disk, function (&$item, $key) use ($uploadConfigs) { - if (!in_array($key, ['public', 'local'])) { - if ($uploadConfigs[$key] ?? false) { - foreach ($uploadConfigs[$key] as $k => $value) { - $item[$k] = $value; - } - } - } - }); - - // 重新分配配置 - app()->config->set([ - 'disks' => $disk, - ], 'filesystem'); - } - } - } - - /** - * 初始化 - * - * @time 2020年09月07日 - * @return $this - */ - protected function initDriver() - { - if ($driver = Utils::config('site.upload')) { - $this->driver = $driver; - } - - return $this; + Utils::setFilesystemConfig(); } /** diff --git a/extend/catcher/Utils.php b/extend/catcher/Utils.php index 5e9142a..ee415b8 100644 --- a/extend/catcher/Utils.php +++ b/extend/catcher/Utils.php @@ -233,4 +233,64 @@ class Utils return $data; } + + /** + * 设置 filesystem config + * + * @time 2021年03月01日 + * @return void + */ + public static function setFilesystemConfig() + { + $configModel = app(Config::class); + + $upload = $configModel->where('key', 'upload')->find(); + + if ($upload) { + $disk = app()->config->get('filesystem.disks'); + + $uploadConfigs = $configModel->getConfig($upload->component); + + if (!empty($uploadConfigs)) { + // 读取上传可配置数据 + foreach ($uploadConfigs as $key => &$config) { + // $disk[$key]['type'] = $key; + // 腾讯云配置处理 + if (strtolower($key) == 'qcloud') { + $config['credentials'] = [ + 'appId' => $config['app_id'] ?? '', + 'secretKey' => $config['secret_key'] ?? '', + 'secretId' => $config['secret_id'] ?? '', + ]; + $readFromCdn = $config['read_from_cdn'] ?? 0; + $config['read_from_cdn'] = intval($readFromCdn) == 1; + } + // OSS 配置 + if (strtolower($key) == 'oss') { + $isCname = $config['is_cname'] ?? 0; + $config['is_cname'] = intval($isCname) == 1; + } + } + + // 合并数组 + array_walk($disk, function (&$item, $key) use ($uploadConfigs) { + if (!in_array($key, ['public', 'local'])) { + if ($uploadConfigs[$key] ?? false) { + foreach ($uploadConfigs[$key] as $k => $value) { + $item[$k] = $value; + } + } + } + }); + + + $default = Utils::config('site.upload'); + // 重新分配配置 + app()->config->set([ + 'default' => $default ? : 'local', + 'disks' => $disk, + ], 'filesystem'); + } + } + } }