Files
nl-blogs/server/models/user_access_log.go

35 lines
1.0 KiB
Go
Raw Normal View History

2026-01-16 10:19:30 +08:00
package models
2026-01-19 13:53:32 +08:00
import (
"time"
"gorm.io/gorm"
)
2026-01-16 13:01:21 +08:00
// UserAccessLog 用户访问日志模型
2026-01-16 10:19:30 +08:00
type UserAccessLog struct {
2026-01-19 13:53:32 +08:00
ID uint `json:"id" gorm:"primaryKey;column:id"`
UserID uint `json:"user_id" gorm:"column:user_id;index"` // 用户ID未登录用户为0
UserIP string `json:"user_ip" gorm:"column:user_ip;index"` // 用户IP地址
UserLocation string `json:"user_location" gorm:"column:user_location"` // 用户归属地
ArticleID uint `json:"article_id" gorm:"column:article_id;index"` // 访问的文章ID
AccessTime int64 `json:"access_time" gorm:"column:access_time"` // 访问时间
DeletedAt int64 `json:"deleted_at" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (UserAccessLog) TableName() string {
return "user_access_logs"
}
// BeforeCreate 创建前钩子
func (u *UserAccessLog) BeforeCreate(tx *gorm.DB) error {
if u.AccessTime == 0 {
u.AccessTime = time.Now().Unix()
}
if u.DeletedAt == 0 {
u.DeletedAt = 0
}
return nil
2026-01-16 10:19:30 +08:00
}