diff --git a/app/console/service/ArticleService.php b/app/console/service/ArticleService.php index fffdaa1..601100d 100644 --- a/app/console/service/ArticleService.php +++ b/app/console/service/ArticleService.php @@ -76,6 +76,21 @@ class ArticleService extends BaseService $num = $params['num'] ?? 0; unset($params['num']); + // 将OSS直连地址替换为代理地址 + $ossDomain = 'https://vehicle-album.oss-cn-beijing.aliyuncs.com/'; + $proxyPath = '/console/upload/image/proxy?file='; + + $params['content'] = str_replace( + '?Expires=', + '&Expires=', + $params['content'] + ); + $params['content'] = str_replace( + $ossDomain, + $proxyPath, + $params['content'] + ); + Db::beginTransaction(); try { $info = $this->create($params); diff --git a/app/mzl/modules/upload/controller/UploadController.php b/app/mzl/modules/upload/controller/UploadController.php index 7114fa0..fc205fc 100644 --- a/app/mzl/modules/upload/controller/UploadController.php +++ b/app/mzl/modules/upload/controller/UploadController.php @@ -65,4 +65,26 @@ class UploadController return $this->errorResponse($e->getMessage()); } } + + public function proxy(Request $request) + { + $filePath = $request->get('file'); // 原OSS文件路径 + try { + // 生成签名URL + $signedUrl = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($filePath); + + // 获取图片数据并返回 + $imageData = file_get_contents($signedUrl); + + // 根据文件类型设置Content-Type + $mime = mime_content_type('data://application/octet-stream;base64,'.base64_encode($imageData)); + + return response($imageData, 200, [ + 'Content-Type' => $mime, + 'Cache-Control' => 'public, max-age=600' // 客户端缓存10分钟 + ]); + } catch (\Exception $e) { + return response('Image not found '.$e->getMessage(), 404); + } + } } \ No newline at end of file diff --git a/config/route.php b/config/route.php index b20a063..c3146c0 100644 --- a/config/route.php +++ b/config/route.php @@ -55,6 +55,9 @@ Route::group('/apis', function() { //文章列表 Route::any('/article/list', [\app\apis\controller\ArticleController::class, 'index']); Route::any('/article/{id}/detail', [\app\apis\controller\ArticleController::class, 'detail']); + + //图片代理 + Route::any('/upload/image/proxy', [\app\mzl\modules\upload\controller\UploadController::class, 'proxy']); }); // ====== ====== ====== 网站 ====== ====== ====== @@ -192,6 +195,7 @@ Route::group('/console', function() { // 上传 Route::any('/upload/image', [\app\mzl\modules\upload\controller\UploadController::class, 'uploadImage']); + Route::any('/upload/image/proxy', [\app\mzl\modules\upload\controller\UploadController::class, 'proxy']); // 型号管理 Route::any('/models/list', [\app\console\controller\ModelController::class, 'index']);