feat(auth): 用户/角色模型与数据访问层
- constants: 用户状态、性别、角色代码常量定义 - model: User/Role/UserRole GORM 模型 - dao: UserDAO (CRUD + 账号查询) + RoleDAO (角色分配/查询/检查) - provider: Auth 模块 Wire Provider Set Made-with: Cursor
This commit is contained in:
29
backend/go-service/app/auth/model/role.go
Normal file
29
backend/go-service/app/auth/model/role.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// Role 角色表模型,对应 auth_roles 表
|
||||
type Role struct {
|
||||
ID int `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
Code string `json:"code" gorm:"uniqueIndex;size:50;not null"` // 角色代码: user/admin/super_admin
|
||||
Name string `json:"name" gorm:"size:50;not null"` // 角色显示名称
|
||||
Description string `json:"description" gorm:"size:200;default:''"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime"`
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (Role) TableName() string {
|
||||
return "auth_roles"
|
||||
}
|
||||
|
||||
// UserRole 用户角色关联表模型,对应 auth_user_roles 表
|
||||
type UserRole struct {
|
||||
UserID int64 `json:"user_id" gorm:"primaryKey"`
|
||||
RoleID int `json:"role_id" gorm:"primaryKey"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime"`
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (UserRole) TableName() string {
|
||||
return "auth_user_roles"
|
||||
}
|
||||
26
backend/go-service/app/auth/model/user.go
Normal file
26
backend/go-service/app/auth/model/user.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Package model 定义 auth 模块的数据库模型
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// User 用户主表模型,对应 auth_users 表
|
||||
type User struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
Username string `json:"username" gorm:"uniqueIndex;size:50;not null"`
|
||||
Email string `json:"email" gorm:"uniqueIndex;size:100;not null"`
|
||||
PasswordHash string `json:"-" gorm:"column:password_hash;size:255;not null"` // JSON 序列化时隐藏密码
|
||||
Nickname string `json:"nickname" gorm:"size:50;not null;default:''"`
|
||||
Avatar string `json:"avatar" gorm:"size:500;not null;default:''"`
|
||||
Gender int `json:"gender" gorm:"not null;default:0"` // 0=未知, 1=男, 2=女
|
||||
Phone *string `json:"phone" gorm:"size:20"` // 可选字段
|
||||
Status int `json:"status" gorm:"not null;default:1"` // 1=正常, 2=禁用, 3=注销
|
||||
LastLoginAt *time.Time `json:"last_login_at"`
|
||||
LastLoginIP *string `json:"last_login_ip" gorm:"column:last_login_ip;size:50"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"not null;autoUpdateTime"`
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (User) TableName() string {
|
||||
return "auth_users"
|
||||
}
|
||||
Reference in New Issue
Block a user