校区服务

This commit is contained in:
Aether
2025-09-18 15:54:38 +08:00
parent 0920cef866
commit 30e00c5949
15 changed files with 486 additions and 240 deletions

View File

@@ -1,30 +0,0 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Container\ContainerInterface;
abstract class AbstractController
{
#[Inject]
protected ContainerInterface $container;
#[Inject]
protected RequestInterface $request;
#[Inject]
protected ResponseInterface $response;
}

View File

@@ -1,27 +0,0 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
class IndexController extends AbstractController
{
public function index()
{
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
return [
'method' => $method,
'message' => "Hello {$user}.",
];
}
}

View File

@@ -1,38 +0,0 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Exception\Handler;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Throwable;
class AppExceptionHandler extends ExceptionHandler
{
public function __construct(protected StdoutLoggerInterface $logger)
{
}
public function handle(Throwable $throwable, ResponseInterface $response)
{
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
$this->logger->error($throwable->getTraceAsString());
return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.'));
}
public function isValid(Throwable $throwable): bool
{
return true;
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\JsonRpc\Contract;
interface CampusServiceInterface
{
/**
* 获取单个校区详情.
* @param int $id 校区ID
*/
public function getCampus(int $id): array;
/**
* 批量获取校区信息.
* @param array $ids 校区ID列表
*/
public function batchGetCampus(array $ids): array;
/**
* 按省份获取校区列表.
* @param string $province 省份名称
*/
public function getCampusByProvince(string $province): array;
/**
* 按城市获取校区列表.
* @param string $province 省份
* @param string $city 城市
*/
public function getCampusByCity(string $province, string $city): array;
/**
* 获取校区完整层级路径.
* @param int $campusId 校区ID
* @return array 如:[省份, 城市, 校区]
*/
public function getCampusHierarchy(int $campusId): array;
}

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace App\JsonRpc\Service;
use App\JsonRpc\Contract\CampusServiceInterface;
use App\Model\Campus;
use Hyperf\RpcServer\Annotation\RpcService;
use RuntimeException;
#[RpcService(
name: 'DataCampus',
server: 'jsonrpc-http',
protocol: 'jsonrpc-http',
publishTo: 'nacos'
)]
class CampusService implements CampusServiceInterface
{
public function getCampus(int $id): array
{
$campus = Campus::find($id);
if (! $campus) {
throw new RuntimeException("校区不存在: {$id}");
}
return $campus->toArray();
}
public function batchGetCampus(array $ids): array
{
if (empty($ids)) {
return [];
}
$campuses = Campus::whereIn('id', $ids)->get()->toArray();
return array_column($campuses, null, 'id');
}
public function getCampusByProvince(string $province): array
{
return Campus::province($province)
->level(3)
->get()
->toArray();
}
public function getCampusByCity(string $province, string $city): array
{
return Campus::province($province)
->city($city)
->level(3)
->get()
->toArray();
}
public function getCampusHierarchy(int $campusId): array
{
$hierarchy = [];
$current = Campus::find($campusId);
if (! $current) {
return $hierarchy;
}
// 从校区向上追溯层级
while ($current) {
array_unshift($hierarchy, [
'id' => $current->id,
'name' => $current->name,
'level' => $current->level,
'province' => $current->province,
'city' => $current->city,
]);
if ($current->parent_id === 0) {
break;
}
$current = Campus::find($current->parent_id);
}
return $hierarchy;
}
}

View File

@@ -1,66 +0,0 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Listener;
use Hyperf\Collection\Arr;
use Hyperf\Database\Events\QueryExecuted;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Logger\LoggerFactory;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
#[Listener]
class DbQueryExecutedListener implements ListenerInterface
{
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(ContainerInterface $container)
{
$this->logger = $container->get(LoggerFactory::class)->get('sql');
}
public function listen(): array
{
return [
QueryExecuted::class,
];
}
/**
* @param QueryExecuted $event
*/
public function process(object $event): void
{
if ($event instanceof QueryExecuted) {
$sql = $event->sql;
if (! Arr::isAssoc($event->bindings)) {
$position = 0;
foreach ($event->bindings as $value) {
$position = strpos($sql, '?', $position);
if ($position === false) {
break;
}
$value = "'{$value}'";
$sql = substr_replace($sql, $value, $position, 1);
$position += strlen($value);
}
}
$this->logger->info(sprintf('[%s] %s', $event->time, $sql));
}
}
}

View File

@@ -1,35 +0,0 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Listener;
use Hyperf\Command\Event\AfterExecute;
use Hyperf\Coordinator\Constants;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
#[Listener]
class ResumeExitCoordinatorListener implements ListenerInterface
{
public function listen(): array
{
return [
AfterExecute::class,
];
}
public function process(object $event): void
{
CoordinatorManager::until(Constants::WORKER_EXIT)->resume();
}
}

80
app/Model/Campus.php Normal file
View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Aether\AetherModel;
use Carbon\Carbon;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Relations\HasMany;
/**
* 校区模型(支持层级结构).
* @property int $id
* @property string $name 校区名称
* @property int $parent_id 父级ID
* @property int $level 层级1-省份2-地级市3-校区
* @property string $province 省份
* @property string $city 地级市
* @property string $address 详细地址
* @property string $contact_phone 联系电话
* @property string $contact_person 联系人
* @property int $status 状态0-禁用1-启用
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Carbon $deleted_at
*/
class Campus extends AetherModel
{
protected ?string $table = 'campus';
protected array $fillable = [
'name',
'parent_id',
'level',
'province',
'city',
'address',
'contact_phone',
'status',
];
protected array $casts = [
'parent_id' => 'integer',
'level' => 'integer',
'status' => 'integer',
];
/**
* 获取子校区.
*/
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id', 'id');
}
/**
* 按省份查询.
*/
public function scopeProvince(Builder $query, string $province): Builder
{
return $query->where('province', $province);
}
/**
* 按城市查询.
*/
public function scopeCity(Builder $query, string $city): Builder
{
return $query->where('city', $city);
}
/**
* 按层级查询.
*/
public function scopeLevel(Builder $query, int $level): Builder
{
return $query->where('level', $level);
}
}