feat: RBAC 基本完成

This commit is contained in:
Blizzard
2025-09-14 21:58:44 +08:00
parent 2ec091bf59
commit 9be75d53fe
35 changed files with 615 additions and 87 deletions
+11 -5
View File
@@ -1,15 +1,16 @@
package system
import (
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"go.uber.org/zap"
"sundynix-go/global"
"sundynix-go/model/commom/response"
"sundynix-go/model/system"
systemReq "sundynix-go/model/system/request"
systemRes "sundynix-go/model/system/response"
"sundynix-go/utils"
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"go.uber.org/zap"
)
var store = base64Captcha.DefaultMemStore
@@ -26,7 +27,7 @@ func (a *AuthApi) Login(c *gin.Context) {
}
if l.CaptchaId != "" && l.Captcha != "" && store.Verify(l.CaptchaId, l.Captcha, true) {
u := &system.User{Account: l.Account, Password: l.Password}
user, err := UserService.Login(u)
user, err := userService.Login(u)
if err != nil {
global.Logger.Error("登录失败! 用户名不存在或者密码错误!", zap.Error(err))
response.FailWithMsg("用户名不存在或者密码错误", c)
@@ -38,6 +39,11 @@ func (a *AuthApi) Login(c *gin.Context) {
response.FailWithMsg("验证码错误", c)
}
// 登出
func (a *AuthApi) Logout(c *gin.Context) {
utils.ClearToken(c)
}
// Captcha api 生成验证码
func (u *AuthApi) Captcha(c *gin.Context) {
var driver = base64Captcha.DriverString{
@@ -45,7 +51,7 @@ func (u *AuthApi) Captcha(c *gin.Context) {
Width: 240,
NoiseCount: 2,
ShowLineOptions: 4,
Length: 6,
Length: 4,
Source: "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM",
}
+5 -3
View File
@@ -7,10 +7,12 @@ type ApiGroup struct {
UserApi
ClientApi
RoleApi
MenuApi
}
var (
UserService = service.ServiceGroupApp.SystemServiceGroup.UserService
ClientService = service.ServiceGroupApp.SystemServiceGroup.ClientService
RoleService = service.ServiceGroupApp.SystemServiceGroup.RoleService
userService = service.ServiceGroupApp.SystemServiceGroup.UserService
clientService = service.ServiceGroupApp.SystemServiceGroup.ClientService
roleService = service.ServiceGroupApp.SystemServiceGroup.RoleService
menuService = service.ServiceGroupApp.SystemServiceGroup.MenuService
)
+5 -5
View File
@@ -20,7 +20,7 @@ func (s *ClientApi) SaveClient(c *gin.Context) {
response.FailWithMsg(err.Error(), c)
return
}
err = ClientService.SaveClient(client)
err = clientService.SaveClient(client)
if err != nil {
global.Logger.Error("保存客户端失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
@@ -36,7 +36,7 @@ func (s *ClientApi) UpdateClient(c *gin.Context) {
response.FailWithMsg(err.Error(), c)
return
}
err = ClientService.UpdateClient(client)
err = clientService.UpdateClient(client)
if err != nil {
global.Logger.Error("更新客户端失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
@@ -52,7 +52,7 @@ func (s *ClientApi) GetClientList(c *gin.Context) {
response.FailWithMsg(err.Error(), c)
return
}
list, total, err := ClientService.GetClientList(pageInfo)
list, total, err := clientService.GetClientList(pageInfo)
if err != nil {
global.Logger.Error("获取客户端列表失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
@@ -73,7 +73,7 @@ func (s *ClientApi) Delete(c *gin.Context) {
response.FailWithMsg(err.Error(), c)
return
}
err = ClientService.DeleteClientByIds(ids)
err = clientService.DeleteClientByIds(ids)
if err != nil {
global.Logger.Error("删除客户端失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
@@ -89,7 +89,7 @@ func (s *ClientApi) Detail(c *gin.Context) {
response.FailWithMsg(err.Error(), c)
return
}
client, err := ClientService.GetClientById(idInfo.ID)
client, err := clientService.GetClientById(idInfo.ID)
if err != nil {
global.Logger.Error("获取客户端详情失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
+100
View File
@@ -0,0 +1,100 @@
package system
import (
"sundynix-go/global"
"sundynix-go/model/commom/response"
"sundynix-go/model/system"
systemReq "sundynix-go/model/system/request"
"sundynix-go/utils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type MenuApi struct {
}
func (m *MenuApi) SaveMenu(c *gin.Context) {
var menu system.Menu
err := c.ShouldBindJSON(&menu)
if err != nil {
response.FailWithMsg("参数错误:"+err.Error(), c)
return
}
if err = menuService.SaveMenu(menu); err != nil {
global.Logger.Error("保存菜单失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
} else {
response.OkWithMsg("保存菜单成功!", c)
}
}
func (m *MenuApi) UpdateMenu(c *gin.Context) {
var menu system.Menu
err := c.ShouldBindJSON(&menu)
if err != nil {
response.FailWithMsg("参数错误:"+err.Error(), c)
return
}
if err = menuService.UpdateMenu(&menu); err != nil {
global.Logger.Error("更新菜单失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
} else {
response.OkWithMsg("更新菜单成功!", c)
}
}
func (m *MenuApi) DeleteMenu(c *gin.Context) {
id := c.Query("id")
err := menuService.DeleteMenu(id)
if err != nil {
global.Logger.Error("删除菜单失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("删除菜单成功!", c)
}
func (m *MenuApi) Detail(c *gin.Context) {
id := c.Query("id")
menu, err := menuService.GetMenuById(id)
if err != nil {
global.Logger.Error("获取菜单详情失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(menu, c)
}
func (m *MenuApi) GetAllMenuTree(c *gin.Context) {
var param systemReq.GetMenuTree
err := c.ShouldBindJSON(&param)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
menus, err := menuService.GetAllMenuTree(param.Category, param.ParentId)
if err != nil {
global.Logger.Error("获取菜单树结构失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(menus, c)
}
func (m *MenuApi) GetUserMenuTree(c *gin.Context) {
}
func (m *MenuApi) Route(c *gin.Context) {
userId := utils.GetUserId(c)
routes, err := menuService.GetUserRoutes(userId)
if err != nil {
global.Logger.Error("获取用户菜单失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(routes, c)
}
+26 -14
View File
@@ -1,13 +1,14 @@
package system
import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"sundynix-go/global"
"sundynix-go/model/commom/request"
"sundynix-go/model/commom/response"
"sundynix-go/model/system"
systemreq "sundynix-go/model/system/request"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type RoleApi struct {
@@ -17,10 +18,10 @@ func (a *RoleApi) SaveRole(context *gin.Context) {
var role system.Role
err := context.ShouldBindJSON(&role)
if err != nil {
response.FailWithMsg(err.Error(), context)
response.FailWithMsg("参数错误"+err.Error(), context)
return
}
err = RoleService.SaveRole(role)
err = roleService.SaveRole(role)
if err != nil {
global.Logger.Error("保存角色失败!", zap.Error(err))
response.FailWithMsg(err.Error(), context)
@@ -36,7 +37,7 @@ func (a *RoleApi) UpdateRole(context *gin.Context) {
response.FailWithMsg(err.Error(), context)
return
}
err = RoleService.UpdateRole(role)
err = roleService.UpdateRole(role)
if err != nil {
global.Logger.Error("更新角色失败!", zap.Error(err))
response.FailWithMsg(err.Error(), context)
@@ -52,7 +53,7 @@ func (a *RoleApi) GetRoleList(c *gin.Context) {
response.FailWithMsg(err.Error(), c)
return
}
list, total, err := RoleService.GetRoleList(pageInfo)
list, total, err := roleService.GetRoleList(pageInfo)
if err != nil {
global.Logger.Error("获取角色列表失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
@@ -72,7 +73,7 @@ func (a *RoleApi) Delete(context *gin.Context) {
response.FailWithMsg(err.Error(), context)
return
}
err = RoleService.DeleteRoleByIds(ids)
err = roleService.DeleteRoleByIds(ids)
if err != nil {
global.Logger.Error("删除角色失败!", zap.Error(err))
response.FailWithMsg(err.Error(), context)
@@ -82,13 +83,8 @@ func (a *RoleApi) Delete(context *gin.Context) {
}
func (a *RoleApi) Detail(context *gin.Context) {
var idInfo request.GetById
err := context.ShouldBindJSON(&idInfo)
if err != nil {
response.FailWithMsg(err.Error(), context)
return
}
role, err := RoleService.GetRoleById(idInfo.ID)
id := context.Query("id")
role, err := roleService.GetRoleById(id)
if err != nil {
global.Logger.Error("获取角色详情失败!", zap.Error(err))
response.FailWithMsg(err.Error(), context)
@@ -96,3 +92,19 @@ func (a *RoleApi) Detail(context *gin.Context) {
}
response.OkWithData(role, context)
}
func (a *RoleApi) GrantMenu(c *gin.Context) {
var grantMenu systemreq.GrantMenu
err := c.ShouldBindJSON(&grantMenu)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
err = roleService.GrantMenu(grantMenu.RoleId, grantMenu.MenuIds)
if err != nil {
global.Logger.Error("授权菜单失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("授权菜单成功!", c)
}
+96 -4
View File
@@ -1,16 +1,49 @@
package system
import (
"sundynix-go/global"
"sundynix-go/model/commom/request"
"sundynix-go/model/commom/response"
"sundynix-go/model/system"
systemReq "sundynix-go/model/system/request"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"sundynix-go/global"
"sundynix-go/model/commom/response"
systemReq "sundynix-go/model/system/request"
)
type UserApi struct {
}
func (u *UserApi) SaveUser(c *gin.Context) {
var user system.User
err := c.ShouldBindJSON(&user)
if err != nil {
response.FailWithMsg("参数错误:"+err.Error(), c)
return
}
if err = userService.SaveUser(user); err != nil {
global.Logger.Error("保存用户失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
} else {
response.OkWithMsg("保存用户成功!", c)
}
}
func (u *UserApi) UpdateUser(c *gin.Context) {
var user system.User
err := c.ShouldBindJSON(&user)
if err != nil {
response.FailWithMsg("参数错误:"+err.Error(), c)
return
}
if err = userService.UpdateUser(&user); err != nil {
global.Logger.Error("更新用户失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
} else {
response.OkWithMsg("更新用户成功!", c)
}
}
func (u *UserApi) GetUserList(c *gin.Context) {
var pageInfo systemReq.GetUserList
err := c.ShouldBindJSON(&pageInfo)
@@ -18,7 +51,7 @@ func (u *UserApi) GetUserList(c *gin.Context) {
response.FailWithMsg(err.Error(), c)
return
}
list, total, err := UserService.GetUserList(pageInfo)
list, total, err := userService.GetUserList(pageInfo)
if err != nil {
global.Logger.Error("获取用户列表失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
@@ -31,3 +64,62 @@ func (u *UserApi) GetUserList(c *gin.Context) {
PageSize: pageInfo.PageSize,
}, c)
}
func (u *UserApi) Delete(c *gin.Context) {
var ids request.IdsReq
err := c.ShouldBindJSON(&ids)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
err = userService.DeleteUserByIds(ids)
if err != nil {
global.Logger.Error("删除用户失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("删除用户成功!", c)
}
func (u *UserApi) Detail(c *gin.Context) {
id := c.Query("id")
user, err := userService.GetUserById(id)
if err != nil {
global.Logger.Error("获取用户详情失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(user, c)
}
func (u *UserApi) ChangePassword(c *gin.Context) {
var changePwd systemReq.ChangePwd
err := c.ShouldBindJSON(&changePwd)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
err = userService.ChangePassword(changePwd.Id, changePwd.NewPwd)
if err != nil {
global.Logger.Error("修改密码失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("修改密码成功", c)
}
func (u *UserApi) GrantRole(c *gin.Context) {
var grantRole systemReq.GrantRole
err := c.ShouldBindJSON(&grantRole)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
err = roleService.GrantRole(grantRole.UserId, grantRole.RoleIds)
if err != nil {
global.Logger.Error("授权角色失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("授权角色成功!", c)
}