sys_login_log.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * @desc:登录日志
  3. * @company:云南奇讯科技有限公司
  4. * @Author: yixiaohu<yxh669@qq.com>
  5. * @Date: 2022/9/26 15:20
  6. */
  7. package sysLoginLog
  8. import (
  9. "context"
  10. "github.com/gogf/gf/v2/frame/g"
  11. "github.com/gogf/gf/v2/os/grpool"
  12. "github.com/gogf/gf/v2/util/gconv"
  13. "github.com/tiger1103/gfast/v3/api/v1/system"
  14. "github.com/tiger1103/gfast/v3/internal/app/system/consts"
  15. "github.com/tiger1103/gfast/v3/internal/app/system/dao"
  16. "github.com/tiger1103/gfast/v3/internal/app/system/model"
  17. "github.com/tiger1103/gfast/v3/internal/app/system/service"
  18. "github.com/tiger1103/gfast/v3/library/liberr"
  19. )
  20. func init() {
  21. service.RegisterSysLoginLog(New())
  22. }
  23. func New() *sSysLoginLog {
  24. return &sSysLoginLog{
  25. Pool: grpool.New(100),
  26. }
  27. }
  28. type sSysLoginLog struct {
  29. Pool *grpool.Pool
  30. }
  31. func (s *sSysLoginLog) Invoke(ctx context.Context, data *model.LoginLogParams) {
  32. s.Pool.Add(
  33. ctx,
  34. func(ctx context.Context) {
  35. //写入日志数据
  36. service.SysUser().LoginLog(ctx, data)
  37. },
  38. )
  39. }
  40. func (s *sSysLoginLog) List(ctx context.Context, req *system.LoginLogSearchReq) (res *system.LoginLogSearchRes, err error) {
  41. res = new(system.LoginLogSearchRes)
  42. if req.PageNum == 0 {
  43. req.PageNum = 1
  44. }
  45. res.CurrentPage = req.PageNum
  46. if req.PageSize == 0 {
  47. req.PageSize = consts.PageSize
  48. }
  49. m := dao.SysLoginLog.Ctx(ctx)
  50. order := "info_id DESC"
  51. if req.LoginName != "" {
  52. m = m.Where("login_name like ?", "%"+req.LoginName+"%")
  53. }
  54. if req.Status != "" {
  55. m = m.Where("status", gconv.Int(req.Status))
  56. }
  57. if req.Ipaddr != "" {
  58. m = m.Where("ipaddr like ?", "%"+req.Ipaddr+"%")
  59. }
  60. if req.LoginLocation != "" {
  61. m = m.Where("login_location like ?", "%"+req.LoginLocation+"%")
  62. }
  63. if len(req.DateRange) != 0 {
  64. m = m.Where("login_time >=? AND login_time <=?", req.DateRange[0], req.DateRange[1])
  65. }
  66. if req.SortName != "" {
  67. if req.SortOrder != "" {
  68. order = req.SortName + " " + req.SortOrder
  69. } else {
  70. order = req.SortName + " DESC"
  71. }
  72. }
  73. err = g.Try(ctx, func(ctx context.Context) {
  74. res.Total, err = m.Count()
  75. liberr.ErrIsNil(ctx, err, "获取日志失败")
  76. err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res.List)
  77. liberr.ErrIsNil(ctx, err, "获取日志数据失败")
  78. })
  79. return
  80. }
  81. func (s *sSysLoginLog) DeleteLoginLogByIds(ctx context.Context, ids []int) (err error) {
  82. err = g.Try(ctx, func(ctx context.Context) {
  83. _, err = dao.SysLoginLog.Ctx(ctx).Delete("info_id in (?)", ids)
  84. liberr.ErrIsNil(ctx, err, "删除失败")
  85. })
  86. return
  87. }
  88. func (s *sSysLoginLog) ClearLoginLog(ctx context.Context) (err error) {
  89. err = g.Try(ctx, func(ctx context.Context) {
  90. _, err = g.DB().Ctx(ctx).Exec(ctx, "truncate "+dao.SysLoginLog.Table())
  91. liberr.ErrIsNil(ctx, err, "清除失败")
  92. })
  93. return
  94. }