Files
backend/internal/model/user.go
lafay 6429322217
Some checks failed
Build Backend / build (push) Successful in 7m12s
Build Backend / build-docker (push) Has been cancelled
feat(verification): add user identity verification system
Implement a complete user identity verification system with support for multiple identity types (student, teacher, staff, alumni). The system includes user-facing verification submission and status checking endpoints, admin endpoints for reviewing verification requests, middleware for protecting verification-required routes, and integration with the user model to track verification status.
2026-04-05 20:27:03 +08:00

93 lines
3.3 KiB
Go

package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// UserStatus 用户状态
type UserStatus string
const (
UserStatusActive UserStatus = "active"
UserStatusBanned UserStatus = "banned"
UserStatusInactive UserStatus = "inactive"
)
// UserIdentity 用户身份类型
type UserIdentity string
const (
UserIdentityGeneral UserIdentity = "general" // 普通用户
UserIdentityStudent UserIdentity = "student" // 学生
UserIdentityTeacher UserIdentity = "teacher" // 教师
UserIdentityStaff UserIdentity = "staff" // 职工
UserIdentityAlumni UserIdentity = "alumni" // 校友
UserIdentityExternal UserIdentity = "external" // 外部人员
)
// VerificationStatus 审核状态
type VerificationStatus string
const (
VerificationStatusNotSubmitted VerificationStatus = "not_submitted" // 未提交
VerificationStatusPending VerificationStatus = "pending" // 审核中
VerificationStatusApproved VerificationStatus = "approved" // 已通过
VerificationStatusRejected VerificationStatus = "rejected" // 已拒绝
)
// User 用户实体
type User struct {
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
Username string `json:"username" gorm:"type:varchar(50);uniqueIndex;not null"`
Nickname string `json:"nickname" gorm:"type:varchar(100);not null"`
Email *string `json:"email" gorm:"type:varchar(255);uniqueIndex"`
Phone *string `json:"phone" gorm:"type:varchar(20);uniqueIndex"`
EmailVerified bool `json:"email_verified" gorm:"default:false"`
PasswordHash string `json:"-" gorm:"type:varchar(255);not null"`
Avatar string `json:"avatar" gorm:"type:text"`
CoverURL string `json:"cover_url" gorm:"type:text"` // 头图URL
Bio string `json:"bio" gorm:"type:text"`
Website string `json:"website" gorm:"type:varchar(255)"`
Location string `json:"location" gorm:"type:varchar(100)"`
// 实名认证信息(可选)
RealName string `json:"real_name" gorm:"type:varchar(100)"` // 真实姓名
IDCard string `json:"-" gorm:"type:varchar(18)"` // 身份证号(加密存储)
IsVerified bool `json:"is_verified" gorm:"default:false"` // 是否实名认证
VerifiedAt *time.Time `json:"verified_at" gorm:"type:timestamp"`
// 统计计数
PostsCount int `json:"posts_count" gorm:"default:0"`
FollowersCount int `json:"followers_count" gorm:"default:0"`
FollowingCount int `json:"following_count" gorm:"default:0"`
// 身份认证
Identity UserIdentity `json:"identity" gorm:"type:varchar(20);default:general"`
VerificationStatus VerificationStatus `json:"verification_status" gorm:"type:varchar(20);default:not_submitted"`
// 状态
Status UserStatus `json:"status" gorm:"type:varchar(20);default:active"`
LastLoginAt *time.Time `json:"last_login_at" gorm:"type:timestamp"`
LastLoginIP string `json:"last_login_ip" gorm:"type:varchar(45)"`
// 时间戳
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
// BeforeCreate 创建前生成UUID
func (u *User) BeforeCreate(tx *gorm.DB) error {
if u.ID == "" {
u.ID = uuid.New().String()
}
return nil
}
func (User) TableName() string {
return "users"
}