This commit is contained in:
2026-08-16 09:02:39 +08:00
parent c554152f4f
commit f7f6db8695
105 changed files with 2535 additions and 948 deletions
+21 -12
View File
@@ -35,8 +35,16 @@ func (h *Announcement) Create(c *gin.Context) {
}
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 {
id, present, err := queryUintValue(c, "ID")
if err != nil {
httpx.Fail(c, err.Error())
return
}
if !present {
// Preserve the legacy endpoint behavior for an omitted ID.
id = 0
}
if err := h.service.Delete(c.Request.Context(), id); err != nil {
httpx.Fail(c, "删除失败")
return
}
@@ -78,8 +86,12 @@ func (h *Announcement) Update(c *gin.Context) {
}
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))
id, _, err := queryUintValue(c, "ID")
if err != nil {
httpx.Fail(c, err.Error())
return
}
item, err := h.service.Find(c.Request.Context(), id)
if err != nil {
httpx.Fail(c, "查询失败")
return
@@ -88,20 +100,17 @@ func (h *Announcement) Find(c *gin.Context) {
}
func (h *Announcement) List(c *gin.Context) {
page, _ := strconv.Atoi(c.Query("page"))
pageSize, _ := strconv.Atoi(c.Query("pageSize"))
start, startErr := parseTime(c.Query("startCreatedAt"))
end, endErr := parseTime(c.Query("endCreatedAt"))
if startErr != nil || endErr != nil || (start == nil) != (end == nil) {
httpx.Fail(c, "创建日期范围不合法")
var req dto.AnnouncementSearchRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
items, total, err := h.service.List(c.Request.Context(), page, pageSize, start, end)
items, total, err := h.service.List(c.Request.Context(), req.Page, req.PageSize, req.StartCreatedAt, req.EndCreatedAt)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: page, PageSize: pageSize}, "获取成功")
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
func (h *Announcement) DataSource(c *gin.Context) {
+52 -6
View File
@@ -18,6 +18,14 @@ func (h *API) List(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if req.Page == 0 {
httpx.Fail(c, "Page值不能为空")
return
}
if req.PageSize == 0 {
httpx.Fail(c, "PageSize值不能为空")
return
}
items, total, err := h.service.ListAPI(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "获取失败")
@@ -39,6 +47,10 @@ func (h *API) Create(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if message := validateAPIRequest(&req); message != "" {
httpx.Fail(c, message)
return
}
created, err := h.service.CreateAPIRequest(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "创建失败")
@@ -52,6 +64,10 @@ func (h *API) Update(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if message := validateAPIRequest(&req); message != "" {
httpx.Fail(c, message)
return
}
if err := h.service.UpdateAPIRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "修改失败")
return
@@ -64,7 +80,11 @@ func (h *API) Delete(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if err := h.service.DeleteAPIs(c.Request.Context(), []uint{req.ID}); err != nil {
if req.ID == 0 {
httpx.Fail(c, "ID值不能为空")
return
}
if err := h.service.DeleteAPI(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, "删除失败")
return
}
@@ -88,6 +108,10 @@ func (h *API) Find(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if req.ID == 0 {
httpx.Fail(c, "ID值不能为空")
return
}
item, err := h.service.FindAPIResponse(c.Request.Context(), req.ID)
if err != nil {
httpx.Fail(c, "获取失败")
@@ -156,10 +180,6 @@ func (h *API) Ignore(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if 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
@@ -173,12 +193,15 @@ func (h *API) ApplySync(c *gin.Context) {
return
}
if err := h.service.ApplyAPISyncRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "同步失败")
httpx.Fail(c, "忽略失败")
return
}
httpx.OK(c)
}
func (h *API) FreshCasbin(c *gin.Context) {
// Policies are read from casbin_rule on every authorization decision, so
// there is no in-memory enforcer cache to reload. Keep the compatible
// endpoint and success response.
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "刷新成功")
}
@@ -188,6 +211,10 @@ func (h *API) SetPolicyPaths(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
if err := h.service.SetPolicyPathsRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "更新失败")
return
@@ -200,6 +227,10 @@ func (h *API) PolicyPaths(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
paths, err := h.service.PolicyPathResponses(c.Request.Context(), req.AuthorityID)
if err != nil {
httpx.Fail(c, "获取失败")
@@ -207,3 +238,18 @@ func (h *API) PolicyPaths(c *gin.Context) {
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"paths": paths}, "获取成功")
}
func validateAPIRequest(req *dto.APIRequest) string {
switch {
case req.Path == "":
return "Path值不能为空"
case req.Description == "":
return "Description值不能为空"
case req.APIGroup == "":
return "ApiGroup值不能为空"
case req.Method == "":
return "Method值不能为空"
default:
return ""
}
}
+128 -37
View File
@@ -2,6 +2,7 @@ package handler
import (
"errors"
"io"
"strconv"
"time"
@@ -28,6 +29,15 @@ func page(c *gin.Context) (int, int) {
size, _ := strconv.Atoi(c.Query("pageSize"))
return value, size
}
func queryUintValue(c *gin.Context, key string) (uint, bool, error) {
raw, ok := c.GetQuery(key)
if !ok || raw == "" {
return 0, false, nil
}
value, err := strconv.ParseUint(raw, 10, 0)
return uint(value), true, err
}
func IDsFromQuery(c *gin.Context) []uint {
values := c.QueryArray("IDs[]")
if len(values) == 0 {
@@ -43,19 +53,57 @@ func IDsFromQuery(c *gin.Context) []uint {
return ids
}
// auditID accepts both forms used by the GVA web client over time: DELETE
// requests may carry the identifier in the query string or in a JSON body.
func auditID(c *gin.Context) (uint, error) {
if raw := c.Query("ID"); raw != "" {
value, err := strconv.ParseUint(raw, 10, 64)
return uint(value), err
}
var request dto.IDRequest
if err := c.ShouldBindJSON(&request); err != nil && !errors.Is(err, io.EOF) {
return 0, err
}
return request.ID, nil
}
// auditIDs mirrors auditID for bulk deletes and supports both query-array and
// JSON-body encodings used by compatible GVA pages.
func auditIDs(c *gin.Context) ([]uint, error) {
if ids := IDsFromQuery(c); len(ids) > 0 {
return ids, nil
}
var request dto.IDsRequest
if err := c.ShouldBindJSON(&request); err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
return request.IDs, nil
}
func (h *Audit) Operations(c *gin.Context) {
p, size := page(c)
status, _ := strconv.Atoi(c.Query("status"))
items, total, err := h.service.OperationsFilter(c.Request.Context(), p, size, c.Query("path"), c.Query("method"), status)
var req dto.OperationRecordSearchRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
items, total, err := h.service.OperationsFilter(c.Request.Context(), req.Page, req.PageSize, req.Path, req.Method, req.Status)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: p, PageSize: size}, "获取成功")
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
func (h *Audit) Operation(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
item, err := h.service.Operation(c.Request.Context(), uint(id))
var req dto.AuditIDQuery
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.ID == 0 {
httpx.Fail(c, "ID值不能为空")
return
}
item, err := h.service.Operation(c.Request.Context(), req.ID)
if err != nil {
httpx.Fail(c, "查询失败")
return
@@ -63,24 +111,28 @@ func (h *Audit) Operation(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, gin.H{"reSysOperationRecord": item}, "查询成功")
}
func (h *Audit) DeleteOperation(c *gin.Context) {
var req dto.IDRequest
if err := c.ShouldBindJSON(&req); err != nil {
id, err := auditID(c)
if err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.DeleteOperations(c.Request.Context(), []uint{req.ID}); err != nil {
if id == 0 {
httpx.Fail(c, "删除失败")
return
}
if err := h.service.DeleteOperations(c.Request.Context(), []uint{id}); err != nil {
httpx.Fail(c, "删除失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
}
func (h *Audit) DeleteOperations(c *gin.Context) {
var req dto.IDsRequest
if err := c.ShouldBindJSON(&req); err != nil {
ids, err := auditIDs(c)
if err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.DeleteOperations(c.Request.Context(), req.IDs); err != nil {
if err := h.service.DeleteOperations(c.Request.Context(), ids); err != nil {
httpx.Fail(c, "批量删除失败")
return
}
@@ -88,18 +140,25 @@ func (h *Audit) DeleteOperations(c *gin.Context) {
}
func (h *Audit) Logins(c *gin.Context) {
p, size := page(c)
status, _ := strconv.ParseBool(c.Query("status"))
items, total, err := h.service.LoginsFilter(c.Request.Context(), p, size, c.Query("username"), status)
var req dto.LoginLogSearchRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
items, total, err := h.service.LoginsFilter(c.Request.Context(), req.Page, req.PageSize, req.Username, req.Status)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: p, PageSize: size}, "获取成功")
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
func (h *Audit) Login(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
item, err := h.service.Login(c.Request.Context(), uint(id))
var req dto.AuditIDQuery
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
item, err := h.service.Login(c.Request.Context(), req.ID)
if err != nil {
httpx.Fail(c, "查询失败")
return
@@ -107,24 +166,28 @@ func (h *Audit) Login(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, item, "查询成功")
}
func (h *Audit) DeleteLogin(c *gin.Context) {
var req dto.IDRequest
if err := c.ShouldBindJSON(&req); err != nil {
id, err := auditID(c)
if err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.DeleteLogins(c.Request.Context(), []uint{req.ID}); err != nil {
if id == 0 {
httpx.Fail(c, "删除失败")
return
}
if err := h.service.DeleteLogins(c.Request.Context(), []uint{id}); err != nil {
httpx.Fail(c, "删除失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
}
func (h *Audit) DeleteLogins(c *gin.Context) {
var req dto.IDsRequest
if err := c.ShouldBindJSON(&req); err != nil {
ids, err := auditIDs(c)
if err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.DeleteLogins(c.Request.Context(), req.IDs); err != nil {
if err := h.service.DeleteLogins(c.Request.Context(), ids); err != nil {
httpx.Fail(c, "批量删除失败")
return
}
@@ -133,8 +196,8 @@ func (h *Audit) DeleteLogins(c *gin.Context) {
func (h *Audit) DataAccess(c *gin.Context) {
var req dto.DataAccessListRequest
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.service.DataAccessRequest(c.Request.Context(), &req)
@@ -146,8 +209,8 @@ func (h *Audit) DataAccess(c *gin.Context) {
}
func (h *Audit) DeleteDataAccess(c *gin.Context) {
var req dto.IDsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.DeleteDataAccess(c.Request.Context(), req.IDs); err != nil {
@@ -158,7 +221,14 @@ func (h *Audit) DeleteDataAccess(c *gin.Context) {
}
func (h *Audit) LogDates(c *gin.Context) {
data, err := h.logs.LogDates(c.Request.Context(), c.Query("month"))
month := c.Query("month")
// Month uses `binding:"required"`; an omitted or empty query is a
// generic parameter error, not the service's format-specific error.
if month == "" {
httpx.Fail(c, "参数错误")
return
}
data, err := h.logs.LogDates(c.Request.Context(), month)
if err != nil {
failLogViewer(c, err)
return
@@ -166,7 +236,12 @@ func (h *Audit) LogDates(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, data, "获取成功")
}
func (h *Audit) LogFiles(c *gin.Context) {
data, err := h.logs.LogFiles(c.Request.Context(), c.Query("date"))
date := c.Query("date")
if date == "" {
httpx.Fail(c, "参数错误")
return
}
data, err := h.logs.LogFiles(c.Request.Context(), date)
if err != nil {
failLogViewer(c, err)
return
@@ -174,16 +249,23 @@ func (h *Audit) LogFiles(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, data, "获取成功")
}
func (h *Audit) LogContent(c *gin.Context) {
date, path := c.Query("date"), c.Query("path")
if date == "" || path == "" {
httpx.Fail(c, "参数错误")
return
}
var cursor *int64
if raw := c.Query("cursor"); raw != "" {
value, err := strconv.ParseInt(raw, 10, 64)
if err != nil || value < 0 {
if err != nil {
httpx.Fail(c, "参数错误")
return
}
cursor = &value
}
data, err := h.logs.LogContent(c.Request.Context(), c.Query("date"), c.Query("path"), cursor)
// Preserve negative cursors for the usecase to classify with the compatible
// "日志文件路径不合法" error rather than rejecting them as binding errors.
data, err := h.logs.LogContent(c.Request.Context(), date, path, cursor)
if err != nil {
failLogViewer(c, err)
return
@@ -211,15 +293,24 @@ func failLogViewer(c *gin.Context, err error) {
}
func (h *Audit) DeleteError(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
if err := h.service.DeleteErrors(c.Request.Context(), []uint{uint(id)}); err != nil {
id, err := auditID(c)
if err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.DeleteErrors(c.Request.Context(), []uint{id}); err != nil {
httpx.Fail(c, "删除失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
}
func (h *Audit) DeleteErrors(c *gin.Context) {
if err := h.service.DeleteErrors(c.Request.Context(), IDsFromQuery(c)); err != nil {
ids, err := auditIDs(c)
if err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.DeleteErrors(c.Request.Context(), ids); err != nil {
httpx.Fail(c, "批量删除失败:"+err.Error())
return
}
@@ -227,8 +318,8 @@ func (h *Audit) DeleteErrors(c *gin.Context) {
}
func (h *Audit) UpdateError(c *gin.Context) {
var req dto.ErrorRecordRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.UpdateErrorRequest(c.Request.Context(), &req); err != nil {
+65 -19
View File
@@ -1,8 +1,6 @@
package handler
import (
"strconv"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
@@ -17,15 +15,23 @@ func NewAuthority(service *service.AuthorityService) *Authority { return &Author
func (h *Authority) List(c *gin.Context) {
items, err := h.service.Authorities(c.Request.Context())
if err != nil {
httpx.Fail(c, "获取失败")
httpx.Fail(c, "获取失败"+err.Error())
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, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
if req.AuthorityName == "" {
httpx.Fail(c, "AuthorityName值不能为空")
return
}
value, err := h.service.CreateAuthorityRequest(c.Request.Context(), &req)
@@ -37,8 +43,20 @@ func (h *Authority) Create(c *gin.Context) {
}
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, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.OldAuthorityID == 0 {
httpx.Fail(c, "OldAuthorityId值不能为空")
return
}
if req.Authority.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
if req.Authority.AuthorityName == "" {
httpx.Fail(c, "AuthorityName值不能为空")
return
}
value, err := h.service.CopyAuthorityRequest(c.Request.Context(), &req)
@@ -50,8 +68,16 @@ func (h *Authority) Copy(c *gin.Context) {
}
func (h *Authority) Update(c *gin.Context) {
var req dto.AuthorityRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
if req.AuthorityName == "" {
httpx.Fail(c, "AuthorityName值不能为空")
return
}
value, err := h.service.UpdateAuthorityRequest(c.Request.Context(), &req)
@@ -63,8 +89,12 @@ func (h *Authority) Update(c *gin.Context) {
}
func (h *Authority) Delete(c *gin.Context) {
var req dto.DeleteAuthorityRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
if err := h.service.DeleteAuthority(c.Request.Context(), req.AuthorityID); err != nil {
@@ -75,8 +105,12 @@ func (h *Authority) Delete(c *gin.Context) {
}
func (h *Authority) SetUsers(c *gin.Context) {
var req dto.SetRoleUsersRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "角色ID不能为空")
return
}
if err := h.service.SetAuthorityUsers(c.Request.Context(), req.AuthorityID, req.UserIDs); err != nil {
@@ -86,8 +120,12 @@ func (h *Authority) SetUsers(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
}
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))
var req dto.SetRoleUsersRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
ids, err := h.service.AuthorityUserIDs(c.Request.Context(), req.AuthorityID)
if err != nil {
httpx.Fail(c, "获取失败"+err.Error())
return
@@ -99,8 +137,12 @@ func (h *Authority) Users(c *gin.Context) {
}
func (h *Authority) SetDataScope(c *gin.Context) {
var req dto.SetDataScopeRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "角色ID不能为空")
return
}
if err := h.service.SetDataScope(c.Request.Context(), req.AuthorityID, req.DataScope, req.DepartmentIDs); err != nil {
@@ -110,8 +152,12 @@ func (h *Authority) SetDataScope(c *gin.Context) {
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
}
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))
var req dto.SetDataScopeRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
ids, err := h.service.DataScopeDepartmentIDs(c.Request.Context(), req.AuthorityID)
if err != nil {
httpx.Fail(c, "获取失败"+err.Error())
return
+27 -17
View File
@@ -25,7 +25,9 @@ func (h *Media) Upload(c *gin.Context) {
}
opened, err := header.Open()
if err != nil {
httpx.Fail(c, "读取文件失败")
// Let the upload service surface this as the generic upload
// failure; keep the endpoint message consistent.
httpx.Fail(c, "上传文件失败")
return
}
defer opened.Close()
@@ -41,8 +43,8 @@ func (h *Media) Upload(c *gin.Context) {
func (h *Media) List(c *gin.Context) {
var req dto.MediaListRequest
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.service.MediaList(c.Request.Context(), biz.MediaFilter{Page: req.Page, PageSize: req.PageSize, Keyword: req.Keyword, CategoryID: req.ClassID, Tag: req.Tag, UserID: req.UserID, StartCreatedAt: req.StartCreatedAt, EndCreatedAt: req.EndCreatedAt, OrderKey: req.OrderKey, Desc: req.Desc})
@@ -54,8 +56,8 @@ func (h *Media) List(c *gin.Context) {
}
func (h *Media) Delete(c *gin.Context) {
var req dto.IDRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.Delete(c.Request.Context(), req.ID); err != nil {
@@ -66,8 +68,8 @@ func (h *Media) Delete(c *gin.Context) {
}
func (h *Media) DeleteMany(c *gin.Context) {
var req dto.IDsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
failed := []uint{}
@@ -97,8 +99,8 @@ func (h *Media) Find(c *gin.Context) {
}
func (h *Media) Rename(c *gin.Context) {
var req dto.RenameMediaRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.Rename(c.Request.Context(), req.ID, req.Name); err != nil {
@@ -109,8 +111,8 @@ func (h *Media) Rename(c *gin.Context) {
}
func (h *Media) ImportURLs(c *gin.Context) {
var req []dto.ImportMediaRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.ImportURLRequests(c.Request.Context(), req); err != nil {
@@ -121,8 +123,8 @@ func (h *Media) ImportURLs(c *gin.Context) {
}
func (h *Media) Storage(c *gin.Context) {
var req dto.StorageListRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
items, next, more, err := h.service.Storage(c.Request.Context(), req.Prefix, req.Cursor, req.Limit)
@@ -143,7 +145,7 @@ func (h *Media) Categories(c *gin.Context) {
}
func (h *Media) SaveCategory(c *gin.Context) {
var req dto.CategoryRequest
if c.ShouldBindJSON(&req) != nil {
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, "参数错误")
return
}
@@ -169,10 +171,14 @@ func (h *Media) DeleteCategory(c *gin.Context) {
func (h *Media) InitUpload(c *gin.Context) {
claims := servermiddleware.Claims(c)
var req dto.InitUploadRequest
if claims == nil || c.ShouldBindJSON(&req) != nil {
if claims == nil {
httpx.Fail(c, "参数错误")
return
}
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if config := h.service.MediaConfig(); config.MaxFileSize > 0 && req.FileSize > config.MaxFileSize {
httpx.Fail(c, "文件超过大小上限")
return
@@ -195,7 +201,7 @@ func (h *Media) SaveChunk(c *gin.Context) {
}
opened, err := header.Open()
if err != nil {
httpx.Fail(c, "读取分片失败")
httpx.Fail(c, "分片读取失败")
return
}
defer opened.Close()
@@ -208,10 +214,14 @@ func (h *Media) SaveChunk(c *gin.Context) {
func (h *Media) CompleteUpload(c *gin.Context) {
claims := servermiddleware.Claims(c)
var req dto.CompleteUploadRequest
if claims == nil || c.ShouldBindJSON(&req) != nil {
if claims == nil {
httpx.Fail(c, "参数错误")
return
}
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
item, err := h.service.CompleteUpload(c.Request.Context(), claims.ID, req.UploadID)
if err != nil {
httpx.Fail(c, err.Error())
+88 -22
View File
@@ -1,8 +1,6 @@
package handler
import (
"strconv"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
@@ -34,8 +32,28 @@ func (h *Menu) Tree(c *gin.Context) {
func (h *Menu) Create(c *gin.Context) {
var req dto.MenuRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.Path == "" {
httpx.Fail(c, "Path值不能为空")
return
}
if req.Name == "" {
httpx.Fail(c, "Name值不能为空")
return
}
if req.Component == "" {
httpx.Fail(c, "Component值不能为空")
return
}
if req.Sort < 0 {
httpx.Fail(c, "Sort长度或值不在合法范围,ge=0")
return
}
if req.Meta.Title == "" {
httpx.Fail(c, "Title值不能为空")
return
}
if err := h.service.Create(c.Request.Context(), &req); err != nil {
@@ -47,8 +65,28 @@ func (h *Menu) Create(c *gin.Context) {
func (h *Menu) Update(c *gin.Context) {
var req dto.MenuRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.Path == "" {
httpx.Fail(c, "Path值不能为空")
return
}
if req.Name == "" {
httpx.Fail(c, "Name值不能为空")
return
}
if req.Component == "" {
httpx.Fail(c, "Component值不能为空")
return
}
if req.Sort < 0 {
httpx.Fail(c, "Sort长度或值不在合法范围,ge=0")
return
}
if req.Meta.Title == "" {
httpx.Fail(c, "Title值不能为空")
return
}
if err := h.service.Update(c.Request.Context(), &req); err != nil {
@@ -60,8 +98,12 @@ func (h *Menu) Update(c *gin.Context) {
func (h *Menu) Delete(c *gin.Context) {
var req dto.DeleteMenuRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.ID == 0 {
httpx.Fail(c, "ID值不能为空")
return
}
if err := h.service.Delete(c.Request.Context(), req.ID); err != nil {
@@ -73,8 +115,12 @@ func (h *Menu) Delete(c *gin.Context) {
func (h *Menu) Find(c *gin.Context) {
var req dto.GetMenuRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.ID == 0 {
httpx.Fail(c, "ID值不能为空")
return
}
item, err := h.service.Find(c.Request.Context(), req.ID)
@@ -87,8 +133,12 @@ func (h *Menu) Find(c *gin.Context) {
func (h *Menu) SetAuthorityMenus(c *gin.Context) {
var req dto.SetAuthorityMenusRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
if err := h.service.SetAuthorityMenus(c.Request.Context(), &req); err != nil {
@@ -100,8 +150,12 @@ func (h *Menu) SetAuthorityMenus(c *gin.Context) {
func (h *Menu) AuthorityMenus(c *gin.Context) {
var req dto.GetAuthorityMenusRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
menus, err := h.service.AuthorityMenus(c.Request.Context(), req.AuthorityID)
@@ -113,18 +167,26 @@ func (h *Menu) AuthorityMenus(c *gin.Context) {
}
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))
var req dto.SetMenuRolesRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.MenuID == 0 {
httpx.Fail(c, "菜单ID不能为空")
return
}
ids, err := h.service.RoleIDs(c.Request.Context(), req.MenuID)
if err != nil {
httpx.Fail(c, "获取失败")
httpx.Fail(c, "获取失败"+err.Error())
return
}
if ids == nil {
ids = []uint{}
}
defaultIDs, err := h.service.DefaultRouterRoleIDs(c.Request.Context(), uint(id))
defaultIDs, err := h.service.DefaultRouterRoleIDs(c.Request.Context(), req.MenuID)
if err != nil {
httpx.Fail(c, "获取失败")
httpx.Fail(c, "获取失败"+err.Error())
return
}
if defaultIDs == nil {
@@ -135,12 +197,16 @@ func (h *Menu) RoleIDs(c *gin.Context) {
func (h *Menu) SetRoles(c *gin.Context) {
var req dto.SetMenuRolesRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.MenuID == 0 {
httpx.Fail(c, "菜单ID不能为空")
return
}
if err := h.service.SetRoles(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "设置失败")
httpx.Fail(c, "设置失败"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
+16 -8
View File
@@ -41,7 +41,7 @@ func (h *Organization) CreateDepartment(c *gin.Context) {
httpx.Fail(c, "创建失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, nil, "创建成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "创建成功")
}
func (h *Organization) UpdateDepartment(c *gin.Context) {
var req dto.DepartmentRequest
@@ -57,7 +57,7 @@ func (h *Organization) UpdateDepartment(c *gin.Context) {
httpx.Fail(c, "更新失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, nil, "更新成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "更新成功")
}
func (h *Organization) DeleteDepartment(c *gin.Context) {
var req dto.DeleteDepartmentRequest
@@ -69,7 +69,7 @@ func (h *Organization) DeleteDepartment(c *gin.Context) {
httpx.Fail(c, "删除失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, nil, "删除成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
}
func (h *Organization) FindDepartment(c *gin.Context) {
var req dto.DeleteDepartmentRequest
@@ -122,8 +122,12 @@ func (h *Organization) SetUserDepartments(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if req.ID == 0 {
httpx.Fail(c, "用户ID不能为空")
return
}
if err := h.departments.SetUserDepartments(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "设置失败")
httpx.Fail(c, "设置失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
@@ -156,7 +160,7 @@ func (h *Organization) CreatePosition(c *gin.Context) {
httpx.Fail(c, "创建失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, nil, "创建成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "创建成功")
}
func (h *Organization) UpdatePosition(c *gin.Context) {
var req dto.PositionRequest
@@ -172,7 +176,7 @@ func (h *Organization) UpdatePosition(c *gin.Context) {
httpx.Fail(c, "更新失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, nil, "更新成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "更新成功")
}
func (h *Organization) DeletePosition(c *gin.Context) {
var req dto.DeletePositionRequest
@@ -184,7 +188,7 @@ func (h *Organization) DeletePosition(c *gin.Context) {
httpx.Fail(c, "删除失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, nil, "删除成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
}
func (h *Organization) FindPosition(c *gin.Context) {
var req dto.DeletePositionRequest
@@ -237,8 +241,12 @@ func (h *Organization) SetUserPositions(c *gin.Context) {
httpx.Fail(c, err.Error())
return
}
if req.ID == 0 {
httpx.Fail(c, "用户ID不能为空")
return
}
if err := h.positions.SetUserPositions(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "设置失败")
httpx.Fail(c, "设置失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
+5 -7
View File
@@ -82,17 +82,15 @@ func (h *Parameter) Get(c *gin.Context) {
}
func (h *Parameter) List(c *gin.Context) {
p, size := page(c)
start, startErr := parseTime(c.Query("startCreatedAt"))
end, endErr := parseTime(c.Query("endCreatedAt"))
if startErr != nil || endErr != nil {
httpx.Fail(c, "参数错误")
var req dto.SystemParameterSearchRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
items, total, err := h.service.ParametersFilter(c.Request.Context(), p, size, c.Query("name"), c.Query("key"), start, end)
items, total, err := h.service.ParametersFilter(c.Request.Context(), req.Page, req.PageSize, req.Name, req.Key, req.StartCreatedAt, req.EndCreatedAt)
if err != nil {
httpx.Fail(c, "获取失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: p, PageSize: size}, "获取成功")
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
+5 -5
View File
@@ -18,8 +18,8 @@ func NewPermission(service *service.PermissionService) *Permission {
func (h *Permission) Buttons(c *gin.Context) {
var req dto.GetAuthorityButtonsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
selected, err := h.service.SelectedButtons(c.Request.Context(), req.AuthorityID, req.MenuID)
@@ -32,8 +32,8 @@ func (h *Permission) Buttons(c *gin.Context) {
func (h *Permission) SetButtons(c *gin.Context) {
var req dto.SetAuthorityButtonsRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.SetSelectedButtons(c.Request.Context(), req.AuthorityID, req.MenuID, req.Selected); err != nil {
@@ -47,7 +47,7 @@ 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, "检查失败")
httpx.Fail(c, err.Error())
return
}
if !allowed {
+12 -4
View File
@@ -53,8 +53,16 @@ func (h *Public) Captcha(c *gin.Context) {
func (h *Public) Login(c *gin.Context) {
var req dto.LoginRequest
if c.ShouldBindJSON(&req) != nil || req.Username == "" || req.Password == "" {
httpx.Fail(c, "请输入用户名和密码")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.Username == "" {
httpx.Fail(c, "Username值不能为空")
return
}
if req.Password == "" {
httpx.Fail(c, "Password值不能为空")
return
}
result, err := h.auth.Login(c.Request.Context(), &req, c.ClientIP(), c.Request.UserAgent())
@@ -109,12 +117,12 @@ func (h *Public) InitializeDatabase(engine *gin.Engine) gin.HandlerFunc {
return
}
if initialized {
httpx.Fail(c, "数据库已初始化,无需重复初始化")
httpx.Fail(c, "已存在数据库配置")
return
}
var input service.DatabaseInit
if c.ShouldBindJSON(&input) != nil {
httpx.Fail(c, "数据库初始化参数无效")
httpx.Fail(c, "参数校验不通过")
return
}
routes := engine.Routes()
+23 -24
View File
@@ -3,7 +3,6 @@ package handler
import (
"fmt"
"net/http"
"strconv"
"time"
"kra/internal/server/httpx"
@@ -24,8 +23,8 @@ func NewTask(service *service.TaskService) *Task {
func (h *Task) Create(c *gin.Context) {
var req dto.TaskRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
_, err := h.service.CreateRequest(c.Request.Context(), &req)
@@ -38,8 +37,8 @@ func (h *Task) Create(c *gin.Context) {
func (h *Task) Update(c *gin.Context) {
var req dto.TaskRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.UpdateRequest(c.Request.Context(), &req); err != nil {
@@ -51,8 +50,8 @@ func (h *Task) Update(c *gin.Context) {
func (h *Task) Delete(c *gin.Context) {
var req dto.DeleteTaskRequest
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.Delete(c.Request.Context(), req.ID); err != nil {
@@ -64,8 +63,8 @@ func (h *Task) Delete(c *gin.Context) {
func (h *Task) Toggle(c *gin.Context) {
var req dto.ToggleTaskRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.Toggle(c.Request.Context(), req.ID, req.Enabled); err != nil {
@@ -77,8 +76,8 @@ func (h *Task) Toggle(c *gin.Context) {
func (h *Task) Trigger(c *gin.Context) {
var req dto.DeleteTaskRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.Trigger(c.Request.Context(), req.ID); err != nil {
@@ -89,31 +88,31 @@ func (h *Task) Trigger(c *gin.Context) {
}
func (h *Task) List(c *gin.Context) {
page, _ := strconv.Atoi(c.Query("page"))
size, _ := strconv.Atoi(c.Query("pageSize"))
var enabled *bool
if raw := c.Query("enabled"); raw != "" {
value, _ := strconv.ParseBool(raw)
enabled = &value
var req dto.TaskListRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
items, total, err := h.service.ListRequest(c.Request.Context(), page, size, c.Query("name"), c.Query("executorType"), enabled)
items, total, err := h.service.ListRequest(c.Request.Context(), req.Page, req.PageSize, req.Name, req.ExecutorType, req.Enabled)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
func (h *Task) Logs(c *gin.Context) {
page, _ := strconv.Atoi(c.Query("page"))
size, _ := strconv.Atoi(c.Query("pageSize"))
taskID, _ := strconv.ParseUint(c.Query("taskId"), 10, 64)
items, total, err := h.service.Logs(c.Request.Context(), page, size, uint(taskID), c.Query("status"))
var req dto.TaskLogListRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
items, total, err := h.service.Logs(c.Request.Context(), req.Page, req.PageSize, req.TaskID, req.Status)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
func (h *Task) Methods(c *gin.Context) {
+77 -21
View File
@@ -23,8 +23,16 @@ func NewUser(user *service.UserService, auth *service.AuthService) *User {
func (h *User) List(c *gin.Context) {
var req dto.UserListRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.Page == 0 {
httpx.Fail(c, "Page值不能为空")
return
}
if req.PageSize == 0 {
httpx.Fail(c, "PageSize值不能为空")
return
}
items, total, err := h.service.ListUsersRequest(c.Request.Context(), &req)
@@ -37,8 +45,24 @@ func (h *User) List(c *gin.Context) {
func (h *User) Create(c *gin.Context) {
var req dto.UserRequest
if c.ShouldBindJSON(&req) != nil || req.Username == "" || req.Password == "" {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.Username == "" {
httpx.Fail(c, "Username值不能为空")
return
}
if req.Password == "" {
httpx.Fail(c, "Password值不能为空")
return
}
if req.NickName == "" {
httpx.Fail(c, "NickName值不能为空")
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
result, err := h.service.CreateUserRequest(c.Request.Context(), &req)
@@ -51,8 +75,12 @@ func (h *User) Create(c *gin.Context) {
func (h *User) Update(c *gin.Context) {
var req dto.UserRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.ID == 0 {
httpx.Fail(c, "ID值不能为空")
return
}
if err := h.service.UpdateUserRequest(c.Request.Context(), &req); err != nil {
@@ -69,8 +97,8 @@ func (h *User) UpdateSelf(c *gin.Context) {
return
}
var req dto.SelfUserRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
value := service.UserInput{ID: claims.ID, NickName: req.NickName, HeaderImg: req.HeaderImg, Phone: req.Phone, Email: req.Email, Enable: req.Enable}
@@ -83,8 +111,12 @@ func (h *User) UpdateSelf(c *gin.Context) {
func (h *User) Delete(c *gin.Context) {
var req dto.IDRequest
if c.ShouldBind(&req) != nil || req.ID == 0 {
httpx.Fail(c, "参数错误")
if err := c.ShouldBind(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.ID == 0 {
httpx.Fail(c, "ID值不能为空")
return
}
claims := middleware.Claims(c)
@@ -101,8 +133,8 @@ func (h *User) Delete(c *gin.Context) {
func (h *User) ResetPassword(c *gin.Context) {
var req dto.ResetPasswordRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.ResetPassword(c.Request.Context(), req.ID, req.Password); err != nil {
@@ -115,8 +147,20 @@ func (h *User) ResetPassword(c *gin.Context) {
func (h *User) ChangePassword(c *gin.Context) {
claims := middleware.Claims(c)
var req dto.ChangePasswordRequest
if claims == nil || c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if claims == nil {
httpx.NoAuth(c, "未登录或非法访问")
return
}
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.Password == "" {
httpx.Fail(c, "Password值不能为空")
return
}
if req.NewPassword == "" {
httpx.Fail(c, "NewPassword值不能为空")
return
}
if err := h.service.ChangePassword(c.Request.Context(), claims.ID, req.Password, req.NewPassword); err != nil {
@@ -129,8 +173,12 @@ func (h *User) ChangePassword(c *gin.Context) {
func (h *User) SetSelfSetting(c *gin.Context) {
claims := middleware.Claims(c)
var setting map[string]any
if claims == nil || c.ShouldBindJSON(&setting) != nil {
httpx.Fail(c, "参数错误")
if claims == nil {
httpx.NoAuth(c, "未登录或非法访问")
return
}
if err := c.ShouldBindJSON(&setting); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.SetUserSetting(c.Request.Context(), claims.ID, setting); err != nil {
@@ -142,8 +190,8 @@ func (h *User) SetSelfSetting(c *gin.Context) {
func (h *User) SetAuthorities(c *gin.Context) {
var req dto.UserAuthoritiesRequest
if c.ShouldBindJSON(&req) != nil || req.ID == 0 || len(req.AuthorityIDs) == 0 {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.SetUserAuthorities(c.Request.Context(), req.ID, req.AuthorityIDs); err != nil {
@@ -156,11 +204,19 @@ func (h *User) SetAuthorities(c *gin.Context) {
func (h *User) SwitchAuthority(c *gin.Context) {
claims := middleware.Claims(c)
var req dto.SwitchAuthorityRequest
if claims == nil || c.ShouldBindJSON(&req) != nil || req.AuthorityID == 0 {
httpx.Fail(c, "参数错误")
if claims == nil {
httpx.NoAuth(c, "未登录或非法访问")
return
}
login, err := h.auth.SwitchAuthority(c.Request.Context(), claims.ID, req.AuthorityID)
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.AuthorityID == 0 {
httpx.Fail(c, "AuthorityId值不能为空")
return
}
login, err := h.auth.SwitchAuthority(c.Request.Context(), claims, req.AuthorityID)
if err != nil {
httpx.Fail(c, err.Error())
return
+74 -25
View File
@@ -1,11 +1,13 @@
package handler
import (
"errors"
"fmt"
"net/http"
"strconv"
"time"
"kra/internal/biz"
"kra/internal/server/httpx"
"kra/internal/service"
"kra/internal/service/dto"
@@ -17,10 +19,18 @@ type Version struct{ service *service.VersionService }
func NewVersion(service *service.VersionService) *Version { return &Version{service: service} }
func versionStage(err error) (biz.VersionStage, bool) {
var stageErr *biz.VersionStageError
if !errors.As(err, &stageErr) {
return "", false
}
return stageErr.Stage, true
}
func (h *Version) Delete(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
if err := h.service.DeleteVersions(c.Request.Context(), []uint{uint(id)}); err != nil {
httpx.Fail(c, "删除失败")
httpx.Fail(c, "删除失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
@@ -33,7 +43,7 @@ func (h *Version) DeleteMany(c *gin.Context) {
ids = append(ids, uint(id))
}
if err := h.service.DeleteVersions(c.Request.Context(), ids); err != nil {
httpx.Fail(c, "删除失败")
httpx.Fail(c, "批量删除失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "批量删除成功")
@@ -42,60 +52,99 @@ func (h *Version) Find(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
item, err := h.service.Version(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "查询失败")
httpx.Fail(c, "查询失败:"+err.Error())
return
}
httpx.OKWithData(c, item)
}
func (h *Version) List(c *gin.Context) {
p, size := page(c)
createdAtRange := make([]*time.Time, 0, 2)
for _, raw := range c.QueryArray("createdAtRange[]") {
value, parseErr := parseTime(raw)
if parseErr != nil {
httpx.Fail(c, parseErr.Error())
return
}
createdAtRange = append(createdAtRange, value)
}
items, total, err := h.service.Versions(c.Request.Context(), p, size, c.Query("versionName"), c.Query("versionCode"), createdAtRange)
if err != nil {
httpx.Fail(c, "获取失败")
var req dto.VersionSearchRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: p, PageSize: size}, "获取成功")
name, code := "", ""
if req.VersionName != nil {
name = *req.VersionName
}
if req.VersionCode != nil {
code = *req.VersionCode
}
createdAtRange := make([]*time.Time, 0, len(req.CreatedAtRange))
for i := range req.CreatedAtRange {
createdAtRange = append(createdAtRange, &req.CreatedAtRange[i])
}
items, total, err := h.service.Versions(c.Request.Context(), req.Page, req.PageSize, name, code, createdAtRange)
if err != nil {
httpx.Fail(c, "获取失败:"+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
func (h *Version) Export(c *gin.Context) {
var req dto.ExportVersionRequest
if c.ShouldBindJSON(&req) != nil || req.VersionName == "" || req.VersionCode == "" {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.Export(c.Request.Context(), req.VersionName, req.VersionCode, req.Description, req.MenuIDs, req.APIIDs, req.DictIDs); err != nil {
httpx.Fail(c, "创建发版失败:"+err.Error())
switch stage, _ := versionStage(err); stage {
case biz.VersionStageMenus:
httpx.Fail(c, "获取菜单数据失败:"+err.Error())
case biz.VersionStageAPIs:
httpx.Fail(c, "获取API数据失败:"+err.Error())
case biz.VersionStageDictionaries:
httpx.Fail(c, "获取字典数据失败:"+err.Error())
case biz.VersionStageJSON:
httpx.Fail(c, "JSON序列化失败:"+err.Error())
case biz.VersionStageSave:
httpx.Fail(c, "保存版本记录失败:"+err.Error())
default:
httpx.Fail(c, "创建发版失败:"+err.Error())
}
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "创建发版成功")
}
func (h *Version) Download(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
rawID := c.Query("ID")
if rawID == "" {
httpx.Fail(c, "版本ID不能为空")
return
}
id, _ := strconv.ParseUint(rawID, 10, 64)
raw, code, err := h.service.VersionData(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "下载失败")
httpx.Fail(c, "获取版本记录失败:"+err.Error())
return
}
name := fmt.Sprintf("version_%s_%s.json", code, time.Now().Format("20060102150405"))
c.Header("Content-Type", "application/json")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", name))
c.Header("Content-Length", strconv.Itoa(len(raw)))
c.Data(http.StatusOK, "application/json; charset=utf-8", []byte(raw))
}
func (h *Version) Import(c *gin.Context) {
var req dto.ImportVersionRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, "解析JSON数据失败:"+err.Error())
return
}
if err := h.service.ImportRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "导入失败:"+err.Error())
if errors.Is(err, biz.ErrInvalidVersion) {
httpx.Fail(c, "版本信息格式错误")
return
}
switch stage, _ := versionStage(err); stage {
case biz.VersionStageMenus:
httpx.Fail(c, "导入菜单失败: "+err.Error())
case biz.VersionStageAPIs:
httpx.Fail(c, "导入API失败: "+err.Error())
case biz.VersionStageDictionaries:
httpx.Fail(c, "导入字典失败: "+err.Error())
default:
httpx.Fail(c, "导入失败:"+err.Error())
}
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "导入成功")