初始化v1

This commit is contained in:
2025-08-03 00:11:15 +08:00
commit b88c8b8afa
171 changed files with 22353 additions and 0 deletions

View File

@@ -0,0 +1,234 @@
package auth
import (
"context"
"errors"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"nl-video-api/internal/dao"
"nl-video-api/internal/model/entity"
)
// PermissionService 权限服务
type PermissionService struct{}
var Permission = &PermissionService{}
// PermissionCreateReq 创建权限请求
type PermissionCreateReq struct {
Name string `json:"name" v:"required|length:2,50#权限名称不能为空|权限名称长度为2-50位"`
Slug string `json:"slug" v:"required|length:2,100#权限标识不能为空|权限标识长度为2-100位"`
Type string `json:"type" v:"required|in:1,2,3#权限类型不能为空|权限类型只能是1,2,3"`
ParentId int `json:"parent_id" v:"min:0#父级ID不能小于0"`
Path string `json:"path"` // 路由路径
Component string `json:"component"` // 组件路径
Icon string `json:"icon"` // 图标
ApiPath string `json:"api_path"` // API路径
Method string `json:"method"` // 请求方法
Description string `json:"description"` // 权限描述
Sort int `json:"sort"` // 排序
Status int `json:"status" v:"in:0,1#状态只能为0或1"`
}
// PermissionUpdateReq 更新权限请求
type PermissionUpdateReq struct {
Id int `json:"id" v:"required|min:1#权限ID不能为空"`
Name string `json:"name" v:"required|length:2,50#权限名称不能为空|权限名称长度为2-50位"`
Slug string `json:"slug" v:"required|length:2,100#权限标识不能为空|权限标识长度为2-100位"`
Type string `json:"type" v:"required|in:1,2,3#权限类型不能为空|权限类型只能是1,2,3"`
ParentId int `json:"parent_id" v:"min:0#父级ID不能小于0"`
Path string `json:"path"` // 路由路径
Component string `json:"component"` // 组件路径
Icon string `json:"icon"` // 图标
ApiPath string `json:"api_path"` // API路径
Method string `json:"method"` // 请求方法
Description string `json:"description"` // 权限描述
Sort int `json:"sort"` // 排序
Status int `json:"status" v:"in:0,1#状态只能为0或1"`
}
// Create 创建权限
func (s *PermissionService) Create(ctx context.Context, req *PermissionCreateReq) (int, error) {
// 检查权限标识是否存在
exists, err := dao.Permission.CheckCodeExists(ctx, req.Slug, 0)
if err != nil {
return 0, err
}
if exists {
return 0, errors.New("权限标识已存在")
}
// 如果有父级ID,检查父级是否存在
if req.ParentId > 0 {
parent, err := dao.Permission.GetById(ctx, req.ParentId)
if err != nil {
return 0, err
}
if parent == nil {
return 0, errors.New("父级权限不存在")
}
}
// 创建权限数据
permission := &entity.Permission{
Name: req.Name,
Slug: req.Slug,
Type: gconv.Int(req.Type),
ParentId: req.ParentId,
Path: req.Path,
Component: req.Component,
Icon: req.Icon,
ApiPath: req.ApiPath,
Method: req.Method,
Description: req.Description,
Sort: req.Sort,
Status: req.Status,
CreatedAt: gtime.Now().String(),
UpdatedAt: gtime.Now().String(),
}
return dao.Permission.Create(ctx, permission)
}
// Update 更新权限
func (s *PermissionService) Update(ctx context.Context, req *PermissionUpdateReq) error {
// 检查权限是否存在
permission, err := dao.Permission.GetById(ctx, req.Id)
if err != nil {
return err
}
if permission == nil {
return errors.New("权限不存在")
}
// 检查权限标识是否存在(排除自己)
exists, err := dao.Permission.CheckCodeExists(ctx, req.Slug, req.Id)
if err != nil {
return err
}
if exists {
return errors.New("权限标识已存在")
}
// 如果有父级ID,检查父级是否存在
if req.ParentId > 0 {
parent, err := dao.Permission.GetById(ctx, req.ParentId)
if err != nil {
return err
}
if parent == nil {
return errors.New("父级权限不存在")
}
// 不能将自己设为父级
if req.ParentId == req.Id {
return errors.New("不能将自己设为父级")
}
}
// 更新数据
updateData := g.Map{
"name": req.Name,
"slug": req.Slug,
"type": gconv.Int(req.Type),
"parent_id": req.ParentId,
"path": req.Path,
"component": req.Component,
"icon": req.Icon,
"api_path": req.ApiPath,
"method": req.Method,
"description": req.Description,
"sort": req.Sort,
"status": req.Status,
"updated_at": gtime.Now().String(),
}
return dao.Permission.Update(ctx, req.Id, updateData)
}
// GetById 获取权限详情
func (s *PermissionService) GetById(ctx context.Context, id int) (*entity.Permission, error) {
return dao.Permission.GetById(ctx, id)
}
// GetList 获取权限列表
func (s *PermissionService) GetList(ctx context.Context, req *dao.PermissionListReq) ([]*entity.Permission, int, error) {
return dao.Permission.GetList(ctx, req)
}
// GetTree 获取权限树形结构
func (s *PermissionService) GetTree(ctx context.Context) ([]*entity.Permission, error) {
return dao.Permission.GetTree(ctx)
}
// Delete 删除权限
func (s *PermissionService) Delete(ctx context.Context, id int) error {
// 检查权限是否存在
permission, err := dao.Permission.GetById(ctx, id)
if err != nil {
return err
}
if permission == nil {
return errors.New("权限不存在")
}
// 检查是否有子权限
children, err := dao.Permission.GetByParentId(ctx, id)
if err != nil {
return err
}
if len(children) > 0 {
return errors.New("存在子权限,无法删除")
}
return dao.Permission.Delete(ctx, id)
}
// GetMenuPermissions 获取菜单权限
func (s *PermissionService) GetMenuPermissions(ctx context.Context) ([]*entity.Permission, error) {
return dao.Permission.GetMenuPermissions(ctx)
}
// GetApiPermissions 获取API权限
func (s *PermissionService) GetApiPermissions(ctx context.Context) ([]*entity.Permission, error) {
return dao.Permission.GetApiPermissions(ctx)
}
// GetUserPermissions 获取用户权限
func (s *PermissionService) GetUserPermissions(ctx context.Context, userId int) ([]*entity.Permission, error) {
return dao.Permission.GetUserPermissions(ctx, userId)
}
// CheckUserPermission 检查用户是否有指定权限
func (s *PermissionService) CheckUserPermission(ctx context.Context, userId int, permissionSlug string) (bool, error) {
permissions, err := s.GetUserPermissions(ctx, userId)
if err != nil {
return false, err
}
for _, permission := range permissions {
if permission.Slug == permissionSlug {
return true, nil
}
}
return false, nil
}
// CheckApiPermission 检查API权限
func (s *PermissionService) CheckApiPermission(ctx context.Context, userId int, apiPath, method string) (bool, error) {
permissions, err := s.GetUserPermissions(ctx, userId)
if err != nil {
return false, err
}
for _, permission := range permissions {
if permission.Type == 3 && permission.ApiPath == apiPath && permission.Method == method {
return true, nil
}
}
return false, nil
}

