mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-07 09:43:43 +08:00
refactor: user login log list for admin is ready
This commit is contained in:
@@ -8,9 +8,10 @@ import (
|
||||
"chatplus/store/vo"
|
||||
"chatplus/utils"
|
||||
"chatplus/utils/resp"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ApiKeyHandler struct {
|
||||
@@ -61,7 +62,7 @@ func (h *ApiKeyHandler) Save(c *gin.Context) {
|
||||
|
||||
func (h *ApiKeyHandler) List(c *gin.Context) {
|
||||
userId := h.GetInt(c, "user_id", -1)
|
||||
query := h.db.Debug().Session(&gorm.Session{})
|
||||
query := h.db.Session(&gorm.Session{})
|
||||
if userId >= 0 {
|
||||
query = query.Where("user_id", userId)
|
||||
}
|
||||
|
||||
@@ -118,6 +118,32 @@ func (h *UserHandler) Remove(c *gin.Context) {
|
||||
resp.SUCCESS(c)
|
||||
}
|
||||
|
||||
func (h *UserHandler) LoginLog(c *gin.Context) {
|
||||
page := h.GetInt(c, "page", 1)
|
||||
pageSize := h.GetInt(c, "page_size", 20)
|
||||
var total int64
|
||||
h.db.Model(&model.UserLoginLog{}).Count(&total)
|
||||
offset := (page - 1) * pageSize
|
||||
var items []model.UserLoginLog
|
||||
res := h.db.Offset(offset).Limit(pageSize).Find(&items)
|
||||
if res.Error != nil {
|
||||
resp.ERROR(c, "获取数据失败")
|
||||
return
|
||||
}
|
||||
var logs []vo.UserLoginLog
|
||||
for _, v := range items {
|
||||
var log vo.UserLoginLog
|
||||
err := utils.CopyObject(v, &log)
|
||||
if err == nil {
|
||||
log.Id = v.Id
|
||||
log.CreatedAt = v.CreatedAt.Unix()
|
||||
logs = append(logs, log)
|
||||
}
|
||||
}
|
||||
|
||||
resp.SUCCESS(c, vo.NewPage(total, page, pageSize, logs))
|
||||
}
|
||||
|
||||
func (h *UserHandler) InitUser(c *gin.Context) {
|
||||
var users []model.User
|
||||
h.db.Find(&users)
|
||||
|
||||
@@ -137,6 +137,7 @@ func main() {
|
||||
group.GET("list", h.List)
|
||||
group.POST("update", h.Update)
|
||||
group.GET("remove", h.Remove)
|
||||
group.GET("loginLog", h.LoginLog)
|
||||
group.GET("test", h.InitUser)
|
||||
}),
|
||||
fx.Invoke(func(s *core.AppServer, h *admin.ChatRoleHandler) {
|
||||
|
||||
9
api/go/store/vo/user_login_log.go
Normal file
9
api/go/store/vo/user_login_log.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package vo
|
||||
|
||||
type UserLoginLog struct {
|
||||
BaseVo
|
||||
UserId uint `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
LoginIp string `json:"login_ip"`
|
||||
LoginAddress string `json:"login_address"`
|
||||
}
|
||||
Reference in New Issue
Block a user