From 7526611f9ce03290e1758aaae5f797108192009f Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Thu, 12 Jun 2025 14:50:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=8A=E4=BC=A0=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/v1/SiteConfig.php | 19 +++++++- app/admin/controller/v1/Upload.php | 63 +++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/app/admin/controller/v1/SiteConfig.php b/app/admin/controller/v1/SiteConfig.php index 165bc36f..a5175321 100644 --- a/app/admin/controller/v1/SiteConfig.php +++ b/app/admin/controller/v1/SiteConfig.php @@ -171,8 +171,25 @@ class SiteConfig if (!empty($configs)) { $configs_map = []; foreach ($configs as $cfg) { - $configs_map[$cfg['name']] = $cfg; + $current = &$configs_map; + + // 根据name中"."拆分为多维数组 + $parts = explode('.', $cfg['name']); + foreach ($parts as $part) { + if (!isset($current[$part])) { + $current[$part] = []; + } + $current = &$current[$part]; + } + $current = [ + 'id' => $cfg['id'], + 'title' => $cfg['title'], + 'name' => $cfg['name'], + 'value' => $cfg['value'] + ]; } + unset($current); + return $configs_map; } diff --git a/app/admin/controller/v1/Upload.php b/app/admin/controller/v1/Upload.php index c8ba0a66..03684874 100644 --- a/app/admin/controller/v1/Upload.php +++ b/app/admin/controller/v1/Upload.php @@ -42,9 +42,17 @@ class Upload $filemd5 = $file->md5(); $filesha1 = $file->sha1(); - $image_model = SysImageUploadRecordModel::md5($filemd5)->find(); + // 获取图片上传配置 + list( + 'filename_keep' => $filename_keep, + 'filemd5_unique' => $filemd5_unique + ) = $this->getUploadOptions('upload_image'); + // 是否需要根据文件MD5值检查文件是否已存在 + $image_model = $filemd5_unique ? SysImageUploadRecordModel::md5($filemd5)->find() : null; if (is_null($image_model)) { - $filename = Filesystem::disk('image')->putFile($param['module'], $file); + // 检查是否需要保留原文件名生成器 + $name_rule = fn() => $filename_keep ? $this->filenameGenerator($file) : null; + $filename = Filesystem::disk('image')->putFile($param['module'], $file, $name_rule()); // 处理图片 $image_manager = ImageManager::imagick(); $image = $image_manager->read('.' . $storage . '/' . $filename); @@ -146,6 +154,33 @@ class Upload return error('上传失败'); } + /** + * 文件名生成回调 + * + * @param \think\file\UploadedFile $file + * @return callable + */ + private function filenameGenerator(\think\file\UploadedFile $file): callable + { + return fn() => date('Ymd') . '/' . pathinfo($file->getOriginalName(), PATHINFO_FILENAME); + } + /** + * 获取上传配置 + * + * @param string $module + * @return array + */ + private function getUploadOptions($module) + { + $config_model = new \app\admin\controller\v1\SiteConfig; + $config = $config_model->getByGroupUniqueLabel('upload'); + $options = data_get($config, $module, []); + throw_if(empty($options), '上传配置错误'); + return [ + 'filename_keep' => (int)data_get($options, 'filename_keep.value', 0) == 1, + 'filemd5_unique' => (int)data_get($options, 'filemd5_unique.value', 0) == 1, + ]; + } /** * 获取水印配置 * @@ -260,9 +295,17 @@ class Upload $filemd5 = $file->md5(); $filesha1 = $file->sha1(); - $video = SysVideoUploadRecordModel::md5($filemd5)->find(); + // 获取视频上传配置 + list( + 'filename_keep' => $filename_keep, + 'filemd5_unique' => $filemd5_unique + ) = $this->getUploadOptions('upload_video'); + // 是否需要根据文件MD5值检查文件是否已存在 + $video = $filemd5_unique ? SysVideoUploadRecordModel::md5($filemd5)->find() : null; if (is_null($video)) { - $filename = Filesystem::disk('video')->putFile($param['module'], $file); + // 检查是否需要保留原文件名 + $name_rule = fn() => $filename_keep ? $this->filenameGenerator($file) : null; + $filename = Filesystem::disk('video')->putFile($param['module'], $file, $name_rule()); // 保存视频 $video = new SysVideoUploadRecordModel(); @@ -312,9 +355,17 @@ class Upload $filemd5 = $file->md5(); $filesha1 = $file->sha1(); - $attachment = SysAttachmentUploadRecordModel::md5($filemd5)->find(); + // 获取视频上传配置 + list( + 'filename_keep' => $filename_keep, + 'filemd5_unique' => $filemd5_unique + ) = $this->getUploadOptions('upload_attachment'); + // 是否需要根据文件MD5值检查文件是否已存在 + $attachment = $filemd5_unique ? SysAttachmentUploadRecordModel::md5($filemd5)->find() : null; if (is_null($attachment)) { - $filename = Filesystem::disk('public')->putFile('attachments', $file); + // 检查是否需要保留原文件名 + $name_rule = fn() => $filename_keep ? $this->filenameGenerator($file) : null; + $filename = Filesystem::disk('public')->putFile('attachments', $file, $name_rule()); // 保存视频 $attachment = new SysAttachmentUploadRecordModel();