271 lines
6.3 KiB
Go
271 lines
6.3 KiB
Go
|
|
package admin
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"strconv"
|
|||
|
|
|
|||
|
|
"github.com/gogf/gf/v2/frame/g"
|
|||
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|||
|
|
"nl-video-api/internal/dao"
|
|||
|
|
"nl-video-api/internal/service/auth"
|
|||
|
|
"nl-video-api/utility/response"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// RoleController 角色控制器
|
|||
|
|
type RoleController struct{}
|
|||
|
|
|
|||
|
|
var Role = &RoleController{}
|
|||
|
|
|
|||
|
|
// Create 创建角色
|
|||
|
|
func (c *RoleController) Create(r *ghttp.Request) {
|
|||
|
|
var req *auth.RoleCreateReq
|
|||
|
|
if err := r.Parse(&req); err != nil {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "参数解析失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
id, err := auth.Role.Create(r.Context(), req)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, g.Map{
|
|||
|
|
"id": id,
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Update 更新角色
|
|||
|
|
func (c *RoleController) Update(r *ghttp.Request) {
|
|||
|
|
var req *auth.RoleUpdateReq
|
|||
|
|
if err := r.Parse(&req); err != nil {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "参数解析失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 从URL路径获取ID
|
|||
|
|
if req.Id == 0 {
|
|||
|
|
idStr := r.Get("id").String()
|
|||
|
|
if id, err := strconv.Atoi(idStr); err == nil {
|
|||
|
|
req.Id = id
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if req.Id <= 0 {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "角色ID无效")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
err := auth.Role.Update(r.Context(), req)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, "更新成功")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetById 获取角色详情
|
|||
|
|
func (c *RoleController) GetById(r *ghttp.Request) {
|
|||
|
|
idStr := r.Get("id").String()
|
|||
|
|
id, err := strconv.Atoi(idStr)
|
|||
|
|
if err != nil || id <= 0 {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "角色ID无效")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
role, err := auth.Role.GetById(r.Context(), id)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if role == nil {
|
|||
|
|
response.Error(r, response.CodeNotFound, "角色不存在")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, role)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetList 获取角色列表
|
|||
|
|
func (c *RoleController) GetList(r *ghttp.Request) {
|
|||
|
|
var req *dao.RoleListReq
|
|||
|
|
if err := r.Parse(&req); err != nil {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "参数解析失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
roles, total, err := auth.Role.GetList(r.Context(), req)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, g.Map{
|
|||
|
|
"list": roles,
|
|||
|
|
"total": total,
|
|||
|
|
"page": req.Page,
|
|||
|
|
"page_size": req.PageSize,
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetAll 获取所有角色
|
|||
|
|
func (c *RoleController) GetAll(r *ghttp.Request) {
|
|||
|
|
roles, err := auth.Role.GetAll(r.Context())
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, g.Map{
|
|||
|
|
"list": roles,
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Delete 删除角色
|
|||
|
|
func (c *RoleController) Delete(r *ghttp.Request) {
|
|||
|
|
idStr := r.Get("id").String()
|
|||
|
|
id, err := strconv.Atoi(idStr)
|
|||
|
|
if err != nil || id <= 0 {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "角色ID无效")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
err = auth.Role.Delete(r.Context(), id)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, "删除成功")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// AssignPermissions 为角色分配权限
|
|||
|
|
func (c *RoleController) AssignPermissions(r *ghttp.Request) {
|
|||
|
|
var req *auth.RolePermissionReq
|
|||
|
|
if err := r.Parse(&req); err != nil {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "参数解析失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 从URL路径获取角色ID
|
|||
|
|
if req.RoleId == 0 {
|
|||
|
|
idStr := r.Get("id").String()
|
|||
|
|
if id, err := strconv.Atoi(idStr); err == nil {
|
|||
|
|
req.RoleId = id
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if req.RoleId <= 0 {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "角色ID无效")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
err := auth.Role.AssignPermissions(r.Context(), req)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, "权限分配成功")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetRolePermissions 获取角色权限
|
|||
|
|
func (c *RoleController) GetRolePermissions(r *ghttp.Request) {
|
|||
|
|
idStr := r.Get("id").String()
|
|||
|
|
id, err := strconv.Atoi(idStr)
|
|||
|
|
if err != nil || id <= 0 {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "角色ID无效")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
permissions, err := auth.Role.GetRolePermissions(r.Context(), id)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 同时返回权限ID列表,方便前端处理
|
|||
|
|
permissionIds, _ := auth.Role.GetRolePermissionIds(r.Context(), id)
|
|||
|
|
|
|||
|
|
response.Success(r, g.Map{
|
|||
|
|
"permissions": permissions,
|
|||
|
|
"permission_ids": permissionIds,
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BatchUpdateStatus 批量更新角色状态
|
|||
|
|
func (c *RoleController) BatchUpdateStatus(r *ghttp.Request) {
|
|||
|
|
type BatchUpdateReq struct {
|
|||
|
|
Ids []int `json:"ids" v:"required#请选择要操作的角色"`
|
|||
|
|
Status int `json:"status" v:"required|in:0,1#状态只能为0或1"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var req BatchUpdateReq
|
|||
|
|
if err := r.Parse(&req); err != nil {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "参数解析失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if len(req.Ids) == 0 {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "请选择要操作的角色")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
err := auth.Role.BatchUpdateStatus(r.Context(), req.Ids, req.Status)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, g.Map{
|
|||
|
|
"count": len(req.Ids),
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CopyRole 复制角色
|
|||
|
|
func (c *RoleController) CopyRole(r *ghttp.Request) {
|
|||
|
|
type CopyRoleReq struct {
|
|||
|
|
SourceId int `json:"source_id" v:"required|min:1#源角色ID不能为空"`
|
|||
|
|
Name string `json:"name" v:"required|length:2,50#角色名称不能为空|角色名称长度为2-50位"`
|
|||
|
|
Code string `json:"code" v:"required|length:2,50#角色编码不能为空|角色编码长度为2-50位"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var req CopyRoleReq
|
|||
|
|
if err := r.Parse(&req); err != nil {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "参数解析失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
newRoleId, err := auth.Role.CopyRole(r.Context(), req.SourceId, req.Name, req.Code)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, g.Map{
|
|||
|
|
"id": newRoleId,
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetRolesByLevel 根据等级获取角色
|
|||
|
|
func (c *RoleController) GetRolesByLevel(r *ghttp.Request) {
|
|||
|
|
levelStr := r.Get("level").String()
|
|||
|
|
level, err := strconv.Atoi(levelStr)
|
|||
|
|
if err != nil || level <= 0 {
|
|||
|
|
response.Error(r, response.CodeInvalidParam, "角色等级无效")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
roles, err := auth.Role.GetRolesByLevel(r.Context(), level)
|
|||
|
|
if err != nil {
|
|||
|
|
response.Error(r, response.CodeError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(r, g.Map{
|
|||
|
|
"list": roles,
|
|||
|
|
"level": level,
|
|||
|
|
})
|
|||
|
|
}
|