Merge branch 'feature/stock_next' into develop
This commit is contained in:
@@ -4,6 +4,7 @@ namespace app\common\controller;
|
|||||||
|
|
||||||
use app\common\service\OssService;
|
use app\common\service\OssService;
|
||||||
use app\common\service\UploadedFilesService;
|
use app\common\service\UploadedFilesService;
|
||||||
|
use app\common\service\UploadService;
|
||||||
use app\common\traits\ResponseTrait;
|
use app\common\traits\ResponseTrait;
|
||||||
use support\Log;
|
use support\Log;
|
||||||
use support\Request;
|
use support\Request;
|
||||||
@@ -44,7 +45,7 @@ class UploadController
|
|||||||
$category = $request->input('category', 'general');
|
$category = $request->input('category', 'general');
|
||||||
|
|
||||||
$fileInfo = [
|
$fileInfo = [
|
||||||
'file_name' => $fileName,
|
'file_name' => basename($fileName),
|
||||||
'file_path' => $pathUrl,
|
'file_path' => $pathUrl,
|
||||||
'file_size' => $file->getSize(),
|
'file_size' => $file->getSize(),
|
||||||
'file_type' => $file->getUploadMimeType(),
|
'file_type' => $file->getUploadMimeType(),
|
||||||
@@ -162,7 +163,7 @@ class UploadController
|
|||||||
$partition = $scene;
|
$partition = $scene;
|
||||||
|
|
||||||
$fileInfo = [
|
$fileInfo = [
|
||||||
'file_name' => $result->file_name,
|
'file_name' => basename($result->file_name),
|
||||||
'file_path' => $result->file_name, // OSS存储路径
|
'file_path' => $result->file_name, // OSS存储路径
|
||||||
'file_size' => $file->getSize(),
|
'file_size' => $file->getSize(),
|
||||||
'file_type' => $file->getUploadMimeType(),
|
'file_type' => $file->getUploadMimeType(),
|
||||||
@@ -220,12 +221,26 @@ class UploadController
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$fileId = $request->input('id');
|
$fileId = $request->input('id');
|
||||||
|
$path = $request->input('path');
|
||||||
if (!$fileId) {
|
|
||||||
return $this->error('文件ID不能为空');
|
|
||||||
}
|
|
||||||
|
|
||||||
$uploadedFilesService = new UploadedFilesService();
|
$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);
|
$result = $uploadedFilesService->deleteFile($fileId);
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
|
|||||||
@@ -56,4 +56,24 @@ class OssService
|
|||||||
// 生成 OSS 私有 URL(有效期为 3600 秒)
|
// 生成 OSS 私有 URL(有效期为 3600 秒)
|
||||||
return $this->ossClient->signUrl($this->bucket, $objectKey, $expireTime);
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -61,7 +61,7 @@ class UploadService
|
|||||||
// 记录文件信息到数据库
|
// 记录文件信息到数据库
|
||||||
$uploadedFilesService = new UploadedFilesService();
|
$uploadedFilesService = new UploadedFilesService();
|
||||||
$fileInfo = [
|
$fileInfo = [
|
||||||
'file_name' => $objectKey,
|
'file_name' => basename($objectKey),
|
||||||
'file_path' => $objectKey,
|
'file_path' => $objectKey,
|
||||||
'file_size' => $size,
|
'file_size' => $size,
|
||||||
'file_type' => $mime,
|
'file_type' => $mime,
|
||||||
@@ -84,4 +84,63 @@ class UploadService
|
|||||||
throw new BusinessException($e->getMessage());
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -134,6 +134,23 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除文件
|
* 删除文件
|
||||||
*
|
*
|
||||||
@@ -148,12 +165,6 @@ class UploadedFilesService
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除物理文件
|
|
||||||
$fullPath = public_path() . $file->file_path;
|
|
||||||
if (file_exists($fullPath)) {
|
|
||||||
unlink($fullPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除数据库记录
|
// 删除数据库记录
|
||||||
return $this->logicDeleteBy(['where' => ['id' => $fileId]]);
|
return $this->logicDeleteBy(['where' => ['id' => $fileId]]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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('/proxy/ossurl', [app\common\controller\UploadController::class, 'proxy']);
|
||||||
Route::any('/upload', [app\common\controller\UploadController::class, 'uploadFile']);
|
Route::any('/upload', [app\common\controller\UploadController::class, 'uploadFile']);
|
||||||
Route::any('/upload/image', [app\common\controller\UploadController::class, 'uploadImage']);
|
Route::any('/upload/image', [app\common\controller\UploadController::class, 'uploadImage']);
|
||||||
|
Route::any('/upload/delete', [app\common\controller\UploadController::class, 'deleteFile']);
|
||||||
|
|
||||||
// 公共
|
// 公共
|
||||||
Route::group('/common', function () {
|
Route::group('/common', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user