..
This commit is contained in:
@@ -101,4 +101,16 @@ class Campus extends AetherModel
|
|||||||
{
|
{
|
||||||
return $query->where('status', 1);
|
return $query->where('status', 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取校区下的老师.
|
||||||
|
* @return HasMany
|
||||||
|
*/
|
||||||
|
public function teachers(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Teacher::class, 'campus_id', 'id')
|
||||||
|
->where('status', 1)
|
||||||
|
->whereNull('deleted_at');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ class CampusValidator extends AetherValidator
|
|||||||
'province' => 'nullable|string|max:100',
|
'province' => 'nullable|string|max:100',
|
||||||
'city' => 'nullable|string|max:100',
|
'city' => 'nullable|string|max:100',
|
||||||
'status' => 'nullable|integer|in:0,1',
|
'status' => 'nullable|integer|in:0,1',
|
||||||
|
'page' => 'nullable|integer|min:1',
|
||||||
|
'size' => 'nullable|integer|min:1|max:100',
|
||||||
],
|
],
|
||||||
'messages' => [
|
'messages' => [
|
||||||
'level.in' => '层级只能是1、2、3',
|
'level.in' => '层级只能是1、2、3',
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ namespace Aether;
|
|||||||
|
|
||||||
class AetherResponse
|
class AetherResponse
|
||||||
{
|
{
|
||||||
|
public const SUCCESS = 0;
|
||||||
|
|
||||||
|
public const ERROR = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 成功响应.
|
* 成功响应.
|
||||||
* @param null|mixed $data 数据
|
* @param null|mixed $data 数据
|
||||||
@@ -14,7 +18,7 @@ class AetherResponse
|
|||||||
public static function success(mixed $data = null, string $message = '操作成功'): array
|
public static function success(mixed $data = null, string $message = '操作成功'): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'code' => 0,
|
'code' => self::SUCCESS,
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
'data' => $data,
|
'data' => $data,
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
@@ -27,7 +31,7 @@ class AetherResponse
|
|||||||
* @param string $message 错误消息
|
* @param string $message 错误消息
|
||||||
* @param null|mixed $data 附加数据
|
* @param null|mixed $data 附加数据
|
||||||
*/
|
*/
|
||||||
public static function error(int $code, string $message = '', mixed $data = null): array
|
public static function error(string $message = '', int $code = self::ERROR, mixed $data = null): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'code' => $code,
|
'code' => $code,
|
||||||
@@ -37,6 +41,22 @@ class AetherResponse
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function page($list, int $total, int $page, int $size): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'code' => self::SUCCESS,
|
||||||
|
'message' => 'success',
|
||||||
|
'data' => [
|
||||||
|
'list' => $list,
|
||||||
|
'page' => $page,
|
||||||
|
'size' => $size,
|
||||||
|
'total' => $total,
|
||||||
|
'pages' => (int) ceil($total / $size),
|
||||||
|
],
|
||||||
|
'timestamp' => time(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取默认错误消息.
|
* 获取默认错误消息.
|
||||||
*/
|
*/
|
||||||
|
|||||||
60
extend/Aether/PHP/Hyperf/GlobalExceptionHandler.php
Normal file
60
extend/Aether/PHP/Hyperf/GlobalExceptionHandler.php
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Aether;
|
||||||
|
|
||||||
|
use Aether\Exception\BusinessException;
|
||||||
|
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||||
|
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||||
|
use Psr\Http\Message\MessageInterface;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
use function Hyperf\Support\env;
|
||||||
|
|
||||||
|
class GlobalExceptionHandler extends ExceptionHandler
|
||||||
|
{
|
||||||
|
public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|ResponseInterface
|
||||||
|
{
|
||||||
|
// 处理业务异常
|
||||||
|
if ($throwable instanceof BusinessException) {
|
||||||
|
$data = [
|
||||||
|
'code' => $throwable->getCode(),
|
||||||
|
'message' => $throwable->getMessage(),
|
||||||
|
'data' => null,
|
||||||
|
'timestamp' => time(),
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
// 记录未知错误日志
|
||||||
|
$this->logger->error(sprintf(
|
||||||
|
'Unknown error: %s %s in %s:%d',
|
||||||
|
$throwable->getMessage(),
|
||||||
|
$throwable->getCode(),
|
||||||
|
$throwable->getFile(),
|
||||||
|
$throwable->getLine()
|
||||||
|
));
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'code' => 500,
|
||||||
|
'message' => 'Server internal error',
|
||||||
|
'data' => env('APP_ENV') === 'dev' ? [
|
||||||
|
'message' => $throwable->getMessage(),
|
||||||
|
'file' => $throwable->getFile(),
|
||||||
|
'line' => $throwable->getLine(),
|
||||||
|
'trace' => $throwable->getTraceAsString(),
|
||||||
|
] : null,
|
||||||
|
'timestamp' => time(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||||
|
return $response->withHeader('Content-Type', 'application/json')
|
||||||
|
->withBody(new SwooleStream($body));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isValid(Throwable $throwable): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user