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 diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index 33b9137..43684cf 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->findFile(['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); 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; + } /** * 删除文件