From 72e3a9a84103c382b44d71dd40a285c0d7256444 Mon Sep 17 00:00:00 2001 From: lejianwen <84855512@qq.com> Date: Fri, 27 Dec 2024 19:25:59 +0800 Subject: [PATCH] feat(admin): Add My Login log --- cmd/apimain.go | 2 +- http/controller/admin/loginLog.go | 23 ++---- http/controller/admin/my/loginLog.go | 113 +++++++++++++++++++++++++++ http/response/admin/user.go | 2 +- http/router/admin.go | 9 ++- model/loginLog.go | 8 +- service/loginLog.go | 9 +++ 7 files changed, 146 insertions(+), 20 deletions(-) create mode 100644 http/controller/admin/my/loginLog.go diff --git a/cmd/apimain.go b/cmd/apimain.go index 4fafbc2..0f6f45f 100644 --- a/cmd/apimain.go +++ b/cmd/apimain.go @@ -169,7 +169,7 @@ func InitGlobal() { global.Lock = lock.NewLocal() } func DatabaseAutoUpdate() { - version := 246 + version := 247 db := global.DB diff --git a/http/controller/admin/loginLog.go b/http/controller/admin/loginLog.go index 90a57b0..fc81f2f 100644 --- a/http/controller/admin/loginLog.go +++ b/http/controller/admin/loginLog.go @@ -56,10 +56,6 @@ func (ct *LoginLog) List(c *gin.Context) { response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) return } - u := service.AllService.UserService.CurUser(c) - if !service.AllService.UserService.IsAdmin(u) || query.IsMy == 1 { - query.UserId = int(u.Id) - } res := service.AllService.LoginLogService.List(query.Page, query.PageSize, func(tx *gorm.DB) { if query.UserId > 0 { tx.Where("user_id = ?", query.UserId) @@ -93,21 +89,16 @@ func (ct *LoginLog) Delete(c *gin.Context) { return } l := service.AllService.LoginLogService.InfoById(f.Id) - u := service.AllService.UserService.CurUser(c) - if !service.AllService.UserService.IsAdmin(u) && l.UserId != u.Id { - response.Fail(c, 101, response.TranslateMsg(c, "NoAccess")) + if l.Id == 0 { + response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) return } - if l.Id > 0 { - err := service.AllService.LoginLogService.Delete(l) - if err == nil { - response.Success(c, nil) - return - } - response.Fail(c, 101, err.Error()) + err := service.AllService.LoginLogService.Delete(l) + if err == nil { + response.Success(c, nil) return } - response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) + response.Fail(c, 101, err.Error()) } // BatchDelete 删除 @@ -119,7 +110,7 @@ func (ct *LoginLog) Delete(c *gin.Context) { // @Param body body admin.LoginLogIds true "登录日志" // @Success 200 {object} response.Response // @Failure 500 {object} response.Response -// @Router /admin/login_log/delete [post] +// @Router /admin/login_log/batchDelete [post] // @Security token func (ct *LoginLog) BatchDelete(c *gin.Context) { f := &admin.LoginLogIds{} diff --git a/http/controller/admin/my/loginLog.go b/http/controller/admin/my/loginLog.go new file mode 100644 index 0000000..1cf9dcd --- /dev/null +++ b/http/controller/admin/my/loginLog.go @@ -0,0 +1,113 @@ +package my + +import ( + "Gwen/global" + "Gwen/http/request/admin" + "Gwen/http/response" + "Gwen/model" + "Gwen/service" + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +type LoginLog struct { +} + +// List 列表 +// @Tags 我的登录日志 +// @Summary 登录日志列表 +// @Description 登录日志列表 +// @Accept json +// @Produce json +// @Param page query int false "页码" +// @Param page_size query int false "页大小" +// @Param user_id query int false "用户ID" +// @Success 200 {object} response.Response{data=model.LoginLogList} +// @Failure 500 {object} response.Response +// @Router /admin/my/login_log/list [get] +// @Security token +func (ct *LoginLog) List(c *gin.Context) { + query := &admin.LoginLogQuery{} + if err := c.ShouldBindQuery(query); err != nil { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) + return + } + u := service.AllService.UserService.CurUser(c) + res := service.AllService.LoginLogService.List(query.Page, query.PageSize, func(tx *gorm.DB) { + tx.Where("user_id = ? and is_deleted = ?", u.Id, model.IsDeletedNo) + tx.Order("id desc") + }) + response.Success(c, res) +} + +// Delete 删除 +// @Tags 我的登录日志 +// @Summary 登录日志删除 +// @Description 登录日志删除 +// @Accept json +// @Produce json +// @Param body body model.LoginLog true "登录日志信息" +// @Success 200 {object} response.Response +// @Failure 500 {object} response.Response +// @Router /admin/my/login_log/delete [post] +// @Security token +func (ct *LoginLog) Delete(c *gin.Context) { + f := &model.LoginLog{} + if err := c.ShouldBindJSON(f); err != nil { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) + return + } + id := f.Id + errList := global.Validator.ValidVar(c, id, "required,gt=0") + if len(errList) > 0 { + response.Fail(c, 101, errList[0]) + return + } + l := service.AllService.LoginLogService.InfoById(f.Id) + if l.Id == 0 || l.IsDeleted == model.IsDeletedYes { + response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) + return + } + u := service.AllService.UserService.CurUser(c) + if l.UserId != u.Id { + response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) + return + } + err := service.AllService.LoginLogService.SoftDelete(l) + if err == nil { + response.Success(c, nil) + return + } + response.Fail(c, 101, err.Error()) +} + +// BatchDelete 删除 +// @Tags 我的登录日志 +// @Summary 登录日志批量删除 +// @Description 登录日志批量删除 +// @Accept json +// @Produce json +// @Param body body admin.LoginLogIds true "登录日志" +// @Success 200 {object} response.Response +// @Failure 500 {object} response.Response +// @Router /admin/my/login_log/batchDelete [post] +// @Security token +func (ct *LoginLog) BatchDelete(c *gin.Context) { + f := &admin.LoginLogIds{} + if err := c.ShouldBindJSON(f); err != nil { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) + return + } + if len(f.Ids) == 0 { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")) + return + } + u := service.AllService.UserService.CurUser(c) + err := service.AllService.LoginLogService.BatchSoftDelete(u.Id, f.Ids) + if err == nil { + response.Success(c, nil) + return + } + response.Fail(c, 101, err.Error()) + return +} diff --git a/http/response/admin/user.go b/http/response/admin/user.go index 7f85975..568aba0 100644 --- a/http/response/admin/user.go +++ b/http/response/admin/user.go @@ -19,7 +19,7 @@ func (lp *LoginPayload) FromUser(user *model.User) { } var UserRouteNames = []string{ - "MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection", "MyPeer", "MyShareRecordList", + "MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection", "MyPeer", "MyShareRecordList", "MyLoginLog", } var AdminRouteNames = []string{"*"} diff --git a/http/router/admin.go b/http/router/admin.go index 92319a3..36046de 100644 --- a/http/router/admin.go +++ b/http/router/admin.go @@ -160,8 +160,8 @@ func OauthBind(rg *gin.RouterGroup) { } func LoginLogBind(rg *gin.RouterGroup) { - aR := rg.Group("/login_log") cont := &admin.LoginLog{} + aR := rg.Group("/login_log").Use(middleware.AdminPrivilege()) aR.GET("/list", cont.List) aR.POST("/delete", cont.Delete) aR.POST("/batchDelete", cont.BatchDelete) @@ -274,6 +274,13 @@ func MyBind(rg *gin.RouterGroup) { rg.GET("/my/peer/list", cont.List) } + + { + cont := &my.LoginLog{} + rg.GET("/my/login_log/list", cont.List) + rg.POST("/my/login_log/delete", cont.Delete) + rg.POST("/my/login_log/batchDelete", cont.BatchDelete) + } } func ShareRecordBind(rg *gin.RouterGroup) { diff --git a/model/loginLog.go b/model/loginLog.go index 0dbb5f4..5a0bce9 100644 --- a/model/loginLog.go +++ b/model/loginLog.go @@ -4,12 +4,13 @@ type LoginLog struct { IdModel UserId uint `json:"user_id" gorm:"default:0;not null;"` Client string `json:"client"` //webadmin,webclient,app, - DeviceId string `json:"device_id"` + DeviceId string `json:"device_id"` Uuid string `json:"uuid"` Ip string `json:"ip"` Type string `json:"type"` //account,oauth Platform string `json:"platform"` //windows,linux,mac,android,ios UserTokenId uint `json:"user_token_id" gorm:"default:0;not null;"` + IsDeleted uint `json:"is_deleted" gorm:"default:0;not null;"` TimeModel } @@ -24,6 +25,11 @@ const ( LoginLogTypeOauth = "oauth" ) +const ( + IsDeletedNo = 0 + IsDeletedYes = 1 +) + type LoginLogList struct { LoginLogs []*LoginLog `json:"list"` Pagination diff --git a/service/loginLog.go b/service/loginLog.go index e484631..7a291d6 100644 --- a/service/loginLog.go +++ b/service/loginLog.go @@ -47,3 +47,12 @@ func (us *LoginLogService) Update(u *model.LoginLog) error { func (us *LoginLogService) BatchDelete(ids []uint) error { return global.DB.Where("id in (?)", ids).Delete(&model.LoginLog{}).Error } + +func (us *LoginLogService) SoftDelete(l *model.LoginLog) error { + l.IsDeleted = model.IsDeletedYes + return us.Update(l) +} + +func (us *LoginLogService) BatchSoftDelete(uid uint, ids []uint) error { + return global.DB.Model(&model.LoginLog{}).Where("user_id = ? and id in (?)", uid, ids).Update("is_deleted", model.IsDeletedYes).Error +}