438 lines
13 KiB
Go
438 lines
13 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type dictionaryRequest struct {
|
|
ID uint `json:"ID" form:"ID"`
|
|
Name string `json:"name" form:"name"`
|
|
Type string `json:"type" form:"type"`
|
|
Status *bool `json:"status" form:"status"`
|
|
Desc string `json:"desc" form:"desc"`
|
|
ParentID *uint `json:"parentID" form:"parentID"`
|
|
}
|
|
|
|
func dictionaryDomain(v dictionaryRequest) *biz.Dictionary {
|
|
status := true
|
|
if v.Status != nil {
|
|
status = *v.Status
|
|
}
|
|
return &biz.Dictionary{ID: v.ID, Name: v.Name, Type: v.Type, Status: status, Desc: v.Desc, ParentID: v.ParentID}
|
|
}
|
|
|
|
type detailRequest struct {
|
|
ID uint `json:"ID" form:"ID"`
|
|
Label string `json:"label" form:"label"`
|
|
Value string `json:"value" form:"value"`
|
|
Extend string `json:"extend" form:"extend"`
|
|
Status *bool `json:"status" form:"status"`
|
|
Sort int `json:"sort" form:"sort"`
|
|
DictionaryID uint `json:"sysDictionaryID" form:"sysDictionaryID"`
|
|
ParentID *uint `json:"parentID" form:"parentID"`
|
|
Level int `json:"level"`
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
func detailDomain(v detailRequest) *biz.DictionaryDetail {
|
|
status := true
|
|
if v.Status != nil {
|
|
status = *v.Status
|
|
}
|
|
return &biz.DictionaryDetail{ID: v.ID, Label: v.Label, Value: v.Value, Extend: v.Extend, Status: status, Sort: v.Sort, DictionaryID: v.DictionaryID, ParentID: v.ParentID, Level: v.Level, Path: v.Path}
|
|
}
|
|
|
|
func registerSettingsRoutes(group *gin.RouterGroup, public *gin.RouterGroup, svc *service.SettingsService) {
|
|
registerDictionaryRoutes(group, svc)
|
|
registerParameterRoutes(group, svc)
|
|
registerAPITokenRoutes(group, svc)
|
|
}
|
|
|
|
func registerDictionaryRoutes(group *gin.RouterGroup, svc *service.SettingsService) {
|
|
dictionaries := group.Group("/sysDictionary")
|
|
dictionaries.POST("/createSysDictionary", func(c *gin.Context) {
|
|
var req dictionaryRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.Name == "" || req.Type == "" {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.CreateDictionary(c.Request.Context(), dictionaryDomain(req)); err != nil {
|
|
fail(c, "创建失败:"+err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
dictionaries.PUT("/updateSysDictionary", func(c *gin.Context) {
|
|
var req dictionaryRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.UpdateDictionary(c.Request.Context(), dictionaryDomain(req)); err != nil {
|
|
fail(c, "更新失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
dictionaries.DELETE("/deleteSysDictionary", func(c *gin.Context) {
|
|
var req dictionaryRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteDictionary(c.Request.Context(), req.ID); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
dictionaries.GET("/findSysDictionary", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := svc.Dictionary(c.Request.Context(), uint(id), c.Query("type"), true)
|
|
if err != nil {
|
|
fail(c, "查询失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"reSysDictionary": item}, "查询成功")
|
|
})
|
|
listHandler := func(details bool) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
size, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
|
items, total, err := svc.Dictionaries(c.Request.Context(), page, size, c.Query("name"), c.Query("type"), details)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
|
|
}
|
|
}
|
|
dictionaries.GET("/getSysDictionaryList", listHandler(false))
|
|
dictionaries.GET("/getSysDictionaryListWithDetails", listHandler(true))
|
|
dictionaries.GET("/exportSysDictionary", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := svc.Dictionary(c.Request.Context(), uint(id), "", true)
|
|
if err != nil {
|
|
fail(c, "导出失败")
|
|
return
|
|
}
|
|
delete(item, "ID")
|
|
delete(item, "CreatedAt")
|
|
delete(item, "UpdatedAt")
|
|
delete(item, "DeletedAt")
|
|
writeResult(c, codeSuccess, item, "导出成功")
|
|
})
|
|
dictionaries.POST("/importSysDictionary", func(c *gin.Context) {
|
|
var req struct {
|
|
JSON string `json:"json"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
var payload struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Status *bool `json:"status"`
|
|
Desc string `json:"desc"`
|
|
Details []detailRequest `json:"sysDictionaryDetails"`
|
|
}
|
|
if json.Unmarshal([]byte(req.JSON), &payload) != nil || payload.Name == "" || payload.Type == "" {
|
|
fail(c, "JSON 格式错误")
|
|
return
|
|
}
|
|
dictionary := dictionaryDomain(dictionaryRequest{Name: payload.Name, Type: payload.Type, Status: payload.Status, Desc: payload.Desc})
|
|
if err := svc.CreateDictionary(c.Request.Context(), dictionary); err != nil {
|
|
fail(c, "导入失败:"+err.Error())
|
|
return
|
|
}
|
|
mapping := map[uint]uint{}
|
|
pending := append([]detailRequest(nil), payload.Details...)
|
|
for len(pending) > 0 {
|
|
progress := false
|
|
next := pending[:0]
|
|
for _, item := range pending {
|
|
oldID := item.ID
|
|
if item.ParentID != nil && *item.ParentID != 0 {
|
|
parent, exists := mapping[*item.ParentID]
|
|
if !exists {
|
|
next = append(next, item)
|
|
continue
|
|
}
|
|
item.ParentID = &parent
|
|
}
|
|
value := detailDomain(item)
|
|
value.ID = 0
|
|
value.DictionaryID = dictionary.ID
|
|
if err := svc.CreateDictionaryDetail(c.Request.Context(), value); err != nil {
|
|
fail(c, "导入详情失败")
|
|
return
|
|
}
|
|
if oldID != 0 {
|
|
mapping[oldID] = value.ID
|
|
}
|
|
progress = true
|
|
}
|
|
if !progress {
|
|
fail(c, "字典层级关系无效")
|
|
return
|
|
}
|
|
pending = next
|
|
}
|
|
ok(c)
|
|
})
|
|
|
|
details := group.Group("/sysDictionaryDetail")
|
|
details.POST("/createSysDictionaryDetail", func(c *gin.Context) {
|
|
var req detailRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.DictionaryID == 0 {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.CreateDictionaryDetail(c.Request.Context(), detailDomain(req)); err != nil {
|
|
fail(c, "创建失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
details.PUT("/updateSysDictionaryDetail", func(c *gin.Context) {
|
|
var req detailRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.UpdateDictionaryDetail(c.Request.Context(), detailDomain(req)); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
details.DELETE("/deleteSysDictionaryDetail", func(c *gin.Context) {
|
|
var req detailRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteDictionaryDetail(c.Request.Context(), req.ID); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
details.GET("/findSysDictionaryDetail", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := svc.DictionaryDetail(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "查询失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"reSysDictionaryDetail": item}, "查询成功")
|
|
})
|
|
details.GET("/getSysDictionaryDetailList", func(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
size, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
|
did, _ := strconv.ParseUint(c.Query("sysDictionaryID"), 10, 64)
|
|
items, total, err := svc.DictionaryDetails(c.Request.Context(), page, size, uint(did), c.Query("label"), c.Query("value"))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
|
|
})
|
|
treeHandler := func(byType bool) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("sysDictionaryID"), 10, 64)
|
|
typ := ""
|
|
if byType {
|
|
typ = c.Query("type")
|
|
}
|
|
items, err := svc.DictionaryTree(c.Request.Context(), uint(id), typ)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, items, "获取成功")
|
|
}
|
|
}
|
|
details.GET("/getDictionaryTreeList", treeHandler(false))
|
|
details.GET("/getDictionaryTreeListByType", treeHandler(true))
|
|
details.GET("/getDictionaryDetailsByParent", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("parentID"), 10, 64)
|
|
did, _ := strconv.ParseUint(c.Query("sysDictionaryID"), 10, 64)
|
|
items, _, err := svc.DictionaryDetails(c.Request.Context(), 1, 10000, uint(did), "", "")
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
out := []map[string]any{}
|
|
for _, item := range items {
|
|
parent, _ := item["parentID"].(*uint)
|
|
if (id == 0 && parent == nil) || (parent != nil && uint64(*parent) == id) {
|
|
out = append(out, item)
|
|
}
|
|
}
|
|
writeResult(c, codeSuccess, out, "获取成功")
|
|
})
|
|
details.GET("/getDictionaryPath", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := svc.DictionaryDetail(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"path": item["path"]}, "获取成功")
|
|
})
|
|
}
|
|
|
|
type parameterRequest struct {
|
|
ID uint `json:"ID" form:"ID"`
|
|
Name string `json:"name" form:"name"`
|
|
Key string `json:"key" form:"key"`
|
|
Value string `json:"value" form:"value"`
|
|
Desc string `json:"desc" form:"desc"`
|
|
}
|
|
|
|
func parameterDomain(v parameterRequest) *biz.SystemParameter {
|
|
return &biz.SystemParameter{ID: v.ID, Name: v.Name, Key: v.Key, Value: v.Value, Desc: v.Desc}
|
|
}
|
|
func registerParameterRoutes(group *gin.RouterGroup, svc *service.SettingsService) {
|
|
router := group.Group("/sysParams")
|
|
router.POST("/createSysParams", func(c *gin.Context) {
|
|
var req parameterRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.Key == "" {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.CreateParameter(c.Request.Context(), parameterDomain(req)); err != nil {
|
|
fail(c, "创建失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.PUT("/updateSysParams", func(c *gin.Context) {
|
|
var req parameterRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.UpdateParameter(c.Request.Context(), parameterDomain(req)); err != nil {
|
|
fail(c, "更新失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.DELETE("/deleteSysParams", func(c *gin.Context) {
|
|
var req parameterRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteParameters(c.Request.Context(), []uint{req.ID}); err != nil {
|
|
fail(c, "删除失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.DELETE("/deleteSysParamsByIds", func(c *gin.Context) {
|
|
var req struct {
|
|
IDs []uint `json:"ids"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteParameters(c.Request.Context(), req.IDs); err != nil {
|
|
fail(c, "删除失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.GET("/findSysParams", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := svc.Parameter(c.Request.Context(), uint(id), "")
|
|
if err != nil {
|
|
fail(c, "查询失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"resysParams": item}, "查询成功")
|
|
})
|
|
router.GET("/getSysParam", func(c *gin.Context) {
|
|
item, err := svc.Parameter(c.Request.Context(), 0, c.Query("key"))
|
|
if err != nil {
|
|
fail(c, "查询失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"data": item}, "查询成功")
|
|
})
|
|
router.GET("/getSysParamsList", func(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
size, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
|
items, total, err := svc.Parameters(c.Request.Context(), page, size, &biz.SystemParameter{Name: c.Query("name"), Key: c.Query("key")})
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
|
|
})
|
|
}
|
|
|
|
func registerAPITokenRoutes(group *gin.RouterGroup, svc *service.SettingsService) {
|
|
router := group.Group("/sysApiToken")
|
|
router.POST("/createApiToken", func(c *gin.Context) {
|
|
var req struct {
|
|
UserID uint `json:"userId"`
|
|
AuthorityID uint `json:"authorityId"`
|
|
Days int `json:"days"`
|
|
Remark string `json:"remark"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
token, err := svc.CreateAPIToken(c.Request.Context(), req.UserID, req.AuthorityID, req.Days, req.Remark)
|
|
if err != nil {
|
|
fail(c, "签发失败:"+err.Error())
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"token": token}, "签发成功")
|
|
})
|
|
router.POST("/getApiTokenList", func(c *gin.Context) {
|
|
var req struct {
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
UserID uint `json:"userId"`
|
|
Status *bool `json:"status"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
items, total, err := svc.APITokens(c.Request.Context(), req.Page, req.PageSize, req.UserID, req.Status)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
|
|
})
|
|
router.POST("/deleteApiToken", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DisableAPIToken(c.Request.Context(), req.ID); err != nil {
|
|
fail(c, "作废失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
}
|