refactor: split backend transport and domain modules

This commit is contained in:
2026-08-14 01:04:21 +08:00
parent acba9624d7
commit e6775f0966
59 changed files with 2733 additions and 1963 deletions
+129
View File
@@ -0,0 +1,129 @@
package handler
import (
"strconv"
"time"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
"github.com/gin-gonic/gin"
)
type Announcement struct{ service *service.AnnouncementService }
func NewAnnouncement(service *service.AnnouncementService) *Announcement {
return &Announcement{service: service}
}
func announcementInput(req dto.AnnouncementRequest) service.AnnouncementInput {
return service.AnnouncementInput{ID: req.ID, Title: req.Title, Content: req.Content, UserID: req.UserID, Attachments: req.Attachments}
}
func (h *Announcement) Create(c *gin.Context) {
var req dto.AnnouncementRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.Create(c.Request.Context(), announcementInput(req)); err != nil {
httpx.Fail(c, "创建失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "创建成功")
}
func (h *Announcement) Delete(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
if err := h.service.Delete(c.Request.Context(), uint(id)); err != nil {
httpx.Fail(c, "删除失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
}
func (h *Announcement) DeleteByIDs(c *gin.Context) {
values := c.QueryArray("IDs[]")
if len(values) == 0 {
values = c.QueryArray("IDs")
}
ids := make([]uint, 0, len(values))
for _, value := range values {
id, err := strconv.ParseUint(value, 10, 64)
if err != nil || id == 0 {
httpx.Fail(c, "参数错误")
return
}
ids = append(ids, uint(id))
}
if err := h.service.DeleteByIDs(c.Request.Context(), ids); err != nil {
httpx.Fail(c, "批量删除失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "批量删除成功")
}
func (h *Announcement) Update(c *gin.Context) {
var req dto.AnnouncementRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.Update(c.Request.Context(), announcementInput(req)); err != nil {
httpx.Fail(c, "更新失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "更新成功")
}
func (h *Announcement) Find(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
item, err := h.service.Find(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "查询失败:"+err.Error())
return
}
httpx.OKWithData(c, item)
}
func (h *Announcement) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
start, startErr := parseTime(c.Query("startCreatedAt"))
end, endErr := parseTime(c.Query("endCreatedAt"))
if startErr != nil || endErr != nil || (start == nil) != (end == nil) {
httpx.Fail(c, "创建日期范围不合法")
return
}
items, total, err := h.service.List(c.Request.Context(), page, pageSize, start, end)
if err != nil {
httpx.Fail(c, "获取失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: page, PageSize: pageSize}, "获取成功")
}
func (h *Announcement) DataSource(c *gin.Context) {
users, err := h.service.UserOptions(c.Request.Context())
if err != nil {
httpx.Fail(c, "查询失败:"+err.Error())
return
}
httpx.OKWithData(c, gin.H{"userID": users})
}
func (h *Announcement) Public(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, gin.H{"info": "不需要鉴权的公告接口信息"}, "获取成功")
}
func parseTime(value string) (*time.Time, error) {
if value == "" {
return nil, nil
}
parsed, err := time.Parse(time.RFC3339, value)
if err != nil {
return nil, err
}
return &parsed, nil
}
+201
View File
@@ -0,0 +1,201 @@
package handler
import (
"strconv"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
"github.com/gin-gonic/gin"
)
type API struct{ service *service.AccessService }
func NewAPI(service *service.AccessService) *API { return &API{service: service} }
func (h *API) List(c *gin.Context) {
var req dto.APIListRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
items, total, err := h.service.ListAPI(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
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())
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"apis": items}, "获取成功")
}
func (h *API) Create(c *gin.Context) {
var req dto.APIRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.CreateAPIRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "创建失败")
return
}
httpx.OK(c)
}
func (h *API) Update(c *gin.Context) {
var req dto.APIRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.UpdateAPIRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "更新失败")
return
}
httpx.OK(c)
}
func (h *API) Delete(c *gin.Context) {
var req dto.DeleteAPIRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.DeleteAPIs(c.Request.Context(), []uint{req.ID}); err != nil {
httpx.Fail(c, "删除失败")
return
}
httpx.OK(c)
}
func (h *API) DeleteByIDs(c *gin.Context) {
var req dto.DeleteAPIsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.DeleteAPIs(c.Request.Context(), req.IDs); err != nil {
httpx.Fail(c, "删除失败")
return
}
httpx.OK(c)
}
func (h *API) Find(c *gin.Context) {
var req dto.GetAPIRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
item, err := h.service.FindAPIResponse(c.Request.Context(), req.ID)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"api": item}, "获取成功")
}
func (h *API) Groups(c *gin.Context) {
items, err := h.service.AllAPI(c.Request.Context())
if err != nil {
httpx.Fail(c, "获取失败")
return
}
seen := map[string]bool{}
groups := []string{}
for _, item := range items {
if item.APIGroup != "" && !seen[item.APIGroup] {
seen[item.APIGroup] = true
groups = append(groups, item.APIGroup)
}
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"groups": groups}, "获取成功")
}
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))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
}
func (h *API) SetRoles(c *gin.Context) {
var req dto.SetAPIRolesRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetAPIRoles(c.Request.Context(), req.APIID, req.AuthorityIDs); err != nil {
httpx.Fail(c, "设置失败")
return
}
httpx.OK(c)
}
func (h *API) Sync(engine *gin.Engine) gin.HandlerFunc {
return func(c *gin.Context) {
routes := engine.Routes()
values := make([]dto.APIRequest, 0, len(routes))
for _, route := range routes {
values = append(values, dto.APIRequest{Path: route.Path, Method: route.Method})
}
result, err := h.service.SyncAPIResponses(c.Request.Context(), values)
if err != nil {
httpx.Fail(c, "同步检查失败")
return
}
httpx.Write(c, httpx.CodeSuccess, result, "获取成功")
}
}
func (h *API) Ignore(c *gin.Context) {
var req dto.IgnoreAPIRequest
if c.ShouldBindJSON(&req) != nil || req.Path == "" || req.Method == "" {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetAPIIgnored(c.Request.Context(), req.Path, req.Method, req.Flag); err != nil {
httpx.Fail(c, "忽略设置失败")
return
}
httpx.OK(c)
}
func (h *API) ApplySync(c *gin.Context) {
var req dto.ApplyAPISyncRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.ApplyAPISyncRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "同步失败")
return
}
httpx.OK(c)
}
func (h *API) FreshCasbin(c *gin.Context) { httpx.OK(c) }
func (h *API) SetPolicyPaths(c *gin.Context) {
var req dto.SetPolicyPathsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetPolicyPathsRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "更新失败")
return
}
httpx.OK(c)
}
func (h *API) PolicyPaths(c *gin.Context) {
var req dto.GetPolicyPathsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
paths, err := h.service.PolicyPathResponses(c.Request.Context(), req.AuthorityID)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"paths": paths}, "获取成功")
}
+117
View File
@@ -0,0 +1,117 @@
package handler
import (
"strconv"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
"github.com/gin-gonic/gin"
)
type Authority struct{ service *service.AccessService }
func NewAuthority(service *service.AccessService) *Authority { return &Authority{service: service} }
func (h *Authority) List(c *gin.Context) {
items, err := h.service.Authorities(c.Request.Context())
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, items, "获取成功")
}
func (h *Authority) Create(c *gin.Context) {
var req dto.AuthorityRequest
if c.ShouldBindJSON(&req) != nil || req.AuthorityID == 0 {
httpx.Fail(c, "参数错误")
return
}
value, err := h.service.CreateAuthorityRequest(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "创建失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"authority": value}, "创建成功")
}
func (h *Authority) Copy(c *gin.Context) {
var req dto.CopyAuthorityRequest
if c.ShouldBindJSON(&req) != nil || req.Authority.AuthorityID == 0 || req.OldAuthorityID == 0 {
httpx.Fail(c, "参数错误")
return
}
value, err := h.service.CopyAuthorityRequest(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "拷贝失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"authority": value}, "拷贝成功")
}
func (h *Authority) Update(c *gin.Context) {
var req dto.AuthorityRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
value, err := h.service.UpdateAuthorityRequest(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "更新失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"authority": value}, "更新成功")
}
func (h *Authority) Delete(c *gin.Context) {
var req dto.DeleteAuthorityRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.DeleteAuthority(c.Request.Context(), req.AuthorityID); err != nil {
httpx.Fail(c, err.Error())
return
}
httpx.OK(c)
}
func (h *Authority) SetUsers(c *gin.Context) {
var req dto.SetRoleUsersRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetAuthorityUsers(c.Request.Context(), req.AuthorityID, req.UserIDs); err != nil {
httpx.Fail(c, "设置失败")
return
}
httpx.OK(c)
}
func (h *Authority) Users(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("authorityId"), 10, 64)
ids, err := h.service.AuthorityUserIDs(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
}
func (h *Authority) SetDataScope(c *gin.Context) {
var req dto.SetDataScopeRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetDataScope(c.Request.Context(), req.AuthorityID, req.DataScope, req.DepartmentIDs); err != nil {
httpx.Fail(c, "设置失败")
return
}
httpx.OK(c)
}
func (h *Authority) DataScopeDepartments(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("authorityId"), 10, 64)
ids, err := h.service.DataScopeDepartmentIDs(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
}
+34
View File
@@ -0,0 +1,34 @@
package handler
import (
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
"github.com/gin-gonic/gin"
)
type Email struct{ service *service.EmailService }
func NewEmail(service *service.EmailService) *Email { return &Email{service: service} }
func (h *Email) Test(c *gin.Context) {
if err := h.service.Test(c.Request.Context()); err != nil {
httpx.Fail(c, "发送失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "发送成功")
}
func (h *Email) Send(c *gin.Context) {
var req dto.SendEmailRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.Send(c.Request.Context(), req.To, req.Subject, req.Body); err != nil {
httpx.Fail(c, "发送失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "发送成功")
}
+136
View File
@@ -0,0 +1,136 @@
package handler
import (
"strconv"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
"github.com/gin-gonic/gin"
)
type Menu struct{ service *service.MenuService }
func NewMenu(service *service.MenuService) *Menu { return &Menu{service: service} }
func (h *Menu) List(c *gin.Context) {
items, err := h.service.List(c.Request.Context())
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: int64(len(items)), Page: 1, PageSize: 999}, "获取成功")
}
func (h *Menu) Tree(c *gin.Context) {
items, err := h.service.Tree(c.Request.Context())
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"menus": items}, "获取成功")
}
func (h *Menu) Create(c *gin.Context) {
var req dto.MenuRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.Create(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "新增失败")
return
}
httpx.OK(c)
}
func (h *Menu) Update(c *gin.Context) {
var req dto.MenuRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.Update(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "更新失败")
return
}
httpx.OK(c)
}
func (h *Menu) Delete(c *gin.Context) {
var req dto.DeleteMenuRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.Delete(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, err.Error())
return
}
httpx.OK(c)
}
func (h *Menu) Find(c *gin.Context) {
var req dto.GetMenuRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
item, err := h.service.Find(c.Request.Context(), req.ID)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"menu": item}, "获取成功")
}
func (h *Menu) SetAuthorityMenus(c *gin.Context) {
var req dto.SetAuthorityMenusRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetAuthorityMenus(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "添加失败")
return
}
httpx.OK(c)
}
func (h *Menu) AuthorityMenus(c *gin.Context) {
var req dto.GetAuthorityMenusRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
menus, err := h.service.AuthorityMenus(c.Request.Context(), req.AuthorityID)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"menus": menus}, "获取成功")
}
func (h *Menu) RoleIDs(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("menuId"), 10, 64)
ids, err := h.service.RoleIDs(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
}
func (h *Menu) SetRoles(c *gin.Context) {
var req dto.SetMenuRolesRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetRoles(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "设置失败")
return
}
httpx.OK(c)
}
+198
View File
@@ -0,0 +1,198 @@
package handler
import (
"strconv"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
"github.com/gin-gonic/gin"
)
type Organization struct{ service *service.OrganizationService }
func NewOrganization(service *service.OrganizationService) *Organization {
return &Organization{service: service}
}
func (h *Organization) ListDepartments(c *gin.Context) {
var req dto.DepartmentListRequest
_ = c.ShouldBindJSON(&req)
items, err := h.service.Departments(c.Request.Context(), req.Name)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, items, "获取成功")
}
func (h *Organization) CreateDepartment(c *gin.Context) {
var req dto.DepartmentRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.CreateDepartment(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "创建失败")
return
}
httpx.OK(c)
}
func (h *Organization) UpdateDepartment(c *gin.Context) {
var req dto.DepartmentRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.UpdateDepartment(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "更新失败")
return
}
httpx.OK(c)
}
func (h *Organization) DeleteDepartment(c *gin.Context) {
var req dto.DeleteDepartmentRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.DeleteDepartment(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, err.Error())
return
}
httpx.OK(c)
}
func (h *Organization) FindDepartment(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
item, err := h.service.Department(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, item, "获取成功")
}
func (h *Organization) DepartmentUsers(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("deptId"), 10, 64)
ids, err := h.service.DepartmentUserIDs(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
}
func (h *Organization) SetDepartmentUsers(c *gin.Context) {
var req dto.SetDepartmentUsersRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetDepartmentUsers(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "设置失败")
return
}
httpx.OK(c)
}
func (h *Organization) SetUserDepartments(c *gin.Context) {
var req dto.SetUserDepartmentsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetUserDepartments(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "设置失败")
return
}
httpx.OK(c)
}
func (h *Organization) ListPositions(c *gin.Context) {
var req dto.PositionListRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
items, total, err := h.service.Positions(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
func (h *Organization) CreatePosition(c *gin.Context) {
var req dto.PositionRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.CreatePosition(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "创建失败")
return
}
httpx.OK(c)
}
func (h *Organization) UpdatePosition(c *gin.Context) {
var req dto.PositionRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.UpdatePosition(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "更新失败")
return
}
httpx.OK(c)
}
func (h *Organization) DeletePosition(c *gin.Context) {
var req dto.DeletePositionRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.DeletePosition(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, "删除失败")
return
}
httpx.OK(c)
}
func (h *Organization) FindPosition(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
item, err := h.service.Position(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, item, "获取成功")
}
func (h *Organization) PositionUsers(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("positionId"), 10, 64)
ids, err := h.service.PositionUserIDs(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
}
func (h *Organization) SetPositionUsers(c *gin.Context) {
var req dto.SetPositionUsersRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetPositionUsers(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "设置失败")
return
}
httpx.OK(c)
}
func (h *Organization) SetUserPositions(c *gin.Context) {
var req dto.SetUserPositionsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetUserPositions(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "设置失败")
return
}
httpx.OK(c)
}
+56
View File
@@ -0,0 +1,56 @@
package handler
import (
"strconv"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
"github.com/gin-gonic/gin"
)
type Permission struct{ service *service.AccessService }
func NewPermission(service *service.AccessService) *Permission { return &Permission{service: service} }
func (h *Permission) Buttons(c *gin.Context) {
var req dto.GetAuthorityButtonsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
selected, err := h.service.SelectedButtons(c.Request.Context(), req.AuthorityID, req.MenuID)
if err != nil {
httpx.Fail(c, "查询失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"selected": selected}, "查询成功")
}
func (h *Permission) SetButtons(c *gin.Context) {
var req dto.SetAuthorityButtonsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.SetSelectedButtons(c.Request.Context(), req.AuthorityID, req.MenuID, req.Selected); err != nil {
httpx.Fail(c, "分配失败")
return
}
httpx.OK(c)
}
func (h *Permission) CanRemoveButton(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
allowed, err := h.service.CanRemoveButton(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "检查失败")
return
}
if !allowed {
httpx.Fail(c, "此按钮正在被使用无法删除")
return
}
httpx.OK(c)
}
+168
View File
@@ -0,0 +1,168 @@
package handler
import (
"fmt"
"net/http"
"strconv"
"time"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
"kra/internal/worker"
"github.com/gin-gonic/gin"
)
type Task struct {
service *service.TaskService
scheduler *worker.TaskScheduler
}
func NewTask(service *service.TaskService, scheduler *worker.TaskScheduler) *Task {
return &Task{service: service, scheduler: scheduler}
}
func (h *Task) Create(c *gin.Context) {
var req dto.TaskRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
id, err := h.service.CreateRequest(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "创建失败:"+err.Error())
return
}
if err = h.scheduler.ScheduleID(c.Request.Context(), id); err != nil {
httpx.Fail(c, "调度失败:"+err.Error())
return
}
httpx.OK(c)
}
func (h *Task) Update(c *gin.Context) {
var req dto.TaskRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.UpdateRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "更新失败:"+err.Error())
return
}
if err := h.scheduler.ScheduleID(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, "调度失败:"+err.Error())
return
}
httpx.OK(c)
}
func (h *Task) Delete(c *gin.Context) {
var req dto.DeleteTaskRequest
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
httpx.Fail(c, "参数错误")
return
}
h.scheduler.Remove(req.ID)
if err := h.service.Delete(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, "删除失败")
return
}
httpx.OK(c)
}
func (h *Task) Toggle(c *gin.Context) {
var req dto.ToggleTaskRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.Toggle(c.Request.Context(), req.ID, req.Enabled); err != nil {
httpx.Fail(c, "设置失败")
return
}
if err := h.scheduler.ScheduleID(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, "调度失败")
return
}
httpx.OK(c)
}
func (h *Task) Trigger(c *gin.Context) {
var req dto.DeleteTaskRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.scheduler.TriggerID(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, "任务不存在")
return
}
httpx.OK(c)
}
func (h *Task) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
size, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
var enabled *bool
if raw := c.Query("enabled"); raw != "" {
value, _ := strconv.ParseBool(raw)
enabled = &value
}
items, total, err := h.service.ListRequest(c.Request.Context(), page, size, c.Query("name"), c.Query("executorType"), enabled, h.scheduler.NextRuns())
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
}
func (h *Task) Logs(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
size, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
taskID, _ := strconv.ParseUint(c.Query("taskId"), 10, 64)
items, total, err := h.service.Logs(c.Request.Context(), page, size, uint(taskID), c.Query("status"))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
}
func (h *Task) Methods(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, gin.H{"methods": h.service.RegisteredMethods()}, "获取成功")
}
func (h *Task) AlertStream(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
c.Header("X-Accel-Buffering", "no")
flusher, ok := c.Writer.(http.Flusher)
if !ok {
httpx.Fail(c, "SSE 不受支持")
return
}
events := h.scheduler.Subscribe()
defer h.scheduler.Unsubscribe(events)
ticker := time.NewTicker(20 * time.Second)
defer ticker.Stop()
_, _ = fmt.Fprint(c.Writer, "event: connected\ndata: {}\n\n")
flusher.Flush()
for {
select {
case event, open := <-events:
if !open {
return
}
_, _ = fmt.Fprintf(c.Writer, "event: alert\ndata: %s\n\n", event)
flusher.Flush()
case <-ticker.C:
_, _ = fmt.Fprint(c.Writer, ": keepalive\n\n")
flusher.Flush()
case <-c.Request.Context().Done():
return
}
}
}