feat():learning后台管理项目初始化
This commit is contained in:
104
internal/models/common.go
Normal file
104
internal/models/common.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// BaseModel 基础模型,包含公共字段
|
||||
type BaseModel struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt,omitempty"`
|
||||
}
|
||||
|
||||
// User 用户模型
|
||||
type User struct {
|
||||
BaseModel
|
||||
Username string `gorm:"uniqueIndex;size:50;not null" json:"username"`
|
||||
Email string `gorm:"uniqueIndex;size:100;not null" json:"email"`
|
||||
Nickname string `gorm:"size:50" json:"nickname"`
|
||||
Avatar string `gorm:"size:255" json:"avatar"`
|
||||
Status int `gorm:"default:1;comment:状态 1:正常 0:禁用" json:"status"`
|
||||
SSOProvider string `gorm:"size:50;not null;comment:SSO提供商" json:"ssoProvider"`
|
||||
LastLoginAt *time.Time `gorm:"comment:最后登录时间" json:"lastLoginAt"`
|
||||
LoginCount int `gorm:"default:0;comment:登录次数" json:"loginCount"`
|
||||
RoleID uint `gorm:"not null;default:0;comment:角色ID" json:"roleId"`
|
||||
}
|
||||
|
||||
// UserWithRoles 带角色信息的用户模型
|
||||
type UserWithRoles struct {
|
||||
User
|
||||
Role *Role `json:"role,omitempty"`
|
||||
}
|
||||
|
||||
// Role 角色模型
|
||||
type Role struct {
|
||||
BaseModel
|
||||
Name string `gorm:"uniqueIndex;size:50;not null" json:"name"`
|
||||
Level int `gorm:"uniqueIndex;not null" json:"level"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
IsDefault bool `gorm:"default:false" json:"isDefault"`
|
||||
}
|
||||
|
||||
// SystemConfig 系统配置模型
|
||||
type SystemConfig struct {
|
||||
BaseModel
|
||||
Key string `gorm:"uniqueIndex;size:100;not null" json:"key"`
|
||||
Name string `gorm:"size:100;not null" json:"name"`
|
||||
Value string `gorm:"type:text" json:"value"`
|
||||
Type string `gorm:"size:20;default:string" json:"type"` // string, int, bool, json
|
||||
Desc string `gorm:"size:255" json:"desc"`
|
||||
Status int `gorm:"default:1" json:"status"` // 1:启用 0:禁用
|
||||
}
|
||||
|
||||
// LoginLog 登录日志模型
|
||||
type LoginLog struct {
|
||||
BaseModel
|
||||
UserID uint `gorm:"not null" json:"userId"`
|
||||
Username string `gorm:"size:50;not null" json:"username"`
|
||||
IP string `gorm:"size:45" json:"ip"`
|
||||
UserAgent string `gorm:"size:500" json:"userAgent"`
|
||||
Status int `gorm:"default:1" json:"status"` // 1:成功 0:失败
|
||||
Message string `gorm:"size:255" json:"message"`
|
||||
}
|
||||
|
||||
// OperationLog 操作日志模型
|
||||
type OperationLog struct {
|
||||
BaseModel
|
||||
UserID uint `gorm:"not null" json:"userId"`
|
||||
Username string `gorm:"size:50;not null" json:"username"`
|
||||
Module string `gorm:"size:50" json:"module"`
|
||||
Operation string `gorm:"size:50" json:"operation"`
|
||||
Method string `gorm:"size:10" json:"method"`
|
||||
Path string `gorm:"size:255" json:"path"`
|
||||
IP string `gorm:"size:45" json:"ip"`
|
||||
UserAgent string `gorm:"size:500" json:"userAgent"`
|
||||
Request string `gorm:"type:text" json:"request"`
|
||||
Response string `gorm:"type:text" json:"response"`
|
||||
Status int `gorm:"default:1" json:"status"`
|
||||
Duration int64 `json:"duration"` // 毫秒
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (User) TableName() string {
|
||||
return "admin_users"
|
||||
}
|
||||
|
||||
func (Role) TableName() string {
|
||||
return "admin_roles"
|
||||
}
|
||||
|
||||
func (SystemConfig) TableName() string {
|
||||
return "admin_system_configs"
|
||||
}
|
||||
|
||||
func (LoginLog) TableName() string {
|
||||
return "admin_login_logs"
|
||||
}
|
||||
|
||||
func (OperationLog) TableName() string {
|
||||
return "admin_operation_logs"
|
||||
}
|
||||
Reference in New Issue
Block a user