user(); if (! $user instanceof WechatUser) { abort(403, '仅微信用户可上传'); } if ($early = $this->ensurePublicDiskReady()) { return $early; } if (!$request->hasFile('file')) { return response()->json(['message' => '未收到文件'], 422); } $uploaded = $request->file('file'); if (!$uploaded->isValid()) { return response()->json(['message' => $uploaded->getErrorMessage()], 422); } $data = $request->validate([ 'file' => ['required', 'file', 'max:5120', 'mimes:jpg,jpeg,png,gif,webp'], ]); try { Storage::disk('public')->makeDirectory('uploads/h5'); $path = Storage::disk('public')->putFile('uploads/h5', $data['file']); } catch (Throwable $e) { report($e); Log::error('h5_upload_putfile_failed', [ 'message' => $e->getMessage(), 'public_path' => storage_path('app/public'), ]); return response()->json([ 'message' => '文件保存失败', 'detail' => config('app.debug') ? $e->getMessage() : null, ], 500); } if ($path === false) { Log::error('h5_upload_putfile_returned_false', [ 'public_path' => storage_path('app/public'), ]); return response()->json([ 'message' => '文件保存失败(写入返回失败)', 'detail' => config('app.debug') ? 'Storage::putFile 返回 false' : null, ], 500); } $safePath = str_replace('\\', '/', $path); return response()->json([ 'url' => url('/storage/'.$safePath), 'path' => $path, ]); } }