update backend route group

This commit is contained in:
2026-03-23 16:24:17 +08:00
parent f5847b392c
commit b97fd3f59b
4 changed files with 424 additions and 1 deletions
@@ -9,6 +9,7 @@ use App\Middleware\AuthMiddleware;
use App\Middleware\PermissionMiddleware;
use App\Model\Route;
use App\Model\RouteGroup;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
@@ -552,4 +553,92 @@ class RouteGroupController extends AbstractController
'data' => $route,
];
}
/**
* 批量同步路由到路由组
*
* 替换式同步:先清空旧归属,再设置新归属,事务保证原子性
*
* @param int $id 路由组 ID
*/
#[OA\Put(
path: '/route-groups/{id}/routes',
summary: '批量同步路由到路由组',
description: '替换式同步:提交 route_ids 数组,组内路由将被完全替换为指定路由。提交空数组清空组内所有路由。若指定路由已属于其他组,将自动从原组移出。',
security: [['bearerAuth' => []]],
tags: ['Route Groups'],
parameters: [
new OA\Parameter(name: 'id', in: 'path', required: true, description: '路由组 ID', schema: new OA\Schema(type: 'integer')),
],
requestBody: new OA\RequestBody(
required: true,
content: new OA\JsonContent(
required: ['route_ids'],
properties: [
new OA\Property(property: 'route_ids', type: 'array', items: new OA\Items(type: 'integer'), description: '路由 ID 数组,空数组表示清空', example: [1, 2, 3]),
]
)
),
responses: [
new OA\Response(
response: 200,
description: '同步成功',
content: new OA\JsonContent(properties: [
new OA\Property(property: 'code', type: 'integer', example: 0),
new OA\Property(property: 'message', type: 'string', example: '同步成功'),
new OA\Property(property: 'data', properties: [
new OA\Property(property: 'id', type: 'integer', example: 1),
new OA\Property(property: 'name', type: 'string', example: 'user-management'),
new OA\Property(property: 'label', type: 'string', nullable: true, example: '用户管理'),
new OA\Property(property: 'routes_count', type: 'integer', example: 5),
], type: 'object'),
])
),
new OA\Response(response: 401, description: '未认证', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
new OA\Response(response: 404, description: '路由组不存在', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
new OA\Response(response: 422, description: '参数校验失败', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
]
)]
#[RequestMapping(path: "{id}/routes", methods: "PUT")]
#[Middleware(AuthMiddleware::class)]
#[Middleware(PermissionMiddleware::class)]
public function syncRoutes(int $id): ResponseInterface|array
{
$group = RouteGroup::query()->find($id);
if (!$group) {
return $this->response->json([
'code' => 404,
'message' => '路由组不存在',
])->withStatus(404);
}
$body = $this->request->getParsedBody();
if (!array_key_exists('route_ids', $body) || !is_array($body['route_ids'])) {
return $this->response->json([
'code' => 422,
'message' => 'route_ids 必须为数组',
])->withStatus(422);
}
$route_ids = array_values(array_map('intval', array_filter($body['route_ids'], 'is_numeric')));
Db::transaction(function () use ($id, $route_ids): void {
// 清空旧归属
Route::query()->where('group_id', $id)->update(['group_id' => null]);
// 设置新归属
if ($route_ids !== []) {
Route::query()->whereIn('id', $route_ids)->update(['group_id' => $id]);
}
});
$group->loadCount('routes');
return [
'code' => 0,
'message' => '同步成功',
'data' => $group,
];
}
}