yxh 4 лет назад
Родитель
Сommit
ff9b812696

+ 47 - 0
api/v1/system/sys_login_log.go

@@ -0,0 +1,47 @@
+/*
+* @desc:登录日志
+* @company:云南奇讯科技有限公司
+* @Author: yixiaohu
+* @Date:   2022/4/24 22:09
+ */
+
+package system
+
+import (
+	"github.com/gogf/gf/v2/frame/g"
+	commonApi "github.com/tiger1103/gfast/v3/api/v1/common"
+	"github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
+)
+
+// LoginLogSearchReq 查询列表请求参数
+type LoginLogSearchReq struct {
+	g.Meta        `path:"/loginLog/list" tags:"登录日志管理" method:"get" summary:"日志列表"`
+	LoginName     string `p:"userName"`      //登陆名
+	Status        string `p:"status"`        //状态
+	Ipaddr        string `p:"ipaddr"`        //登录地址
+	SortName      string `p:"orderByColumn"` //排序字段
+	SortOrder     string `p:"isAsc"`         //排序方式
+	LoginLocation string `p:"loginLocation"` //登录地点
+	commonApi.PageReq
+}
+
+type LoginLogSearchRes struct {
+	g.Meta `mime:"application/json"`
+	commonApi.ListRes
+	List []*entity.SysLoginLog `json:"list"`
+}
+
+type LoginLogDelReq struct {
+	g.Meta `path:"/loginLog/delete" tags:"登录日志管理" method:"delete" summary:"删除日志"`
+	Ids    []int `p:"ids" v:"required#ids必须"`
+}
+
+type LoginLogDelRes struct {
+}
+
+type LoginLogClearReq struct {
+	g.Meta `path:"/loginLog/clear" tags:"登录日志管理" method:"delete" summary:"清除日志"`
+}
+
+type LoginLogClearRes struct {
+}

+ 35 - 0
internal/app/system/controller/sys_login_log.go

@@ -0,0 +1,35 @@
+/*
+* @desc:登录日志管理
+* @company:云南奇讯科技有限公司
+* @Author: yixiaohu
+* @Date:   2022/4/24 22:14
+ */
+
+package controller
+
+import (
+	"context"
+	"github.com/tiger1103/gfast/v3/api/v1/system"
+	"github.com/tiger1103/gfast/v3/internal/app/system/service"
+)
+
+var LoginLog = loginLogController{}
+
+type loginLogController struct {
+	BaseController
+}
+
+func (c *loginLogController) List(ctx context.Context, req *system.LoginLogSearchReq) (res *system.LoginLogSearchRes, err error) {
+	res, err = service.SysLoginLog().List(ctx, req)
+	return
+}
+
+func (c *loginLogController) Delete(ctx context.Context, req *system.LoginLogDelReq) (res *system.LoginLogDelRes, err error) {
+	err = service.SysLoginLog().DeleteLoginLogByIds(ctx, req.Ids)
+	return
+}
+
+func (c *loginLogController) Clear(ctx context.Context, req *system.LoginLogClearReq) (res *system.LoginLogClearRes, err error) {
+	err = service.SysLoginLog().ClearLoginLog(ctx)
+	return
+}

+ 1 - 0
internal/app/system/router/router.go

@@ -35,6 +35,7 @@ func BindController(group *ghttp.RouterGroup) {
 			controller.DictData,
 			controller.Config,
 			controller.Monitor,
+			controller.LoginLog,
 		)
 	})
 }

+ 67 - 0
internal/app/system/service/sys_login_log.go

@@ -9,12 +9,21 @@ package service
 
 import (
 	"context"
+	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/os/grpool"
+	"github.com/gogf/gf/v2/util/gconv"
+	"github.com/tiger1103/gfast/v3/api/v1/system"
+	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
 	"github.com/tiger1103/gfast/v3/internal/app/system/model"
+	"github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao"
+	"github.com/tiger1103/gfast/v3/library/liberr"
 )
 
 type ISysLoginLog interface {
 	Invoke(ctx context.Context, data *model.LoginLogParams)
+	List(ctx context.Context, req *system.LoginLogSearchReq) (res *system.LoginLogSearchRes, err error)
+	DeleteLoginLogByIds(ctx context.Context, ids []int) (err error)
+	ClearLoginLog(ctx context.Context) (err error)
 }
 
 type sysLoginLogImpl struct {
@@ -40,3 +49,61 @@ func (s *sysLoginLogImpl) Invoke(ctx context.Context, data *model.LoginLogParams
 		},
 	)
 }
+
+func (s *sysLoginLogImpl) List(ctx context.Context, req *system.LoginLogSearchReq) (res *system.LoginLogSearchRes, err error) {
+	res = new(system.LoginLogSearchRes)
+	if req.PageNum == 0 {
+		req.PageNum = 1
+	}
+	res.CurrentPage = req.PageNum
+	if req.PageSize == 0 {
+		req.PageSize = consts.PageSize
+	}
+	m := dao.SysLoginLog.Ctx(ctx)
+	order := "info_id DESC"
+	if req.LoginName != "" {
+		m = m.Where("login_name like ?", "%"+req.LoginName+"%")
+	}
+	if req.Status != "" {
+		m = m.Where("status", gconv.Int(req.Status))
+	}
+	if req.Ipaddr != "" {
+		m = m.Where("ipaddr like ?", "%"+req.Ipaddr+"%")
+	}
+	if req.LoginLocation != "" {
+		m = m.Where("login_location like ?", "%"+req.LoginLocation+"%")
+	}
+	if len(req.DateRange) != 0 {
+		m = m.Where("login_time >=? AND login_time <=?", req.DateRange[0], req.DateRange[1])
+	}
+	if req.SortName != "" {
+		if req.SortOrder != "" {
+			order = req.SortName + " " + req.SortOrder
+		} else {
+			order = req.SortName + " DESC"
+		}
+	}
+	err = g.Try(func() {
+		res.Total, err = m.Count()
+		liberr.ErrIsNil(ctx, err, "获取日志失败")
+		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res.List)
+		liberr.ErrIsNil(ctx, err, "获取日志数据失败")
+	})
+	return
+}
+
+func (s *sysLoginLogImpl) DeleteLoginLogByIds(ctx context.Context, ids []int) (err error) {
+	err = g.Try(func() {
+		_, err = dao.SysLoginLog.Ctx(ctx).Delete("info_id in (?)", ids)
+		liberr.ErrIsNil(ctx, err, "删除失败")
+	})
+	return
+}
+
+func (s *sysLoginLogImpl) ClearLoginLog(ctx context.Context) (err error) {
+	err = g.Try(func() {
+		_, err = g.DB().Ctx(ctx).Exec(ctx, "truncate "+dao.SysLoginLog.Table())
+		liberr.ErrIsNil(ctx, err, "清除失败")
+	})
+	return
+}