Files
nl-blogs/server/models/user_access_log.go
2026-01-19 13:53:32 +08:00

35 lines
1.0 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 models
import (
"time"
"gorm.io/gorm"
)
// UserAccessLog 用户访问日志模型
type UserAccessLog struct {
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
}