Files
backend/internal/model/user.go
lan ee78071d4d
All checks were successful
Build Backend / build (push) Successful in 3m2s
Build Backend / build-docker (push) Successful in 2m44s
refactor: improve system stability, performance, and code structure
This commit introduces several architectural improvements and optimizations across the codebase:

- **Performance & Reliability**:
  - Implemented Redis pipelining in `ConversationCache.CacheMessage` to reduce network round-trips.
  - Added a circuit breaker to the JPush client to prevent cascading failures.
  - Introduced batch deletion and batch member addition capabilities in repositories.
  - Added message idempotency support using `client_msg_id` and a Redis-based cache.
  - Optimized WebSocket handling with connection limits (total and per-user) and improved error logging.

- **Code Refactoring**:
  - Refactored `Router` to use a `RouterDeps` struct, simplifying the constructor and improving maintainability.
  - Unified model ID generation logic using new `id_helper.go` (supporting UUID and Snowflake).
  - Standardized JSON serialization/deserialization in models using `json_helper.go`.
  - Refactored DTO conversion logic, specifically for `UserResponse` (using functional options) and `Report` responses.
  - Removed redundant/deprecated DTOs like `PostDetailResponse` and `TradeItemDetailResponse`.

- **Cache Improvements**:
  - Enhanced `LayeredCache` with `SetRaw` to avoid double-encoding when promoting values from Redis to local cache.
  - Added `DeleteBatch` support to the cache interface.

- **Other Changes**:
  - Cleaned up `config.go` by removing redundant default values and explicit environment variable overrides.
  - Improved WebSocket registration flow to handle connection limits gracefully.
2026-05-04 13:07:03 +08:00

115 lines
4.4 KiB
Go

package model
import (
"time"
"gorm.io/gorm"
)
// UserStatus 用户状态
type UserStatus string
const (
UserStatusActive UserStatus = "active"
UserStatusBanned UserStatus = "banned"
UserStatusInactive UserStatus = "inactive"
UserStatusPendingDeletion UserStatus = "pending_deletion"
)
// VisibilityLevel 可见性级别
type VisibilityLevel string
const (
VisibilityEveryone VisibilityLevel = "everyone"
VisibilityFollowing VisibilityLevel = "following"
VisibilitySelf VisibilityLevel = "self"
)
// PrivacySettings 隐私设置
type PrivacySettings struct {
FollowersVisibility VisibilityLevel `json:"followers_visibility" gorm:"type:varchar(20);default:everyone"`
FollowingVisibility VisibilityLevel `json:"following_visibility" gorm:"type:varchar(20);default:everyone"`
PostsVisibility VisibilityLevel `json:"posts_visibility" gorm:"type:varchar(20);default:everyone"`
FavoritesVisibility VisibilityLevel `json:"favorites_visibility" gorm:"type:varchar(20);default:everyone"`
}
// 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" // 已拒绝
VerificationStatusNone VerificationStatus = "none" // 无认证(对外展示用)
)
// 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)"`
// 账号注销
DeletionRequestedAt *time.Time `json:"deletion_requested_at" gorm:"type:timestamp"`
// 隐私设置
PrivacySettings PrivacySettings `json:"privacy_settings" gorm:"embedded;embeddedPrefix:privacy_"`
// 时间戳
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 {
SetUUIDIfEmpty(&u.ID)
return nil
}
func (User) TableName() string {
return "users"
}