From 88bc59ed87b94695ac30703a9a0b7df9923b4538 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Mon, 15 Sep 2025 17:10:22 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=8F=AF=E5=88=B6?= =?UTF-8?q?=E5=AE=9A=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/service/UploadService.php | 35 ++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index 33b9137..4204020 100644 --- a/app/common/service/UploadService.php +++ b/app/common/service/UploadService.php @@ -51,8 +51,39 @@ class UploadService $sceneUrl = $scene ? ($scene . '/') : ''; $dir = 'x_server/' . $sceneUrl . date('Ymd') . '/'; - $uniqueName = md5($originalName . microtime(true)); - $objectKey = $dir . $uniqueName . ($extension ? ('.' . $extension) : ''); + // 使用传入的 original_name 命名(缺少扩展名时补上推断的扩展名),并做简单安全处理 + $safeBaseName = preg_replace('/[\\\\\/]+/', '_', $originalName); + if ($extension && strtolower(pathinfo($safeBaseName, PATHINFO_EXTENSION)) !== strtolower($extension)) { + // 若提供的名称没有扩展名或扩展名不一致,则以推断扩展名为准 + $safeBaseName = pathinfo($safeBaseName, PATHINFO_FILENAME) . '.' . $extension; + } elseif (!$extension && !pathinfo($safeBaseName, PATHINFO_EXTENSION)) { + // 实在没有扩展名信息时,保持原名 + } + + // 重名检测:同一目录、同名且同类型则在文件名后追加序号 + $uploadedFilesService = new UploadedFilesService(); + $candidateName = $safeBaseName; + $nameBody = pathinfo($safeBaseName, PATHINFO_FILENAME); + $nameExt = pathinfo($safeBaseName, PATHINFO_EXTENSION); + $counter = 1; + while (true) { + $candidatePath = $dir . $candidateName; + $existing = $uploadedFilesService->getFile(['file_path' => $candidatePath]); + if (!$existing) { + break; + } + // 若存在同路径同名,且类型也相同,则追加序号 + if (isset($existing->file_type) && strtolower($existing->file_type) === strtolower($mime)) { + $suffix = ' (' . $counter . ')'; + $candidateName = $nameBody . $suffix . ($nameExt !== '' ? ('.' . $nameExt) : ''); + $counter++; + continue; + } + // 类型不同则允许同名,结束循环 + break; + } + + $objectKey = $dir . $candidateName; $ossService = new OssService(); $ossService->uploadImage($filePath, $objectKey); From c24c9af1fd7945fa6d6055c8da98817199bc9f29 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Mon, 15 Sep 2025 17:27:26 +0800 Subject: [PATCH 2/4] 1 --- app/common/service/UploadService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index 4204020..43684cf 100644 --- a/app/common/service/UploadService.php +++ b/app/common/service/UploadService.php @@ -68,7 +68,7 @@ class UploadService $counter = 1; while (true) { $candidatePath = $dir . $candidateName; - $existing = $uploadedFilesService->getFile(['file_path' => $candidatePath]); + $existing = $uploadedFilesService->findFile(['file_path' => $candidatePath]); if (!$existing) { break; } From e41be712d346a2df30739a9cb6e3b3f3086ccd1c Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Mon, 15 Sep 2025 17:33:34 +0800 Subject: [PATCH 3/4] 1 --- app/common/service/UploadedFilesService.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/common/service/UploadedFilesService.php b/app/common/service/UploadedFilesService.php index 1e2435d..ed01db2 100644 --- a/app/common/service/UploadedFilesService.php +++ b/app/common/service/UploadedFilesService.php @@ -152,6 +152,22 @@ class UploadedFilesService return $file; } + /** + * 获取文件 + * + * @param string $path + * @return bool|\Illuminate\Database\Eloquent\Model + */ + public function findFile(array $where) + { + $file = UploadedFiles::where(['file_path' => $where['file_path']]); + + if (!$file) { + return false; + } + + return $file; + } /** * 删除文件 From 48bd9c516de98f4e4e8d020a389bad3c3318c55d Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Tue, 16 Sep 2025 09:49:16 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=BC=96=E7=A0=81=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/service/VehColorService.php | 36 ++++++++++++++++ app/admin/service/VehModificationService.php | 43 ++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/app/admin/service/VehColorService.php b/app/admin/service/VehColorService.php index be83fcd..7c111c8 100644 --- a/app/admin/service/VehColorService.php +++ b/app/admin/service/VehColorService.php @@ -48,11 +48,47 @@ class VehColorService ); } + /** + * 生成唯一的7位颜色编码 + * 格式:C + (O/I) + 5位字母数字组合 + * + * @param int $type 类型:2=I,其他=O + * @return string 7位唯一编码 + */ + public function generateUniqueColorCode($type) + { + $secondChar = $type ==2 ? 'I' : 'O'; + $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $maxAttempts = 10; // 防止无限循环 + + for ($attempt = 0; $attempt < $maxAttempts; $attempt++) { + // 生成后5位随机字符 + $suffix = ''; + for ($i = 0; $i < 5; $i++) { + $suffix .= $chars[rand(0, strlen($chars) - 1)]; + } + + $colorCode = 'C' . $secondChar . $suffix; + + // 检查是否已存在 + $existing = VehColor::where('color_code', $colorCode)->first(); + if (!$existing) { + return $colorCode; + } + } + + // 如果1000次尝试后仍然重复,使用时间戳确保唯一性 + $timestamp = substr(time(), -5); + return 'C' . $secondChar . $timestamp; + } + /** * 添加前置处理 */ public function beforeServiceStore($data, Request $request) { + $type = $data['type'] ?? 0; + $data['color_code'] = $this->generateUniqueColorCode($type); $data['created_by'] = $request->user->uid; $data['updated_by'] = $request->user->uid; return $data; diff --git a/app/admin/service/VehModificationService.php b/app/admin/service/VehModificationService.php index 6c56d9b..038129b 100644 --- a/app/admin/service/VehModificationService.php +++ b/app/admin/service/VehModificationService.php @@ -80,4 +80,47 @@ class VehModificationService return $config; } + + /** + * 生成唯一的7位颜色编码 + * 格式:C + (O/I) + 5位字母数字组合 + * + * @param int $type 类型 + * @return string 7位唯一编码 + */ + public function generateUniqueModificationCode($type) + { + $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $maxAttempts = 10; // 防止无限循环 + + for ($attempt = 0; $attempt < $maxAttempts; $attempt++) { + // 生成后5位随机字符 + $suffix = ''; + for ($i = 0; $i < 5; $i++) { + $suffix .= $chars[rand(0, strlen($chars) - 1)]; + } + + $code = 'C' . $type . $suffix; + + // 检查是否已存在 + $existing = VehModification::where('code', $code)->first(); + if (!$existing) { + return $code; + } + } + + // 如果1000次尝试后仍然重复,使用时间戳确保唯一性 + $timestamp = substr(time(), -5); + return 'C' . $type . $timestamp; + } + + /** + * 添加前置处理 + */ + public function beforeServiceStore($data, Request $request) + { + $type = $data['type'] ?? 0; + $data['code'] = $this->generateUniqueModificationCode($type); + return $data; + } } \ No newline at end of file