loginLog.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package admin
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/lejianwen/rustdesk-api/global"
  5. "github.com/lejianwen/rustdesk-api/http/request/admin"
  6. "github.com/lejianwen/rustdesk-api/http/response"
  7. "github.com/lejianwen/rustdesk-api/model"
  8. "github.com/lejianwen/rustdesk-api/service"
  9. "gorm.io/gorm"
  10. "strconv"
  11. )
  12. type LoginLog struct {
  13. }
  14. // Detail 登录日志
  15. // @Tags 登录日志
  16. // @Summary 登录日志详情
  17. // @Description 登录日志详情
  18. // @Accept json
  19. // @Produce json
  20. // @Param id path int true "ID"
  21. // @Success 200 {object} response.Response{data=model.LoginLog}
  22. // @Failure 500 {object} response.Response
  23. // @Router /admin/login_log/detail/{id} [get]
  24. // @Security token
  25. func (ct *LoginLog) Detail(c *gin.Context) {
  26. id := c.Param("id")
  27. iid, _ := strconv.Atoi(id)
  28. u := service.AllService.LoginLogService.InfoById(uint(iid))
  29. if u.Id > 0 {
  30. response.Success(c, u)
  31. return
  32. }
  33. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  34. return
  35. }
  36. // List 列表
  37. // @Tags 登录日志
  38. // @Summary 登录日志列表
  39. // @Description 登录日志列表
  40. // @Accept json
  41. // @Produce json
  42. // @Param page query int false "页码"
  43. // @Param page_size query int false "页大小"
  44. // @Param user_id query int false "用户ID"
  45. // @Success 200 {object} response.Response{data=model.LoginLogList}
  46. // @Failure 500 {object} response.Response
  47. // @Router /admin/login_log/list [get]
  48. // @Security token
  49. func (ct *LoginLog) List(c *gin.Context) {
  50. query := &admin.LoginLogQuery{}
  51. if err := c.ShouldBindQuery(query); err != nil {
  52. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  53. return
  54. }
  55. res := service.AllService.LoginLogService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
  56. if query.UserId > 0 {
  57. tx.Where("user_id = ?", query.UserId)
  58. }
  59. tx.Order("id desc")
  60. })
  61. response.Success(c, res)
  62. }
  63. // Delete 删除
  64. // @Tags 登录日志
  65. // @Summary 登录日志删除
  66. // @Description 登录日志删除
  67. // @Accept json
  68. // @Produce json
  69. // @Param body body model.LoginLog true "登录日志信息"
  70. // @Success 200 {object} response.Response
  71. // @Failure 500 {object} response.Response
  72. // @Router /admin/login_log/delete [post]
  73. // @Security token
  74. func (ct *LoginLog) Delete(c *gin.Context) {
  75. f := &model.LoginLog{}
  76. if err := c.ShouldBindJSON(f); err != nil {
  77. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  78. return
  79. }
  80. id := f.Id
  81. errList := global.Validator.ValidVar(c, id, "required,gt=0")
  82. if len(errList) > 0 {
  83. response.Fail(c, 101, errList[0])
  84. return
  85. }
  86. l := service.AllService.LoginLogService.InfoById(f.Id)
  87. if l.Id == 0 {
  88. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  89. return
  90. }
  91. err := service.AllService.LoginLogService.Delete(l)
  92. if err == nil {
  93. response.Success(c, nil)
  94. return
  95. }
  96. response.Fail(c, 101, err.Error())
  97. }
  98. // BatchDelete 删除
  99. // @Tags 登录日志
  100. // @Summary 登录日志批量删除
  101. // @Description 登录日志批量删除
  102. // @Accept json
  103. // @Produce json
  104. // @Param body body admin.LoginLogIds true "登录日志"
  105. // @Success 200 {object} response.Response
  106. // @Failure 500 {object} response.Response
  107. // @Router /admin/login_log/batchDelete [post]
  108. // @Security token
  109. func (ct *LoginLog) BatchDelete(c *gin.Context) {
  110. f := &admin.LoginLogIds{}
  111. if err := c.ShouldBindJSON(f); err != nil {
  112. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  113. return
  114. }
  115. if len(f.Ids) == 0 {
  116. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
  117. return
  118. }
  119. err := service.AllService.LoginLogService.BatchDelete(f.Ids)
  120. if err == nil {
  121. response.Success(c, nil)
  122. return
  123. }
  124. response.Fail(c, 101, err.Error())
  125. return
  126. }