From cb67dd094b9202041f6d7e3214d4c46b59a5f6ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Fri, 21 Mar 2025 09:21:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E3=80=81?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8A=E4=BC=A0=E5=AF=B9=E6=8E=A5=E9=98=BF?= =?UTF-8?q?=E9=87=8C=E4=BA=91OSS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/application/common/config/route.php | 8 +- .../common/controller/Attachment.php | 140 ++++++++++++++++++ .../application/common/model/Attachment.php | 55 +++++++ Server/application/common/util/AliyunOSS.php | 77 ++++++++++ .../common/{ => util}/AliyunSMS.php | 4 +- 5 files changed, 281 insertions(+), 3 deletions(-) create mode 100644 Server/application/common/controller/Attachment.php create mode 100644 Server/application/common/model/Attachment.php create mode 100644 Server/application/common/util/AliyunOSS.php rename Server/application/common/{ => util}/AliyunSMS.php (98%) diff --git a/Server/application/common/config/route.php b/Server/application/common/config/route.php index e8bb5000..9ddc28db 100644 --- a/Server/application/common/config/route.php +++ b/Server/application/common/config/route.php @@ -13,4 +13,10 @@ Route::group('v1/auth', function () { // 需要JWT认证的接口 Route::get('info', 'app\\common\\controller\\Auth@info')->middleware(['jwt']); // 获取用户信息 Route::post('refresh', 'app\\common\\controller\\Auth@refresh')->middleware(['jwt']); // 刷新令牌 -}); \ No newline at end of file +}); + +// 附件上传相关路由 +Route::group('v1/', function () { + Route::post('attachment/upload', 'app\\common\\controller\\Attachment@upload'); // 上传附件 + Route::get('attachment/:id', 'app\\common\\controller\\Attachment@info'); // 获取附件信息 +})->middleware(['jwt']); \ No newline at end of file diff --git a/Server/application/common/controller/Attachment.php b/Server/application/common/controller/Attachment.php new file mode 100644 index 00000000..78bbb759 --- /dev/null +++ b/Server/application/common/controller/Attachment.php @@ -0,0 +1,140 @@ + 400, + 'msg' => '请选择要上传的文件' + ]); + } + + // 验证文件 + $validate = \think\facade\Validate::rule([ + 'file' => [ + 'fileSize' => 10485760, // 10MB + 'fileExt' => 'jpg,jpeg,png,gif,doc,docx,pdf,zip,rar', + 'fileMime' => 'image/jpeg,image/png,image/gif,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/pdf,application/zip,application/x-rar-compressed' + ] + ]); + + if (!$validate->check(['file' => $file])) { + return json([ + 'code' => 400, + 'msg' => $validate->getError() + ]); + } + + // 生成文件hash + $hashKey = md5_file($file->getRealPath()); + + // 检查文件是否已存在 + $existFile = AttachmentModel::getByHashKey($hashKey); + if ($existFile) { + return json([ + 'code' => 200, + 'msg' => '文件已存在', + 'data' => [ + 'id' => $existFile['id'], + 'name' => $existFile['name'], + 'url' => $existFile['source'] + ] + ]); + } + + // 生成OSS对象名称 + $objectName = AliyunOSS::generateObjectName($file->getOriginalName()); + + // 上传到OSS + $result = AliyunOSS::uploadFile($file->getRealPath(), $objectName); + + if (!$result['success']) { + return json([ + 'code' => 500, + 'msg' => '文件上传失败:' . $result['error'] + ]); + } + + // 保存到数据库 + $attachmentData = [ + 'name' => Request::param('name') ?: $file->getOriginalName(), + 'hash_key' => $hashKey, + 'server' => 'aliyun_oss', + 'source' => $result['url'], + 'size' => $result['size'], + 'suffix' => pathinfo($file->getOriginalName(), PATHINFO_EXTENSION) + ]; + + $attachmentId = AttachmentModel::addAttachment($attachmentData); + + if (!$attachmentId) { + return json([ + 'code' => 500, + 'msg' => '保存附件信息失败' + ]); + } + + return json([ + 'code' => 200, + 'msg' => '上传成功', + 'data' => [ + 'id' => $attachmentId, + 'name' => $attachmentData['name'], + 'url' => $attachmentData['source'] + ] + ]); + + } catch (\Exception $e) { + return json([ + 'code' => 500, + 'msg' => '上传失败:' . $e->getMessage() + ]); + } + } + + /** + * 获取附件信息 + * @param int $id 附件ID + * @return \think\response\Json + */ + public function info($id) + { + try { + $attachment = AttachmentModel::find($id); + + if (!$attachment) { + return json([ + 'code' => 404, + 'msg' => '附件不存在' + ]); + } + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => $attachment + ]); + + } catch (\Exception $e) { + return json([ + 'code' => 500, + 'msg' => '获取失败:' . $e->getMessage() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/common/model/Attachment.php b/Server/application/common/model/Attachment.php new file mode 100644 index 00000000..cf3a8a57 --- /dev/null +++ b/Server/application/common/model/Attachment.php @@ -0,0 +1,55 @@ + 'integer', + 'dl_count' => 'integer', + 'size' => 'integer', + 'scene' => 'integer', + 'create_at' => 'datetime', + 'update_at' => 'datetime', + 'delete_at' => 'datetime' + ]; + + /** + * 添加附件记录 + * @param array $data 附件数据 + * @return int|bool + */ + public static function addAttachment($data) + { + $attachment = new self(); + return $attachment->allowField(true)->save($data); + } + + /** + * 根据hash_key获取附件 + * @param string $hashKey + * @return array|null + */ + public static function getByHashKey($hashKey) + { + return self::where('hash_key', $hashKey) + ->where('delete_at', null) + ->find(); + } +} \ No newline at end of file diff --git a/Server/application/common/util/AliyunOSS.php b/Server/application/common/util/AliyunOSS.php new file mode 100644 index 00000000..a8ec91c8 --- /dev/null +++ b/Server/application/common/util/AliyunOSS.php @@ -0,0 +1,77 @@ +getMessage()); + } + } + + /** + * 上传文件到OSS + * @param string $filePath 本地文件路径 + * @param string $objectName OSS对象名称 + * @return array + * @throws OssException + */ + public static function uploadFile($filePath, $objectName) + { + try { + $client = self::getClient(); + + // 上传文件 + $result = $client->uploadFile(self::BUCKET, $objectName, $filePath); + + // 获取文件访问URL + $url = $client->signUrl(self::BUCKET, $objectName, 3600); + + return [ + 'success' => true, + 'url' => $url, + 'object_name' => $objectName, + 'size' => filesize($filePath), + 'mime_type' => mime_content_type($filePath) + ]; + } catch (OssException $e) { + return [ + 'success' => false, + 'error' => $e->getMessage() + ]; + } + } + + /** + * 生成OSS对象名称 + * @param string $originalName 原始文件名 + * @return string + */ + public static function generateObjectName($originalName) + { + $ext = pathinfo($originalName, PATHINFO_EXTENSION); + $name = md5(uniqid(mt_rand(), true)); + return date('Y/m/d/') . $name . '.' . $ext; + } +} \ No newline at end of file diff --git a/Server/application/common/AliyunSMS.php b/Server/application/common/util/AliyunSMS.php similarity index 98% rename from Server/application/common/AliyunSMS.php rename to Server/application/common/util/AliyunSMS.php index 3a920d4b..e742591a 100644 --- a/Server/application/common/AliyunSMS.php +++ b/Server/application/common/util/AliyunSMS.php @@ -1,6 +1,6 @@