feat: add login log record
This commit is contained in:
+15
-18
@@ -1,28 +1,25 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "message-nest/pkg/util"
|
|
||||||
|
|
||||||
type LoginLog struct {
|
type LoginLog struct {
|
||||||
ID uint `gorm:"autoIncrement;type:integer;primaryKey" json:"id"`
|
UUIDModel
|
||||||
UserID int `json:"user_id" gorm:"type:int;index"`
|
|
||||||
Username string `json:"username" gorm:"type:varchar(100);default:'';index"`
|
ID uint `gorm:"autoIncrement;type:integer;primaryKey" json:"id"`
|
||||||
IP string `json:"ip" gorm:"type:varchar(64);default:'';"`
|
UserID int `json:"user_id" gorm:"type:int;index"`
|
||||||
UA string `json:"ua" gorm:"type:varchar(512);default:'';"`
|
Username string `json:"username" gorm:"type:varchar(100);default:'';index"`
|
||||||
CreatedAt util.Time `json:"created_on" gorm:"column:created_on;autoCreateTime"`
|
IP string `json:"ip" gorm:"type:varchar(64);default:'';"`
|
||||||
|
UA string `json:"ua" gorm:"type:varchar(512);default:'';"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddLoginLog(userID int, username string, ip string, ua string) error {
|
func AddLoginLog(userID int, username string, ip string, ua string) error {
|
||||||
log := LoginLog{UserID: userID, Username: username, IP: ip, UA: ua}
|
log := LoginLog{UserID: userID, Username: username, IP: ip, UA: ua}
|
||||||
return db.Create(&log).Error
|
return db.Create(&log).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRecentLoginLogs(limit int) ([]LoginLog, error) {
|
func GetRecentLoginLogs(limit int) ([]LoginLog, error) {
|
||||||
if limit <= 0 {
|
if limit <= 0 {
|
||||||
limit = 8
|
limit = 8
|
||||||
}
|
}
|
||||||
var logs []LoginLog
|
var logs []LoginLog
|
||||||
err := db.Model(&LoginLog{}).Order("id DESC").Limit(limit).Find(&logs).Error
|
err := db.Model(&LoginLog{}).Order("id DESC").Limit(limit).Find(&logs).Error
|
||||||
return logs, err
|
return logs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user