View File

@@ -0,0 +1,328 @@
package auth
import (
"context"
"errors"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"nl-video-api/internal/dao"
"nl-video-api/internal/model/entity"
)
// RoleService 角色服务
type RoleService struct{}
var Role = &RoleService{}
// RoleCreateReq 创建角色请求
type RoleCreateReq struct {
Name string `json:"name" v:"required|length:2,50#角色名称不能为空|角色名称长度为2-50位"`
Slug string `json:"slug" v:"required|length:2,50#角色标识不能为空|角色标识长度为2-50位"`
Level int `json:"level" v:"required|min:1,max:10#角色等级不能为空|角色等级范围1-10"`
Description string `json:"description"` // 角色描述
Sort int `json:"sort"` // 排序
Status int `json:"status" v:"in:0,1#状态只能为0或1"`
IsSystem int `json:"is_system" v:"in:0,1#系统角色标识只能为0或1"`
}
// RoleUpdateReq 更新角色请求
type RoleUpdateReq struct {
Id int `json:"id" v:"required|min:1#角色ID不能为空"`
Name string `json:"name" v:"required|length:2,50#角色名称不能为空|角色名称长度为2-50位"`
Slug string `json:"slug" v:"required|length:2,50#角色标识不能为空|角色标识长度为2-50位"`
Level int `json:"level" v:"required|min:1,max:10#角色等级不能为空|角色等级范围1-10"`
Description string `json:"description"` // 角色描述
Sort int `json:"sort"` // 排序
Status int `json:"status" v:"in:0,1#状态只能为0或1"`
IsSystem int `json:"is_system" v:"in:0,1#系统角色标识只能为0或1"`
}
// RolePermissionReq 角色权限分配请求
type RolePermissionReq struct {
RoleId int `json:"role_id" v:"required|min:1#角色ID不能为空"`
PermissionIds []int `json:"permission_ids"` // 权限ID列表
}
// Create 创建角色
func (s *RoleService) Create(ctx context.Context, req *RoleCreateReq) (int, error) {
// 检查角色标识是否存在
exists, err := dao.Role.CheckCodeExists(ctx, req.Slug, 0)
if err != nil {
return 0, err
}
if exists {
return 0, errors.New("角色标识已存在")
}
// 创建角色数据
role := &entity.Role{
Name: req.Name,
Slug: req.Slug,
Level: req.Level,
Description: req.Description,
Sort: req.Sort,
Status: req.Status,
IsSystem: req.IsSystem,
CreatedAt: gtime.Now().String(),
UpdatedAt: gtime.Now().String(),
}
return dao.Role.Create(ctx, role)
}
// Update 更新角色
func (s *RoleService) Update(ctx context.Context, req *RoleUpdateReq) error {
// 检查角色是否存在
role, err := dao.Role.GetById(ctx, req.Id)
if err != nil {
return err
}
if role == nil {
return errors.New("角色不存在")
}
// 系统角色不允许修改标识和系统标识
if role.IsSystem == 1 {
if req.Slug != role.Slug {
return errors.New("系统角色不允许修改标识")
}
if req.IsSystem != role.IsSystem {
return errors.New("系统角色不允许修改系统标识")
}
}
// 检查角色标识是否存在(排除自己)
exists, err := dao.Role.CheckCodeExists(ctx, req.Slug, req.Id)
if err != nil {
return err
}
if exists {
return errors.New("角色标识已存在")
}
// 更新数据
updateData := g.Map{
"name": req.Name,
"slug": req.Slug,
"level": req.Level,
"description": req.Description,
"sort": req.Sort,
"status": req.Status,
"is_system": req.IsSystem,
"updated_at": gtime.Now().String(),
}
return dao.Role.Update(ctx, req.Id, updateData)
}
// GetById 获取角色详情
func (s *RoleService) GetById(ctx context.Context, id int) (*entity.Role, error) {
return dao.Role.GetById(ctx, id)
}
// GetList 获取角色列表
func (s *RoleService) GetList(ctx context.Context, req *dao.RoleListReq) ([]*entity.Role, int, error) {
return dao.Role.GetList(ctx, req)
}
// GetAll 获取所有角色
func (s *RoleService) GetAll(ctx context.Context) ([]*entity.Role, error) {
return dao.Role.GetAll(ctx)
}
// Delete 删除角色
func (s *RoleService) Delete(ctx context.Context, id int) error {
// 检查角色是否存在
role, err := dao.Role.GetById(ctx, id)
if err != nil {
return err
}
if role == nil {
return errors.New("角色不存在")
}
// 系统角色不允许删除
if role.IsSystem == 1 {
return errors.New("系统角色不允许删除")
}
// 检查是否有管理员使用该角色
// 这里需要查询管理员表,暂时跳过
return dao.Role.Delete(ctx, id)
}
// AssignPermissions 为角色分配权限
func (s *RoleService) AssignPermissions(ctx context.Context, req *RolePermissionReq) error {
// 检查角色是否存在
role, err := dao.Role.GetById(ctx, req.RoleId)
if err != nil {
return err
}
if role == nil {
return errors.New("角色不存在")
}
// 验证权限ID是否有效
if len(req.PermissionIds) > 0 {
permissions, err := dao.Permission.GetPermissionsByIds(ctx, req.PermissionIds)
if err != nil {
return err
}
if len(permissions) != len(req.PermissionIds) {
return errors.New("存在无效的权限ID")
}
}
return dao.Role.AssignPermissions(ctx, req.RoleId, req.PermissionIds)
}
// GetRolePermissions 获取角色权限
func (s *RoleService) GetRolePermissions(ctx context.Context, roleId int) ([]*entity.Permission, error) {
// 检查角色是否存在
role, err := dao.Role.GetById(ctx, roleId)
if err != nil {
return nil, err
}
if role == nil {
return nil, errors.New("角色不存在")
}
return dao.Role.GetRolePermissions(ctx, roleId)
}
// GetRolePermissionIds 获取角色权限ID列表
func (s *RoleService) GetRolePermissionIds(ctx context.Context, roleId int) ([]int, error) {
permissions, err := s.GetRolePermissions(ctx, roleId)
if err != nil {
return nil, err
}
var permissionIds []int
for _, permission := range permissions {
permissionIds = append(permissionIds, permission.Id)
}
return permissionIds, nil
}
// BatchUpdateStatus 批量更新角色状态
func (s *RoleService) BatchUpdateStatus(ctx context.Context, ids []int, status int) error {
if len(ids) == 0 {
return errors.New("请选择要操作的角色")
}
// 检查是否包含系统角色
roles, err := dao.Role.GetRolesByIds(ctx, ids)
if err != nil {
return err
}
for _, role := range roles {
if role.IsSystem == 1 {
return errors.New("不能修改系统角色状态")
}
}
// 批量更新
successCount := 0
for _, id := range ids {
updateData := g.Map{
"status": status,
"updated_at": gtime.Now().String(),
}
if err := dao.Role.Update(ctx, id, updateData); err != nil {
g.Log().Errorf(ctx, "批量更新角色状态失败: ID=%d, 错误=%v", id, err)
} else {
successCount++
}
}
if successCount == 0 {
return errors.New("批量更新失败")
}
return nil
}
// CopyRole 复制角色
func (s *RoleService) CopyRole(ctx context.Context, sourceId int, newName, newSlug string) (int, error) {
// 检查源角色是否存在
sourceRole, err := dao.Role.GetById(ctx, sourceId)
if err != nil {
return 0, err
}
if sourceRole == nil {
return 0, errors.New("源角色不存在")
}
// 检查新角色标识是否存在
exists, err := dao.Role.CheckCodeExists(ctx, newSlug, 0)
if err != nil {
return 0, err
}
if exists {
return 0, errors.New("角色标识已存在")
}
// 创建新角色
newRole := &entity.Role{
Name: newName,
Slug: newSlug,
Level: sourceRole.Level,
Description: sourceRole.Description + " (复制)",
Sort: sourceRole.Sort,
Status: 1, // 默认启用
IsSystem: 0, // 复制的角色不是系统角色
CreatedAt: gtime.Now().String(),
UpdatedAt: gtime.Now().String(),
}
newRoleId, err := dao.Role.Create(ctx, newRole)
if err != nil {
return 0, err
}
// 复制权限
sourcePermissions, err := dao.Role.GetRolePermissions(ctx, sourceId)
if err != nil {
return newRoleId, err // 角色创建成功,但权限复制失败
}
if len(sourcePermissions) > 0 {
var permissionIds []int
for _, permission := range sourcePermissions {
permissionIds = append(permissionIds, permission.Id)
}
err = dao.Role.AssignPermissions(ctx, newRoleId, permissionIds)
if err != nil {
g.Log().Errorf(ctx, "复制角色权限失败: 新角色ID=%d, 错误=%v", newRoleId, err)
}
}
return newRoleId, nil
}
// GetRolesByLevel 根据等级获取角色
func (s *RoleService) GetRolesByLevel(ctx context.Context, level int) ([]*entity.Role, error) {
req := &dao.RoleListReq{
Level: level,
Status: 1, // 只获取启用的角色
Page: 0, // 不分页
PageSize: 0,
}
roles, _, err := dao.Role.GetList(ctx, req)
return roles, err
}
// CheckRoleExists 检查角色是否存在
func (s *RoleService) CheckRoleExists(ctx context.Context, id int) (bool, error) {
role, err := dao.Role.GetById(ctx, id)
if err != nil {
return false, err
}
return role != nil, nil
}