Files
sundynix-radio-be/api/v1/system/sys_role.go
T
2026-02-27 13:54:01 +08:00

164 lines
4.5 KiB
Go

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"
)
type RoleApi struct {
}
// SaveRole
// @tags 角色管理
// @Summary 创建角色
// @Security ApiKeyAuth
// @accept json
// @Produce json
// @Param data body system.Role true "角色信息"
// @Success 200 {object} response.Response
// @Router /role/save [post]
func (a *RoleApi) SaveRole(context *gin.Context) {
var role system.Role
err := context.ShouldBindJSON(&role)
if err != nil {
response.FailWithMsg("参数错误"+err.Error(), context)
return
}
err = roleService.SaveRole(role)
if err != nil {
global.Logger.Error("保存角色失败!", zap.Error(err))
response.FailWithMsg(err.Error(), context)
return
}
response.OkWithMsg("保存角色成功!", context)
}
// UpdateRole
// @tags 角色管理
// @Summary 修改角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system.Role true "角色ID"
// @Success 200 {object} response.Response"
// @Router /role/update [post]
func (a *RoleApi) UpdateRole(context *gin.Context) {
var role system.Role
err := context.ShouldBindJSON(&role)
if err != nil {
response.FailWithMsg(err.Error(), context)
return
}
err = roleService.UpdateRole(role)
if err != nil {
global.Logger.Error("更新角色失败!", zap.Error(err))
response.FailWithMsg(err.Error(), context)
return
}
response.OkWithMsg("更新角色成功!", context)
}
// GetRoleList
// @tags 角色管理
// @Summary 获取角色列表
// @Description 获取角色列表
// @Accept application/json
// @Produce application/json
// @Param data body systemreq.GetRoleList true "页码, 每页大小, 搜索条件"
// @success 200 {object} response.Response{data=response.PageResult,msg=string} "获取角色列表,返回包括列表,总数,页码,每页大小"
// @Router /role/getRoleList [post]
func (a *RoleApi) GetRoleList(c *gin.Context) {
var pageInfo systemreq.GetRoleList
err := c.ShouldBindJSON(&pageInfo)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
list, total, err := roleService.GetRoleList(pageInfo)
if err != nil {
global.Logger.Error("获取角色列表失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
}
response.OkWithData(response.PageResult{
List: list,
Total: total,
Page: pageInfo.Current,
PageSize: pageInfo.PageSize,
}, c)
}
// Delete
// @Tags 角色管理
// @Summary 删除角色
// @Description 删除角色
// @Accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除角色"
// @Success 200 {object} response.Response{msg=string} "删除角色"
// @Router /role/delete [post]
func (a *RoleApi) Delete(context *gin.Context) {
var ids request.IdsReq
err := context.ShouldBindJSON(&ids)
if err != nil {
response.FailWithMsg(err.Error(), context)
return
}
err = roleService.DeleteRoleByIds(ids)
if err != nil {
global.Logger.Error("删除角色失败!", zap.Error(err))
response.FailWithMsg(err.Error(), context)
return
}
response.OkWithMsg("删除角色成功!", context)
}
// Detail
// @Tags 角色管理
// @Summary 角色详情
// @Security ApiKeyAuth
// @Produce application/json
// @Param id query string true "id"
// @Success 200 {object} response.Response{data=system.Role} "角色详情"
// @Router /role/detail [get]
func (a *RoleApi) Detail(context *gin.Context) {
id := context.Query("id")
role, err := roleService.GetRoleById(id)
if err != nil {
global.Logger.Error("获取角色详情失败!", zap.Error(err))
response.FailWithMsg(err.Error(), context)
return
}
response.OkWithData(role, context)
}
// GrantMenu
// @tags 角色管理
// @Summary 授权菜单给角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body systemreq.GrantMenu true "授权菜单给角色"
// @success 200 {object} response.Response "授权菜单给角色"
// @Router /role/grantMenu [post]
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)
}