基础数据-校区服务
This commit is contained in:
56
extend/Aether/PHP/Hyperf/AetherResponse.php
Normal file
56
extend/Aether/PHP/Hyperf/AetherResponse.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Aether;
|
||||
|
||||
class AetherResponse
|
||||
{
|
||||
/**
|
||||
* 成功响应.
|
||||
* @param null|mixed $data 数据
|
||||
* @param string $message 消息
|
||||
*/
|
||||
public static function success(mixed $data = null, string $message = '操作成功'): array
|
||||
{
|
||||
return [
|
||||
'code' => 0,
|
||||
'message' => $message,
|
||||
'data' => $data,
|
||||
'timestamp' => time(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误响应.
|
||||
* @param int $code 错误码
|
||||
* @param string $message 错误消息
|
||||
* @param null|mixed $data 附加数据
|
||||
*/
|
||||
public static function error(int $code, string $message = '', mixed $data = null): array
|
||||
{
|
||||
return [
|
||||
'code' => $code,
|
||||
'message' => $message ?: self::getDefaultMessage($code),
|
||||
'data' => $data,
|
||||
'timestamp' => time(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认错误消息.
|
||||
*/
|
||||
private static function getDefaultMessage(int $code): string
|
||||
{
|
||||
$messages = [
|
||||
400 => '请求参数错误',
|
||||
401 => '未授权',
|
||||
403 => '禁止访问',
|
||||
404 => '资源不存在',
|
||||
500 => '服务器内部错误',
|
||||
10001 => '校区不存在',
|
||||
];
|
||||
|
||||
return $messages[$code] ?? '未知错误';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user