135 lines
4.7 KiB
Go
135 lines
4.7 KiB
Go
package server
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func registerAuthorityRoutes(group *gin.RouterGroup, svc *service.AccessService) {
|
|
router := group.Group("/authority")
|
|
router.POST("/getAuthorityList", func(c *gin.Context) {
|
|
items, err := svc.Authorities(c.Request.Context())
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, items, "获取成功")
|
|
})
|
|
type authorityRequest struct {
|
|
AuthorityID uint `json:"authorityId"`
|
|
AuthorityName string `json:"authorityName"`
|
|
ParentID *uint `json:"parentId"`
|
|
DataScope int `json:"dataScope"`
|
|
DefaultRouter string `json:"defaultRouter"`
|
|
}
|
|
router.POST("/createAuthority", func(c *gin.Context) {
|
|
var req authorityRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.AuthorityID == 0 {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
value := &biz.Authority{AuthorityID: req.AuthorityID, AuthorityName: req.AuthorityName, ParentID: req.ParentID, DataScope: req.DataScope, DefaultRouter: req.DefaultRouter}
|
|
if err := svc.CreateAuthority(c.Request.Context(), value); err != nil {
|
|
fail(c, "创建失败:"+err.Error())
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"authority": gin.H{"authorityId": value.AuthorityID, "authorityName": value.AuthorityName, "parentId": value.ParentID, "dataScope": value.DataScope, "defaultRouter": value.DefaultRouter}}, "创建成功")
|
|
})
|
|
router.POST("/copyAuthority", func(c *gin.Context) {
|
|
var req struct {
|
|
Authority authorityRequest `json:"authority"`
|
|
OldAuthorityID uint `json:"oldAuthorityId"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil || req.Authority.AuthorityID == 0 || req.OldAuthorityID == 0 {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
value := &biz.Authority{AuthorityID: req.Authority.AuthorityID, AuthorityName: req.Authority.AuthorityName, ParentID: req.Authority.ParentID, DataScope: req.Authority.DataScope, DefaultRouter: req.Authority.DefaultRouter}
|
|
if err := svc.CopyAuthority(c.Request.Context(), req.OldAuthorityID, value); err != nil {
|
|
fail(c, "拷贝失败:"+err.Error())
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"authority": gin.H{"authorityId": value.AuthorityID, "authorityName": value.AuthorityName, "parentId": value.ParentID, "dataScope": value.DataScope, "defaultRouter": value.DefaultRouter}}, "拷贝成功")
|
|
})
|
|
router.PUT("/updateAuthority", func(c *gin.Context) {
|
|
var req authorityRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
value := &biz.Authority{AuthorityID: req.AuthorityID, AuthorityName: req.AuthorityName, ParentID: req.ParentID, DataScope: req.DataScope, DefaultRouter: req.DefaultRouter}
|
|
if err := svc.UpdateAuthority(c.Request.Context(), value); err != nil {
|
|
fail(c, "更新失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"authority": gin.H{"authorityId": value.AuthorityID, "authorityName": value.AuthorityName, "parentId": value.ParentID, "dataScope": value.DataScope, "defaultRouter": value.DefaultRouter}}, "更新成功")
|
|
})
|
|
router.POST("/deleteAuthority", func(c *gin.Context) {
|
|
var req struct {
|
|
AuthorityID uint `json:"authorityId"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteAuthority(c.Request.Context(), req.AuthorityID); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.POST("/setRoleUsers", func(c *gin.Context) {
|
|
var req struct {
|
|
AuthorityID uint `json:"authorityId"`
|
|
UserIDs []uint `json:"userIds"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetAuthorityUsers(c.Request.Context(), req.AuthorityID, req.UserIDs); err != nil {
|
|
fail(c, "设置失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.GET("/getUsersByAuthority", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("authorityId"), 10, 64)
|
|
ids, err := svc.AuthorityUserIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, ids, "获取成功")
|
|
})
|
|
router.POST("/setDataScope", func(c *gin.Context) {
|
|
var req struct {
|
|
AuthorityID uint `json:"authorityId"`
|
|
DataScope int `json:"dataScope"`
|
|
DepartmentIDs []uint `json:"deptIds"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetDataScope(c.Request.Context(), req.AuthorityID, req.DataScope, req.DepartmentIDs); err != nil {
|
|
fail(c, "设置失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.GET("/getDataScopeDepts", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("authorityId"), 10, 64)
|
|
ids, err := svc.DataScopeDepartmentIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, ids, "获取成功")
|
|
})
|
|
}
|