validateDocumentAccess($document, $user)) { throw new \Exception('您没有权限访问此文档'); } if (!Storage::disk('local')->exists($document->file_path)) { throw new \Exception('文档不存在或已被删除'); } return Storage::disk('local')->download( $document->file_path, $document->file_name ); } /** * 记录文档下载日志 */ public function logDownload(Document $document, User $user, ?string $ipAddress = null): DownloadLog { return DownloadLog::create([ 'document_id' => $document->id, 'user_id' => $user->id, 'downloaded_at' => now(), 'ip_address' => $ipAddress ?? request()->ip(), ]); } }