From e59a690c31c057223de9d147d955b7e217434146 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 10 Sep 2025 14:33:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/controller/UploadController.php | 29 +++++++--- app/common/service/OssService.php | 20 +++++++ app/common/service/UploadService.php | 61 ++++++++++++++++++++- app/common/service/UploadedFilesService.php | 27 ++++++--- config/route.php | 1 + 5 files changed, 122 insertions(+), 16 deletions(-) diff --git a/app/common/controller/UploadController.php b/app/common/controller/UploadController.php index 1637350..678034b 100644 --- a/app/common/controller/UploadController.php +++ b/app/common/controller/UploadController.php @@ -4,6 +4,7 @@ namespace app\common\controller; use app\common\service\OssService; use app\common\service\UploadedFilesService; +use app\common\service\UploadService; use app\common\traits\ResponseTrait; use support\Log; use support\Request; @@ -44,7 +45,7 @@ class UploadController $category = $request->input('category', 'general'); $fileInfo = [ - 'file_name' => $fileName, + 'file_name' => basename($fileName), 'file_path' => $pathUrl, 'file_size' => $file->getSize(), 'file_type' => $file->getUploadMimeType(), @@ -162,7 +163,7 @@ class UploadController $partition = $scene; $fileInfo = [ - 'file_name' => $result->file_name, + 'file_name' => basename($result->file_name), 'file_path' => $result->file_name, // OSS存储路径 'file_size' => $file->getSize(), 'file_type' => $file->getUploadMimeType(), @@ -220,12 +221,26 @@ class UploadController { try { $fileId = $request->input('id'); - - if (!$fileId) { - return $this->error('文件ID不能为空'); - } - + $path = $request->input('path'); + $uploadedFilesService = new UploadedFilesService(); + if(!empty($path) && empty($fileId)){ + $where = ['file_path' => $path]; + $file = $uploadedFilesService->getFile($where); + $fileId = $file->id; + }elseif(empty($path) && !empty($fileId)){ + $where = ['id' => $fileId]; + $file = $uploadedFilesService->getFile($where); + $path = $file->file_path; + } + if (!$fileId) { + return $this->error('文件ID与路径不能为空'); + } + + // 删除 OSS 上的文件 + $ossService = new OssService(); + $ossService->deleteFile($path); + $result = $uploadedFilesService->deleteFile($fileId); if ($result) { diff --git a/app/common/service/OssService.php b/app/common/service/OssService.php index 4d75b1f..72a956e 100644 --- a/app/common/service/OssService.php +++ b/app/common/service/OssService.php @@ -56,4 +56,24 @@ class OssService // 生成 OSS 私有 URL(有效期为 3600 秒) return $this->ossClient->signUrl($this->bucket, $objectKey, $expireTime); } + + /** + * 删除 OSS 上的文件 + * @param string $objectKey OSS上存储的对象名称 + * @return bool 删除是否成功 + */ + public function deleteFile($objectKey) + { + try { + // 删除 OSS 上的文件 + $this->ossClient->deleteObject($this->bucket, $objectKey); + return true; + } catch (OssException $e) { + // 错误处理 + throw new \RuntimeException('OSS Delete Error: ' . $e->getMessage()); + } catch (\Throwable $th) { + // 错误处理 + throw new BusinessException('Delete Error: ' . $th->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index bec4942..33b7293 100644 --- a/app/common/service/UploadService.php +++ b/app/common/service/UploadService.php @@ -61,7 +61,7 @@ class UploadService // 记录文件信息到数据库 $uploadedFilesService = new UploadedFilesService(); $fileInfo = [ - 'file_name' => $objectKey, + 'file_name' => basename($objectKey), 'file_path' => $objectKey, 'file_size' => $size, 'file_type' => $mime, @@ -84,4 +84,63 @@ class UploadService throw new BusinessException($e->getMessage()); } } + + /** + * 删除 OSS 上的文件 + * + * @param array $param + * - object_key: OSS 对象键名-oss路径(必需) + * - file_id: 数据库文件记录ID(可选,用于删除数据库记录) + * @return bool 删除是否成功 + * + * 示例 + * $uploadService = new UploadService(); + * + * 只删除 OSS 文件 + * $uploadService->deleteOssFile([ + * 'object_key' => 'vehicle_album/documents/20241201/abc123.pdf' + * ]); + * + * 同时删除 OSS 文件和数据库记录 + * $uploadService->deleteOssFile([ + * 'object_key' => 'vehicle_album/images/20241201/abc123.jpg', + * 'file_id' => 123 + * ]); + */ + public function deleteOssFile($objectKey,$fileId = null) + { + try { + + if (empty($objectKey)) { + Log::error('OSS对象键名不能为空', $objectKey); + throw new BusinessException('OSS对象键名不能为空'); + } + + $uploadedFilesService = new UploadedFilesService(); + // 如果提供了文件ID,同时删除数据库记录 + if ($fileId) { + $uploadedFilesService->deleteFile($fileId); + }else{ + $uploadedFilesService = new UploadedFilesService(); + if (empty($fileId)) { + $where = ['file_path' => $objectKey]; + $file = $uploadedFilesService->getFile($where); + $fileId = $file->id; + } + if(!empty($fileId)){ + $uploadedFilesService->deleteFile($fileId); + } + } + + // 删除 OSS 上的文件 + $ossService = new OssService(); + $ossService->deleteFile($objectKey); + + Log::info('OSS文件删除成功', ['object_key' => $objectKey, 'file_id' => $fileId]); + return true; + } catch (\Throwable $e) { + Log::error('删除OSS文件失败: ' . $e->getMessage()); + throw new BusinessException('删除OSS文件失败: ' . $e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/service/UploadedFilesService.php b/app/common/service/UploadedFilesService.php index 1319ab3..3689ac0 100644 --- a/app/common/service/UploadedFilesService.php +++ b/app/common/service/UploadedFilesService.php @@ -134,26 +134,37 @@ class UploadedFilesService ]; } + /** + * 获取文件 + * + * @param string $path + * @return bool|\Illuminate\Database\Eloquent\Model + */ + public function getFile(array $where) + { + $file = $this->logicShow(['where' => $where]); + + if (!$file) { + return false; + } + + return $file; + } + /** * 删除文件 - * + * * @param int $fileId * @return bool */ public function deleteFile(int $fileId): bool { $file = $this->logicShow(['where' => ['id' => $fileId]]); - + if (!$file) { return false; } - // 删除物理文件 - $fullPath = public_path() . $file->file_path; - if (file_exists($fullPath)) { - unlink($fullPath); - } - // 删除数据库记录 return $this->logicDeleteBy(['where' => ['id' => $fileId]]); } diff --git a/config/route.php b/config/route.php index b1eb26e..69c2d56 100644 --- a/config/route.php +++ b/config/route.php @@ -37,6 +37,7 @@ Route::any('/feishu/callback', [app\common\controller\NotifyController::class, ' Route::any('/proxy/ossurl', [app\common\controller\UploadController::class, 'proxy']); Route::any('/upload', [app\common\controller\UploadController::class, 'uploadFile']); Route::any('/upload/image', [app\common\controller\UploadController::class, 'uploadImage']); +Route::any('/upload/delete', [app\common\controller\UploadController::class, 'deleteFile']); // 公共 Route::group('/common', function () { From b1203379878f203d54df451a287b4bb918f7daa8 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 10 Sep 2025 15:02:11 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/service/UploadedFilesService.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/common/service/UploadedFilesService.php b/app/common/service/UploadedFilesService.php index 3689ac0..1e2435d 100644 --- a/app/common/service/UploadedFilesService.php +++ b/app/common/service/UploadedFilesService.php @@ -142,7 +142,9 @@ class UploadedFilesService */ public function getFile(array $where) { - $file = $this->logicShow(['where' => $where]); + $config['model'] = $this->modelClass; + $config['where'] = $where; + $file = $this->logicShow($config); if (!$file) { return false; @@ -159,14 +161,20 @@ class UploadedFilesService */ public function deleteFile(int $fileId): bool { - $file = $this->logicShow(['where' => ['id' => $fileId]]); + $config['model'] = $this->modelClass; + $config['where'] = ['id' => $fileId]; + $file = $this->logicShow($config); if (!$file) { return false; } // 删除数据库记录 - return $this->logicDeleteBy(['where' => ['id' => $fileId]]); + $config =[ + 'model' => $this->modelClass, + 'where' => ['id' => $fileId], + ]; + return $this->logicDeleteBy($config); } /** From e3aaee87f1005575644e7e444f7f2c12518bb8bc Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 10 Sep 2025 16:09:58 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/crm/controller/CustomerController.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/crm/controller/CustomerController.php b/app/crm/controller/CustomerController.php index 0860af8..88f6b6f 100644 --- a/app/crm/controller/CustomerController.php +++ b/app/crm/controller/CustomerController.php @@ -295,14 +295,10 @@ class CustomerController extends BaseController if (Util::isEmpty($channel_id)) { return $this->error(trans('channel_not_null')); } - $country_id = Util::getParam($request, 'country_id', 0); - if (Util::isEmpty($country_id)) { -// return $this->error('country_id'.trans('not_null')); + $country_id = Util::getParam($request, 'country_id', 245); + if(empty($country_id)){ + $country_id = 245; } -// $car_type_id = Util::getParam($request, 'car_type_id'); -// if (Util::isEmpty($car_type_id)) { -// return $this->error(trans('car_type_not_null')); -// } $contacts = Util::getParam($request, 'contacts'); $email = Util::getParam($request, 'email'); $phone = Util::getParam($request, 'phone');