feat(permission): 为权限树接口添加状态查询参数和返回值
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m22s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m22s
- 新增 PermissionTreeRequest DTO 支持 status 查询参数 - PermissionTreeNode 返回值新增 status 字段 - Store 层 GetAll 方法支持状态过滤 - Handler 层使用 QueryParser 解析请求参数
This commit is contained in:
@@ -109,15 +109,12 @@ func (h *PermissionHandler) List(c *fiber.Ctx) error {
|
||||
// GetTree 获取权限树
|
||||
// GET /api/admin/permissions/tree
|
||||
func (h *PermissionHandler) GetTree(c *fiber.Ctx) error {
|
||||
var availableForRoleType *int
|
||||
if roleTypeStr := c.Query("available_for_role_type"); roleTypeStr != "" {
|
||||
roleType, err := strconv.Atoi(roleTypeStr)
|
||||
if err == nil && (roleType == 1 || roleType == 2) {
|
||||
availableForRoleType = &roleType
|
||||
}
|
||||
var req dto.PermissionTreeRequest
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
tree, err := h.service.GetTree(c.UserContext(), availableForRoleType)
|
||||
tree, err := h.service.GetTree(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -67,6 +67,12 @@ type PermissionPageResult struct {
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
|
||||
// PermissionTreeRequest 权限树查询请求
|
||||
type PermissionTreeRequest struct {
|
||||
AvailableForRoleType *int `json:"available_for_role_type" query:"available_for_role_type" validate:"omitempty,min=1,max=2" minimum:"1" maximum:"2" description:"可用角色类型 (1:平台角色, 2:客户角色)"`
|
||||
Status *int `json:"status" query:"status" validate:"omitempty,min=0,max=1" minimum:"0" maximum:"1" description:"状态 (0:禁用, 1:启用)"`
|
||||
}
|
||||
|
||||
// PermissionTreeNode 权限树节点(用于层级展示)
|
||||
type PermissionTreeNode struct {
|
||||
ID uint `json:"id" description:"权限ID"`
|
||||
@@ -77,5 +83,6 @@ type PermissionTreeNode struct {
|
||||
AvailableForRoleTypes string `json:"available_for_role_types" description:"可用角色类型 (1:平台角色, 2:客户角色)"`
|
||||
URL string `json:"url,omitempty" description:"请求路径"`
|
||||
Sort int `json:"sort" description:"排序值"`
|
||||
Status int `json:"status" description:"状态 (0:禁用, 1:启用)"`
|
||||
Children []*PermissionTreeNode `json:"children,omitempty" description:"子权限列表"`
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func registerPermissionRoutes(api fiber.Router, h *admin.PermissionHandler, doc
|
||||
Register(permissions, doc, groupPath, "GET", "/tree", h.GetTree, RouteSpec{
|
||||
Summary: "获取权限树",
|
||||
Tags: []string{"权限"},
|
||||
Input: nil, // 无参数或 Query 参数
|
||||
Input: new(dto.PermissionTreeRequest),
|
||||
Output: new([]*dto.PermissionTreeNode),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
@@ -301,7 +301,7 @@ func (s *Service) getUserPermissionsAndMenus(ctx context.Context, userID uint, u
|
||||
}
|
||||
|
||||
func (s *Service) getAllPermissionsForSuperAdmin(ctx context.Context, device string) ([]string, []dto.MenuNode, []string, error) {
|
||||
permissions, err := s.permissionStore.GetAll(ctx, nil)
|
||||
permissions, err := s.permissionStore.GetAll(ctx, nil, nil)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(errors.CodeInternalError, err, "查询所有权限失败")
|
||||
}
|
||||
|
||||
@@ -230,8 +230,8 @@ func (s *Service) List(ctx context.Context, req *dto.PermissionListRequest) ([]*
|
||||
}
|
||||
|
||||
// GetTree 获取权限树
|
||||
func (s *Service) GetTree(ctx context.Context, availableForRoleType *int) ([]*dto.PermissionTreeNode, error) {
|
||||
permissions, err := s.permissionStore.GetAll(ctx, availableForRoleType)
|
||||
func (s *Service) GetTree(ctx context.Context, req *dto.PermissionTreeRequest) ([]*dto.PermissionTreeNode, error) {
|
||||
permissions, err := s.permissionStore.GetAll(ctx, req.AvailableForRoleType, req.Status)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "获取权限列表失败")
|
||||
}
|
||||
@@ -252,6 +252,7 @@ func buildPermissionTree(permissions []*model.Permission) []*dto.PermissionTreeN
|
||||
AvailableForRoleTypes: p.AvailableForRoleTypes,
|
||||
URL: p.URL,
|
||||
Sort: p.Sort,
|
||||
Status: p.Status,
|
||||
Children: make([]*dto.PermissionTreeNode, 0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ func (s *PermissionStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Pe
|
||||
}
|
||||
|
||||
// GetAll 获取所有权限(用于构建权限树)
|
||||
func (s *PermissionStore) GetAll(ctx context.Context, availableForRoleType *int) ([]*model.Permission, error) {
|
||||
func (s *PermissionStore) GetAll(ctx context.Context, availableForRoleType *int, status *int) ([]*model.Permission, error) {
|
||||
var permissions []*model.Permission
|
||||
query := s.db.WithContext(ctx)
|
||||
|
||||
@@ -130,6 +130,10 @@ func (s *PermissionStore) GetAll(ctx context.Context, availableForRoleType *int)
|
||||
query = query.Where("available_for_role_types LIKE ?", "%"+roleTypeStr+"%")
|
||||
}
|
||||
|
||||
if status != nil {
|
||||
query = query.Where("status = ?", *status)
|
||||
}
|
||||
|
||||
if err := query.Order("sort ASC, id ASC").Find(&permissions).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user