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

@@ -0,0 +1,36 @@
<?php
namespace App\Support;
use Illuminate\Http\Request;
/**
* 【业务文案翻译辅助类】
*
* lang/{locale}/sso.php 等语言包取字符串,供 JSON 里「msg」字段使用。
* 当前实现:玩家 SSO / Bearer 鉴权相关错误码80018004 docs/04 错误码段。
*
* 依赖 NegotiateLotteryLocale 已写入 lottery_locale若未写则使用 fallback 语言包。
*/
final class LotteryMessage
{
/**
* SSO 鉴权类错误的用户可见文案(与 ApiResponse msg 对应)。
*
* @param int $code 业务错误码,如 8001
*/
public static function sso(Request $request, int $code): string
{
$fallback = (string) config('lottery.locales.fallback', 'en');
$locale = (string) ($request->attributes->get('lottery_locale') ?? $fallback);
$key = 'sso.'.$code; // 对应 lang/{locale}/sso.php 内键名,如 '8001'
$msg = trans($key, [], $locale);
// Laravel 无键时返回整条 key 字符串,此时改用 fallback 语言再试一次
if ($msg !== $key) {
return $msg;
}
return trans($key, [], $fallback);
}
}