Files
backend/internal/model/device_token.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

86 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package model
import (
"time"
"gorm.io/gorm"
)
// DeviceType 设备类型
type DeviceType string
const (
DeviceTypeIOS DeviceType = "ios" // iOS设备
DeviceTypeAndroid DeviceType = "android" // Android设备
DeviceTypeWeb DeviceType = "web" // Web端
)
// DeviceToken 设备Token实体
// 用于管理用户的多设备推送Token
type DeviceToken struct {
ID int64 `gorm:"primaryKey;autoIncrement:false" json:"id"` // 雪花算法ID
UserID string `gorm:"column:user_id;type:varchar(50);index;not null" json:"user_id"` // 用户ID (UUID格式)
DeviceID string `gorm:"type:varchar(100);not null" json:"device_id"` // 设备唯一标识
DeviceType DeviceType `gorm:"type:varchar(20);not null" json:"device_type"` // 设备类型
PushToken string `gorm:"type:varchar(256)" json:"push_token,omitempty"` // 推送TokenJPush RegistrationID等Web端可为空
IsActive bool `gorm:"default:true" json:"is_active"` // 是否活跃
DeviceName string `gorm:"type:varchar(100)" json:"device_name,omitempty"` // 设备名称(可选)
// 时间戳
LastUsedAt *time.Time `json:"last_used_at,omitempty"` // 最后使用时间
// 软删除
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
// 时间戳
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}
// BeforeCreate 创建前生成雪花算法ID
func (d *DeviceToken) BeforeCreate(tx *gorm.DB) error {
return SetSnowflakeInt64ID(&d.ID)
}
func (DeviceToken) TableName() string {
return "device_tokens"
}
// UpdateLastUsed 更新最后使用时间
func (d *DeviceToken) UpdateLastUsed() {
now := time.Now()
d.LastUsedAt = &now
}
// Deactivate 停用设备
func (d *DeviceToken) Deactivate() {
d.IsActive = false
}
// Activate 激活设备
func (d *DeviceToken) Activate() {
d.IsActive = true
now := time.Now()
d.LastUsedAt = &now
}
// IsIOS 判断是否为iOS设备
func (d *DeviceToken) IsIOS() bool {
return d.DeviceType == DeviceTypeIOS
}
// IsAndroid 判断是否为Android设备
func (d *DeviceToken) IsAndroid() bool {
return d.DeviceType == DeviceTypeAndroid
}
// IsWeb 判断是否为Web端
func (d *DeviceToken) IsWeb() bool {
return d.DeviceType == DeviceTypeWeb
}
// SupportsMobilePush 判断是否支持手机推送
func (d *DeviceToken) SupportsMobilePush() bool {
return d.DeviceType == DeviceTypeIOS || d.DeviceType == DeviceTypeAndroid
}