166 lines
5.6 KiB
Go
166 lines
5.6 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"kra/internal/biz"
|
|
"kra/internal/service"
|
|
)
|
|
|
|
type versionMenuRequest struct {
|
|
Path string `json:"path"`
|
|
Name string `json:"name"`
|
|
Hidden bool `json:"hidden"`
|
|
Component string `json:"component"`
|
|
Sort int `json:"sort"`
|
|
Meta struct {
|
|
ActiveName string `json:"activeName"`
|
|
KeepAlive bool `json:"keepAlive"`
|
|
DefaultMenu bool `json:"defaultMenu"`
|
|
Title string `json:"title"`
|
|
Icon string `json:"icon"`
|
|
CloseTab bool `json:"closeTab"`
|
|
TransitionType string `json:"transitionType"`
|
|
} `json:"meta"`
|
|
MenuButtons []struct {
|
|
Name string `json:"name"`
|
|
Desc string `json:"desc"`
|
|
} `json:"menuBtn"`
|
|
Children []versionMenuRequest `json:"children"`
|
|
}
|
|
|
|
func versionMenuDomain(v versionMenuRequest) *biz.Menu {
|
|
m := &biz.Menu{Path: v.Path, Name: v.Name, Hidden: v.Hidden, Component: v.Component, Sort: v.Sort, ActiveName: v.Meta.ActiveName, KeepAlive: v.Meta.KeepAlive, DefaultMenu: v.Meta.DefaultMenu, Title: v.Meta.Title, Icon: v.Meta.Icon, CloseTab: v.Meta.CloseTab, TransitionType: v.Meta.TransitionType}
|
|
for _, b := range v.MenuButtons {
|
|
m.Buttons = append(m.Buttons, &biz.MenuButton{Name: b.Name, Description: b.Desc})
|
|
}
|
|
for _, child := range v.Children {
|
|
m.Children = append(m.Children, versionMenuDomain(child))
|
|
}
|
|
return m
|
|
}
|
|
func registerVersionRoutes(group *gin.RouterGroup, svc *service.VersionService) {
|
|
router := group.Group("/sysVersion")
|
|
router.DELETE("/deleteSysVersion", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
if err := svc.DeleteVersions(c.Request.Context(), []uint{uint(id)}); err != nil {
|
|
fail(c, "删除失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.DELETE("/deleteSysVersionByIds", func(c *gin.Context) {
|
|
raw := c.QueryArray("IDs[]")
|
|
ids := make([]uint, 0, len(raw))
|
|
for _, v := range raw {
|
|
id, _ := strconv.ParseUint(v, 10, 64)
|
|
ids = append(ids, uint(id))
|
|
}
|
|
if err := svc.DeleteVersions(c.Request.Context(), ids); err != nil {
|
|
fail(c, "删除失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.GET("/findSysVersion", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := svc.Version(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "查询失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, item, "查询成功")
|
|
})
|
|
router.GET("/getSysVersionList", func(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
size, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
|
items, total, err := svc.Versions(c.Request.Context(), page, size, c.Query("versionName"), c.Query("versionCode"))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
|
|
})
|
|
router.POST("/exportVersion", func(c *gin.Context) {
|
|
var req struct {
|
|
VersionName string `json:"versionName"`
|
|
VersionCode string `json:"versionCode"`
|
|
Description string `json:"description"`
|
|
MenuIDs []uint `json:"menuIds"`
|
|
APIIDs []uint `json:"apiIds"`
|
|
DictIDs []uint `json:"dictIds"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil || req.VersionName == "" || req.VersionCode == "" {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.Export(c.Request.Context(), req.VersionName, req.VersionCode, req.Description, req.MenuIDs, req.APIIDs, req.DictIDs); err != nil {
|
|
fail(c, "创建发版失败:"+err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.GET("/downloadVersionJson", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
raw, code, err := svc.VersionData(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "下载失败")
|
|
return
|
|
}
|
|
name := fmt.Sprintf("version_%s_%s.json", code, time.Now().Format("20060102150405"))
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", name))
|
|
c.Data(http.StatusOK, "application/json; charset=utf-8", []byte(raw))
|
|
})
|
|
router.POST("/importVersion", func(c *gin.Context) {
|
|
var req struct {
|
|
Version struct {
|
|
Name string `json:"name"`
|
|
Code string `json:"code"`
|
|
Description string `json:"description"`
|
|
ExportTime string `json:"exportTime"`
|
|
} `json:"version"`
|
|
Menus []versionMenuRequest `json:"menus"`
|
|
APIs []struct {
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
APIGroup string `json:"apiGroup"`
|
|
Method string `json:"method"`
|
|
} `json:"apis"`
|
|
Dictionaries []struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Status bool `json:"status"`
|
|
Desc string `json:"desc"`
|
|
Details []detailRequest `json:"sysDictionaryDetails"`
|
|
} `json:"dictionaries"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
bundle := &biz.VersionBundle{Name: req.Version.Name, Code: req.Version.Code, Description: req.Version.Description, ExportTime: req.Version.ExportTime}
|
|
for _, m := range req.Menus {
|
|
bundle.Menus = append(bundle.Menus, versionMenuDomain(m))
|
|
}
|
|
for _, a := range req.APIs {
|
|
bundle.APIs = append(bundle.APIs, &biz.API{Path: a.Path, Description: a.Description, APIGroup: a.APIGroup, Method: a.Method})
|
|
}
|
|
for _, d := range req.Dictionaries {
|
|
dictionary := &biz.Dictionary{Name: d.Name, Type: d.Type, Status: d.Status, Desc: d.Desc}
|
|
for _, detail := range d.Details {
|
|
dictionary.Details = append(dictionary.Details, detailDomain(detail))
|
|
}
|
|
bundle.Dictionaries = append(bundle.Dictionaries, dictionary)
|
|
}
|
|
if err := svc.Import(c.Request.Context(), bundle); err != nil {
|
|
fail(c, "导入失败:"+err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
}
|