246 lines
5.8 KiB
Go
246 lines
5.8 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"
|
||
|
|
)
|
||
|
|
|
||
|
|
// PermissionController 权限控制器
|
||
|
|
type PermissionController struct{}
|
||
|
|
|
||
|
|
var Permission = &PermissionController{}
|
||
|
|
|
||
|
|
// Create 创建权限
|
||
|
|
func (c *PermissionController) Create(r *ghttp.Request) {
|
||
|
|
var req *auth.PermissionCreateReq
|
||
|
|
if err := r.Parse(&req); err != nil {
|
||
|
|
response.Error(r, response.CodeInvalidParam, "参数解析失败: "+err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
id, err := auth.Permission.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 *PermissionController) Update(r *ghttp.Request) {
|
||
|
|
var req *auth.PermissionUpdateReq
|
||
|
|
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.Permission.Update(r.Context(), req)
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, "更新成功")
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetById 获取权限详情
|
||
|
|
func (c *PermissionController) 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
|
||
|
|
}
|
||
|
|
|
||
|
|
permission, err := auth.Permission.GetById(r.Context(), id)
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if permission == nil {
|
||
|
|
response.Error(r, response.CodeNotFound, "权限不存在")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, permission)
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetList 获取权限列表
|
||
|
|
func (c *PermissionController) GetList(r *ghttp.Request) {
|
||
|
|
var req *dao.PermissionListReq
|
||
|
|
if err := r.Parse(&req); err != nil {
|
||
|
|
response.Error(r, response.CodeInvalidParam, "参数解析失败: "+err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
permissions, total, err := auth.Permission.GetList(r.Context(), req)
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, g.Map{
|
||
|
|
"list": permissions,
|
||
|
|
"total": total,
|
||
|
|
"page": req.Page,
|
||
|
|
"page_size": req.PageSize,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetTree 获取权限树形结构
|
||
|
|
func (c *PermissionController) GetTree(r *ghttp.Request) {
|
||
|
|
permissions, err := auth.Permission.GetTree(r.Context())
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, g.Map{
|
||
|
|
"tree": permissions,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// Delete 删除权限
|
||
|
|
func (c *PermissionController) 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.Permission.Delete(r.Context(), id)
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, "删除成功")
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetMenuPermissions 获取菜单权限
|
||
|
|
func (c *PermissionController) GetMenuPermissions(r *ghttp.Request) {
|
||
|
|
permissions, err := auth.Permission.GetMenuPermissions(r.Context())
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, g.Map{
|
||
|
|
"list": permissions,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetApiPermissions 获取API权限
|
||
|
|
func (c *PermissionController) GetApiPermissions(r *ghttp.Request) {
|
||
|
|
permissions, err := auth.Permission.GetApiPermissions(r.Context())
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, g.Map{
|
||
|
|
"list": permissions,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetUserPermissions 获取用户权限
|
||
|
|
func (c *PermissionController) GetUserPermissions(r *ghttp.Request) {
|
||
|
|
userIdStr := r.Get("user_id").String()
|
||
|
|
userId, err := strconv.Atoi(userIdStr)
|
||
|
|
if err != nil || userId <= 0 {
|
||
|
|
response.Error(r, response.CodeInvalidParam, "用户ID无效")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
permissions, err := auth.Permission.GetUserPermissions(r.Context(), userId)
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, g.Map{
|
||
|
|
"list": permissions,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// CheckPermission 检查权限
|
||
|
|
func (c *PermissionController) CheckPermission(r *ghttp.Request) {
|
||
|
|
userIdStr := r.Get("user_id").String()
|
||
|
|
userId, err := strconv.Atoi(userIdStr)
|
||
|
|
if err != nil || userId <= 0 {
|
||
|
|
response.Error(r, response.CodeInvalidParam, "用户ID无效")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
permissionCode := r.Get("permission_code").String()
|
||
|
|
if permissionCode == "" {
|
||
|
|
response.Error(r, response.CodeInvalidParam, "权限编码不能为空")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
hasPermission, err := auth.Permission.CheckUserPermission(r.Context(), userId, permissionCode)
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, g.Map{
|
||
|
|
"has_permission": hasPermission,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// CheckApiPermission 检查API权限
|
||
|
|
func (c *PermissionController) CheckApiPermission(r *ghttp.Request) {
|
||
|
|
userIdStr := r.Get("user_id").String()
|
||
|
|
userId, err := strconv.Atoi(userIdStr)
|
||
|
|
if err != nil || userId <= 0 {
|
||
|
|
response.Error(r, response.CodeInvalidParam, "用户ID无效")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
apiPath := r.Get("api_path").String()
|
||
|
|
if apiPath == "" {
|
||
|
|
response.Error(r, response.CodeInvalidParam, "API路径不能为空")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
method := r.Get("method").String()
|
||
|
|
if method == "" {
|
||
|
|
response.Error(r, response.CodeInvalidParam, "请求方法不能为空")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
hasPermission, err := auth.Permission.CheckApiPermission(r.Context(), userId, apiPath, method)
|
||
|
|
if err != nil {
|
||
|
|
response.Error(r, response.CodeError, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(r, g.Map{
|
||
|
|
"has_permission": hasPermission,
|
||
|
|
})
|
||
|
|
}
|