This commit is contained in:
2026-08-16 01:19:10 +08:00
parent c4b114762b
commit c554152f4f
43 changed files with 1311 additions and 318 deletions
+2 -2
View File
@@ -242,10 +242,10 @@ func (h *Dictionary) Path(c *gin.Context) {
httpx.Fail(c, "字典详情ID格式错误")
return
}
item, err := h.service.DictionaryDetail(c.Request.Context(), uint(id))
path, err := h.service.DictionaryPath(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"path": item.Path}, "获取成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{"path": path}, "获取成功")
}
-5
View File
@@ -30,11 +30,6 @@ func exportParams(values url.Values) map[string]string {
out[key] = items[0]
}
}
for key, items := range values {
if key != "params" && len(items) > 0 {
out[key] = items[0]
}
}
return out
}
+49 -27
View File
@@ -1,8 +1,6 @@
package handler
import (
"strconv"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
@@ -24,7 +22,7 @@ func (h *Organization) ListDepartments(c *gin.Context) {
_ = c.ShouldBindJSON(&req)
items, err := h.departments.Departments(c.Request.Context(), req.Name)
if err != nil {
httpx.Fail(c, "获取失败")
httpx.Fail(c, "获取失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, items, "获取成功")
@@ -63,19 +61,23 @@ func (h *Organization) UpdateDepartment(c *gin.Context) {
}
func (h *Organization) DeleteDepartment(c *gin.Context) {
var req dto.DeleteDepartmentRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.departments.Delete(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, err.Error())
httpx.Fail(c, "删除失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, nil, "删除成功")
}
func (h *Organization) FindDepartment(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
item, err := h.departments.Department(c.Request.Context(), uint(id))
var req dto.DeleteDepartmentRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
item, err := h.departments.Department(c.Request.Context(), req.ID)
if err != nil {
httpx.Fail(c, "获取失败:"+err.Error())
return
@@ -83,8 +85,12 @@ func (h *Organization) FindDepartment(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, item, "获取成功")
}
func (h *Organization) DepartmentUsers(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("deptId"), 10, 64)
ids, err := h.departments.UserIDs(c.Request.Context(), uint(id))
var req dto.SetDepartmentUsersRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
ids, err := h.departments.UserIDs(c.Request.Context(), req.DepartmentID)
if err != nil {
httpx.Fail(c, "获取失败:"+err.Error())
return
@@ -96,8 +102,12 @@ func (h *Organization) DepartmentUsers(c *gin.Context) {
}
func (h *Organization) SetDepartmentUsers(c *gin.Context) {
var req dto.SetDepartmentUsersRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.DepartmentID == 0 {
httpx.Fail(c, "部门ID不能为空")
return
}
if err := h.departments.SetUsers(c.Request.Context(), &req); err != nil {
@@ -108,8 +118,8 @@ func (h *Organization) SetDepartmentUsers(c *gin.Context) {
}
func (h *Organization) SetUserDepartments(c *gin.Context) {
var req dto.SetUserDepartmentsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.departments.SetUserDepartments(c.Request.Context(), &req); err != nil {
@@ -121,13 +131,13 @@ func (h *Organization) SetUserDepartments(c *gin.Context) {
func (h *Organization) ListPositions(c *gin.Context) {
var req dto.PositionListRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
items, total, err := h.positions.Positions(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "获取失败")
httpx.Fail(c, "获取失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
@@ -166,8 +176,8 @@ func (h *Organization) UpdatePosition(c *gin.Context) {
}
func (h *Organization) DeletePosition(c *gin.Context) {
var req dto.DeletePositionRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.positions.Delete(c.Request.Context(), req.ID); err != nil {
@@ -177,8 +187,12 @@ func (h *Organization) DeletePosition(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, nil, "删除成功")
}
func (h *Organization) FindPosition(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
item, err := h.positions.Position(c.Request.Context(), uint(id))
var req dto.DeletePositionRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
item, err := h.positions.Position(c.Request.Context(), req.ID)
if err != nil {
httpx.Fail(c, "获取失败:"+err.Error())
return
@@ -186,8 +200,12 @@ func (h *Organization) FindPosition(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, item, "获取成功")
}
func (h *Organization) PositionUsers(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("positionId"), 10, 64)
ids, err := h.positions.UserIDs(c.Request.Context(), uint(id))
var req dto.SetPositionUsersRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
ids, err := h.positions.UserIDs(c.Request.Context(), req.PositionID)
if err != nil {
httpx.Fail(c, "获取失败:"+err.Error())
return
@@ -199,8 +217,12 @@ func (h *Organization) PositionUsers(c *gin.Context) {
}
func (h *Organization) SetPositionUsers(c *gin.Context) {
var req dto.SetPositionUsersRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.PositionID == 0 {
httpx.Fail(c, "岗位ID不能为空")
return
}
if err := h.positions.SetUsers(c.Request.Context(), &req); err != nil {
@@ -211,8 +233,8 @@ func (h *Organization) SetPositionUsers(c *gin.Context) {
}
func (h *Organization) SetUserPositions(c *gin.Context) {
var req dto.SetUserPositionsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.positions.SetUserPositions(c.Request.Context(), &req); err != nil {
+1 -5
View File
@@ -123,11 +123,7 @@ func (h *Public) InitializeDatabase(engine *gin.Engine) gin.HandlerFunc {
values = append(values, dto.Route{Path: route.Path, Method: route.Method})
}
if err := h.system.InitializeRoutes(c.Request.Context(), &input, values); err != nil {
if errors.Is(err, biz.ErrTaskRuntimeReload) {
httpx.Fail(c, "数据库已初始化,但定时任务加载失败:"+err.Error())
return
}
httpx.Fail(c, "自动创建数据库失败: "+err.Error())
httpx.Fail(c, "自动创建数据库失败,请查看后台日志,检查后在进行初始化")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "自动创建数据库成功")
-7
View File
@@ -1,9 +1,6 @@
package handler
import (
"errors"
"kra/internal/biz"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
@@ -67,10 +64,6 @@ func (h *SystemConfig) Set(c *gin.Context) {
func (h *SystemConfig) Reload(c *gin.Context) {
if err := h.system.ReloadConfig(c.Request.Context()); err != nil {
if errors.Is(err, biz.ErrTaskRuntimeReload) {
httpx.Fail(c, "系统配置已重载,但定时任务重载失败:"+err.Error())
return
}
httpx.Fail(c, "重载系统失败:"+err.Error())
return
}
+15 -23
View File
@@ -1,14 +1,13 @@
package handler
import (
"errors"
"fmt"
"net/http"
"strconv"
"time"
"kra/internal/biz"
"kra/internal/server/httpx"
"kra/internal/server/middleware"
"kra/internal/service"
"kra/internal/service/dto"
@@ -31,13 +30,7 @@ func (h *Task) Create(c *gin.Context) {
}
_, err := h.service.CreateRequest(c.Request.Context(), &req)
if err != nil {
if errors.Is(err, biz.ErrTaskSchedule) {
var scheduleErr *biz.TaskScheduleError
errors.As(err, &scheduleErr)
httpx.Fail(c, "调度失败:"+scheduleErr.Error())
return
}
httpx.Fail(c, "创建失败:"+err.Error())
httpx.Fail(c, "创建失败: "+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "创建成功")
@@ -50,13 +43,7 @@ func (h *Task) Update(c *gin.Context) {
return
}
if err := h.service.UpdateRequest(c.Request.Context(), &req); err != nil {
if errors.Is(err, biz.ErrTaskSchedule) {
var scheduleErr *biz.TaskScheduleError
errors.As(err, &scheduleErr)
httpx.Fail(c, "调度失败:"+scheduleErr.Error())
return
}
httpx.Fail(c, "更新失败:"+err.Error())
httpx.Fail(c, "更新失败: "+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "更新成功")
@@ -134,8 +121,13 @@ func (h *Task) Methods(c *gin.Context) {
}
func (h *Task) AlertStream(c *gin.Context) {
claims := middleware.Claims(c)
if claims == nil || claims.ID == 0 {
httpx.Fail(c, "未获取到用户身份")
return
}
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Cache-Control", "no-cache, no-transform")
c.Header("Connection", "keep-alive")
c.Header("X-Accel-Buffering", "no")
flusher, ok := c.Writer.(http.Flusher)
@@ -145,11 +137,11 @@ func (h *Task) AlertStream(c *gin.Context) {
}
// Remove the normal server deadline for long-lived SSE connections.
_ = http.NewResponseController(c.Writer).SetWriteDeadline(time.Time{})
events := h.service.Subscribe()
defer h.service.Unsubscribe(events)
ticker := time.NewTicker(20 * time.Second)
events := h.service.Subscribe(claims.ID)
defer h.service.Unsubscribe(claims.ID, events)
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
_, _ = fmt.Fprint(c.Writer, "event: connected\ndata: {}\n\n")
_, _ = fmt.Fprint(c.Writer, ": connected\n\n")
flusher.Flush()
for {
select {
@@ -157,10 +149,10 @@ func (h *Task) AlertStream(c *gin.Context) {
if !open {
return
}
_, _ = fmt.Fprintf(c.Writer, "event: alert\ndata: %s\n\n", event)
_, _ = fmt.Fprintf(c.Writer, "event: timedTask:alert\ndata: %s\n\n", event)
flusher.Flush()
case <-ticker.C:
_, _ = fmt.Fprint(c.Writer, ": keepalive\n\n")
_, _ = fmt.Fprint(c.Writer, ": ping\n\n")
flusher.Flush()
case <-c.Request.Context().Done():
return