feat: 增强玩家端 API 鉴权中间件,支持多语言错误消息返回
This commit is contained in:
@@ -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() 按请求语言翻译
|
||||
* (依赖前置的 NegotiateLotteryLocale),code 仍为文档约定业务码(如 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 混淆
|
||||
|
||||
Reference in New Issue
Block a user