feat: 增强玩家端 API 鉴权中间件,支持多语言错误消息返回

This commit is contained in:
2026-05-08 15:20:36 +08:00
parent 9f8080cefe
commit fc0999664a
9 changed files with 199 additions and 4 deletions

View File

@@ -5,15 +5,19 @@ namespace App\Http\Middleware;
use App\Exceptions\PlayerAuthenticationException;
use App\Services\PlayerTokenResolver;
use App\Support\ApiResponse;
use App\Support\LotteryMessage;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* 玩家端受保护路由前置:解析 Authorization失败时直接返回 { code, msg, data },不进入控制器。
* 玩家端 API 鉴权中间件】
*
* 成功后在 request 上挂 `lottery_player`,控制器内使用 `$request->lotteryPlayer()`
*(由 AppServiceProvider 注册的宏,返回 ?Player
* - 成功:解析 Bearer Player写入 request attribute `lottery_player`
* - 失败:直接 JSON 返回,不进入控制器;其中 msg 经由 LotteryMessage::sso() 按请求语言翻译
* (依赖前置的 NegotiateLotteryLocalecode 仍为文档约定业务码(如 8001)。
*
* PlayerAuthenticationException getMessage() 仅作开发与日志用语,可与 API msg 语种不一致。
*/
class EnsurePlayerApi
{
@@ -22,7 +26,13 @@ class EnsurePlayerApi
try {
$player = app(PlayerTokenResolver::class)->resolve($request);
} catch (PlayerAuthenticationException $e) {
return ApiResponse::error($e->getMessage(), $e->lotteryCode, null, $e->httpStatus);
// msg多语言用户提示code / httpStatus仍来自异常内业务定义
return ApiResponse::error(
LotteryMessage::sso($request, $e->lotteryCode),
$e->lotteryCode,
null,
$e->httpStatus,
);
}
// 使用 attributes避免与 Laravel 内置 input 混淆