From 3c89a01a816a4471f39167eddcbf657da8d000fb Mon Sep 17 00:00:00 2001 From: Aether Date: Thu, 25 Sep 2025 10:01:34 +0800 Subject: [PATCH] .. --- app/Model/Campus.php | 12 ++++ app/Validator/CampusValidator.php | 2 + extend/Aether/PHP/Hyperf/AetherResponse.php | 24 +++++++- .../PHP/Hyperf/GlobalExceptionHandler.php | 60 +++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 extend/Aether/PHP/Hyperf/GlobalExceptionHandler.php diff --git a/app/Model/Campus.php b/app/Model/Campus.php index 45cd79b..bc7ce3e 100644 --- a/app/Model/Campus.php +++ b/app/Model/Campus.php @@ -101,4 +101,16 @@ class Campus extends AetherModel { 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'); + } + } diff --git a/app/Validator/CampusValidator.php b/app/Validator/CampusValidator.php index 0a81896..78149ac 100644 --- a/app/Validator/CampusValidator.php +++ b/app/Validator/CampusValidator.php @@ -54,6 +54,8 @@ class CampusValidator extends AetherValidator 'province' => 'nullable|string|max:100', 'city' => 'nullable|string|max:100', 'status' => 'nullable|integer|in:0,1', + 'page' => 'nullable|integer|min:1', + 'size' => 'nullable|integer|min:1|max:100', ], 'messages' => [ 'level.in' => '层级只能是1、2、3', diff --git a/extend/Aether/PHP/Hyperf/AetherResponse.php b/extend/Aether/PHP/Hyperf/AetherResponse.php index be14309..7546559 100644 --- a/extend/Aether/PHP/Hyperf/AetherResponse.php +++ b/extend/Aether/PHP/Hyperf/AetherResponse.php @@ -6,6 +6,10 @@ namespace Aether; class AetherResponse { + public const SUCCESS = 0; + + public const ERROR = 1; + /** * 成功响应. * @param null|mixed $data 数据 @@ -14,7 +18,7 @@ class AetherResponse public static function success(mixed $data = null, string $message = '操作成功'): array { return [ - 'code' => 0, + 'code' => self::SUCCESS, 'message' => $message, 'data' => $data, 'timestamp' => time(), @@ -27,7 +31,7 @@ class AetherResponse * @param string $message 错误消息 * @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 [ '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(), + ]; + } + /** * 获取默认错误消息. */ diff --git a/extend/Aether/PHP/Hyperf/GlobalExceptionHandler.php b/extend/Aether/PHP/Hyperf/GlobalExceptionHandler.php new file mode 100644 index 0000000..e99fef0 --- /dev/null +++ b/extend/Aether/PHP/Hyperf/GlobalExceptionHandler.php @@ -0,0 +1,60 @@ + $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; + } +}