- 在多个控制器中引入 ApiMessage,替换原有的 ApiResponse 错误处理逻辑,确保错误信息的一致性与可读性。 - 更新错误返回信息,使用更具语义的键值,提升 API 的可维护性与用户体验。 - 适配相关控制器的请求参数,确保在处理错误时能够正确返回相应的错误信息。
47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Support;
|
||
|
||
use App\Lottery\ErrorCode;
|
||
use Illuminate\Http\Request;
|
||
|
||
/**
|
||
* 【管理后台业务文案翻译辅助类】
|
||
*
|
||
* 从 lang/{locale}/admin.php 等语言包取字符串,供 JSON 里「msg」字段使用。
|
||
* 与 LotteryMessage 对应,专门处理管理端错误消息。
|
||
*/
|
||
final class AdminMessage
|
||
{
|
||
/**
|
||
* 取通用管理后台错误的用户可见文案(lang/{locale}/admin.php)。
|
||
*
|
||
* @param string $key 语言包键名,如 'unauthenticated', 'permission_denied'
|
||
*/
|
||
public static function get(Request $request, string $key, array $replace = []): string
|
||
{
|
||
return ApiMessage::get($request, 'admin.'.$key, $replace);
|
||
}
|
||
|
||
/**
|
||
* 取错误码对应的用户可见文案。
|
||
*
|
||
* @param int $code {@see ErrorCode} 管理端段(8110–8114)
|
||
*/
|
||
public static function errorCode(Request $request, int $code): string
|
||
{
|
||
$fallback = (string) config('lottery.locales.fallback', 'en');
|
||
$locale = (string) ($request->attributes->get('lottery_locale') ?? LotteryLocale::resolve($request));
|
||
$key = 'admin.'.$code;
|
||
|
||
$msg = trans($key, [], $locale);
|
||
if ($msg !== $key) {
|
||
return $msg;
|
||
}
|
||
|
||
return trans($key, [], $fallback);
|
||
}
|
||
}
|