package models import ( "time" ) // Resource 资源模型(系统所有可访问的资源) type Resource struct { ID uint `gorm:"primaryKey" json:"id"` TenantID uint `gorm:"not null;index:idx_tenant_resource" json:"tenant_id"` Name string `gorm:"size:100;not null" json:"name"` // 资源名称 DisplayName string `gorm:"size:200" json:"display_name"` Description string `gorm:"type:text" json:"description"` // 资源标识 Code string `gorm:"size:100;not null;uniqueIndex:idx_tenant_resource_code" json:"code"` // 资源代码 Type string `gorm:"size:50;not null" json:"type"` // api, page, button, data 等 Group string `gorm:"size:100" json:"group"` // 所属分组 // 权限配置 Actions []string `gorm:"type:jsonb" json:"actions"` // 允许的操作:create, read, update, delete, export 等 Path string `gorm:"size:500" json:"path"` // 资源路径或 API 端点 ParentID *uint `gorm:"index" json:"parent_id,omitempty"` // 父资源 ID // 状态 Status string `gorm:"size:20;default:'enabled'" json:"status"` // enabled, disabled IsSystem bool `gorm:"default:false" json:"is_system"` // 是否系统资源 // 资源层级 Level int `gorm:"default:0" json:"level"` // 层级深度 SortOrder int `gorm:"default:0" json:"sort_order"` // 排序 // 时间戳 CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt *time.Time `gorm:"index" json:"deleted_at,omitempty"` // 关联 Tenant Tenant `gorm:"foreignKey:TenantID" json:"tenant,omitempty"` Roles []Role `gorm:"many2many:role_resources;" json:"roles,omitempty"` Parent *Resource `gorm:"foreignKey:ParentID" json:"parent,omitempty"` Children []Resource `gorm:"foreignKey:ParentID" json:"children,omitempty"` } // Permission 权限检查辅助结构 type Permission struct { ResourceCode string `json:"resource_code"` Action string `json:"action"` // create, read, update, delete, manage 等 TenantID uint `json:"tenant_id,omitempty"` UserID uint `json:"user_id,omitempty"` } // HasPermission 检查用户是否有权限(需要在 handler 中实现) func HasPermission(userID, tenantID uint, resourceCode, action string) bool { // TODO: 实现权限检查逻辑 // 1. 查询用户角色 // 2. 查询角色资源 // 3. 检查资源是否包含该操作 return false }