refactor: user login log list for admin is ready

This commit is contained in:
RockYang
2023-06-21 06:53:41 +08:00
parent feff1684c4
commit 0e6606e469
9 changed files with 170 additions and 26 deletions

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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) {

View 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"`
}