..
This commit is contained in:
@@ -8,6 +8,7 @@ use Hyperf\Contract\ContainerInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
abstract class AetherController
|
||||
{
|
||||
@@ -41,6 +42,7 @@ abstract class AetherController
|
||||
|
||||
/**
|
||||
* 创建资源 (RESTFul: POST resources).
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function create(): array
|
||||
{
|
||||
@@ -51,6 +53,7 @@ abstract class AetherController
|
||||
|
||||
/**
|
||||
* 更新资源 (RESTFul: PUT resources/{id}).
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function update(int $id): array
|
||||
{
|
||||
@@ -61,6 +64,7 @@ abstract class AetherController
|
||||
|
||||
/**
|
||||
* 删除资源 (RESTFul: DELETE resources/{id}).
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function delete(int $id): array
|
||||
{
|
||||
|
||||
@@ -6,10 +6,6 @@ namespace Aether;
|
||||
|
||||
class AetherResponse
|
||||
{
|
||||
public const SUCCESS = 0;
|
||||
|
||||
public const ERROR = 1;
|
||||
|
||||
/**
|
||||
* 成功响应.
|
||||
* @param null|mixed $data 数据
|
||||
@@ -18,10 +14,9 @@ class AetherResponse
|
||||
public static function success(mixed $data = null, string $message = '操作成功'): array
|
||||
{
|
||||
return [
|
||||
'code' => self::SUCCESS,
|
||||
'message' => $message,
|
||||
'data' => $data,
|
||||
'timestamp' => time(),
|
||||
Config::RESPONSE_FIELD_KEY_DATA => $data,
|
||||
Config::RESPONSE_FIELD_KEY_CODE => Config::RESPONSE_SUCCESS_CODE,
|
||||
Config::RESPONSE_FIELD_KEY_MESSAGE => $message ?: Config::RESPONSE_SUCCESS_MESSAGE,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -31,29 +26,27 @@ class AetherResponse
|
||||
* @param string $message 错误消息
|
||||
* @param null|mixed $data 附加数据
|
||||
*/
|
||||
public static function error(string $message = '', int $code = self::ERROR, mixed $data = null): array
|
||||
public static function error(string $message = '', int $code = Config::RESPONSE_FAIL_CODE, mixed $data = null): array
|
||||
{
|
||||
return [
|
||||
'code' => $code,
|
||||
'message' => $message ?: self::getDefaultMessage($code),
|
||||
'data' => $data,
|
||||
'timestamp' => time(),
|
||||
Config::RESPONSE_FIELD_KEY_CODE => $code,
|
||||
Config::RESPONSE_FIELD_KEY_DATA => $data,
|
||||
Config::RESPONSE_FIELD_KEY_MESSAGE => $message ?: self::getDefaultMessage($code),
|
||||
];
|
||||
}
|
||||
|
||||
public static function page($list, int $total, int $page, int $size): array
|
||||
{
|
||||
return [
|
||||
'code' => self::SUCCESS,
|
||||
'message' => 'success',
|
||||
'data' => [
|
||||
Config::RESPONSE_FIELD_KEY_CODE => Config::RESPONSE_SUCCESS_CODE,
|
||||
Config::RESPONSE_FIELD_KEY_MESSAGE => 'success',
|
||||
Config::RESPONSE_FIELD_KEY_LIST => [
|
||||
'list' => $list,
|
||||
'page' => $page,
|
||||
'size' => $size,
|
||||
'total' => $total,
|
||||
'pages' => (int) ceil($total / $size),
|
||||
],
|
||||
'timestamp' => time(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -11,4 +11,14 @@ class Config
|
||||
public const RESPONSE_FIELD_KEY_DATA = 'data';
|
||||
|
||||
public const RESPONSE_FIELD_KEY_MESSAGE = 'message';
|
||||
|
||||
public const RESPONSE_FIELD_KEY_LIST = 'list';
|
||||
|
||||
public const RESPONSE_SUCCESS_CODE = 200;
|
||||
|
||||
public const RESPONSE_SUCCESS_MESSAGE = 'success';
|
||||
|
||||
public const RESPONSE_FAIL_CODE = -1;
|
||||
|
||||
public const RESPONSE_FAIL_MESSAGE = 'fail';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user