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
+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{}, "导入成功")