fix: align admin behavior and Kratos integrations

This commit is contained in:
2026-08-14 14:14:51 +08:00
parent 8c4c6ba661
commit e0b0e1d8c8
123 changed files with 4334 additions and 1877 deletions
+27 -10
View File
@@ -1,7 +1,7 @@
package handler
import (
"strconv"
"strings"
"kra/internal/server/httpx"
"kra/internal/service"
@@ -28,7 +28,7 @@ func (h *API) List(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
func (h *API) All(c *gin.Context) {
items, err := h.service.AllAPI(c.Request.Context())
items, err := h.service.AllAPI(c.Request.Context(), true)
if err != nil {
httpx.Fail(c, "获取失败")
return
@@ -41,11 +41,12 @@ func (h *API) Create(c *gin.Context) {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.CreateAPIRequest(c.Request.Context(), &req); err != nil {
created, err := h.service.CreateAPIRequest(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "创建失败")
return
}
httpx.OK(c)
httpx.Write(c, httpx.CodeSuccess, created, "创建成功")
}
func (h *API) Update(c *gin.Context) {
var req dto.APIRequest
@@ -97,28 +98,40 @@ func (h *API) Find(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, gin.H{"api": item}, "获取成功")
}
func (h *API) Groups(c *gin.Context) {
items, err := h.service.AllAPI(c.Request.Context())
items, err := h.service.AllAPI(c.Request.Context(), false)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
seen := map[string]bool{}
groups := []string{}
groupAPIMap := map[string]string{}
for _, item := range items {
if item.APIGroup != "" && !seen[item.APIGroup] {
if !seen[item.APIGroup] {
seen[item.APIGroup] = true
groups = append(groups, item.APIGroup)
}
parts := strings.Split(item.Path, "/")
if len(parts) > 1 {
groupAPIMap[parts[1]] = item.APIGroup
}
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"groups": groups}, "获取成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{"groups": groups, "apiGroupMap": groupAPIMap}, "获取成功")
}
func (h *API) Roles(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("apiId"), 10, 64)
ids, err := h.service.APIRoleIDs(c.Request.Context(), uint(id))
path, method := c.Query("path"), c.Query("method")
if path == "" || method == "" {
httpx.Fail(c, "API路径和请求方法不能为空")
return
}
ids, err := h.service.APIRoleIDs(c.Request.Context(), path, method)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
if ids == nil {
ids = []uint{}
}
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
}
func (h *API) SetRoles(c *gin.Context) {
@@ -127,7 +140,11 @@ func (h *API) SetRoles(c *gin.Context) {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetAPIRoles(c.Request.Context(), req.APIID, req.AuthorityIDs); err != nil {
if req.Path == "" || req.Method == "" {
httpx.Fail(c, "API路径和请求方法不能为空")
return
}
if err := h.service.SetAPIRoles(c.Request.Context(), req.Path, req.Method, req.AuthorityIDs); err != nil {
httpx.Fail(c, "设置失败")
return
}