mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	[操作日志]增加若干查询条件
This commit is contained in:
		@@ -4,6 +4,9 @@ import (
 | 
				
			|||||||
	_ "github.com/go-sql-driver/mysql"
 | 
						_ "github.com/go-sql-driver/mysql"
 | 
				
			||||||
	"github.com/iwind/TeaGo/Tea"
 | 
						"github.com/iwind/TeaGo/Tea"
 | 
				
			||||||
	"github.com/iwind/TeaGo/dbs"
 | 
						"github.com/iwind/TeaGo/dbs"
 | 
				
			||||||
 | 
						timeutil "github.com/iwind/TeaGo/utils/time"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type LogDAO dbs.DAO
 | 
					type LogDAO dbs.DAO
 | 
				
			||||||
@@ -32,20 +35,51 @@ func (this *LogDAO) CreateLog(adminType string, adminId int64, level string, des
 | 
				
			|||||||
	op := NewLogOperator()
 | 
						op := NewLogOperator()
 | 
				
			||||||
	op.Type = adminType
 | 
						op.Type = adminType
 | 
				
			||||||
	op.AdminId, op.Level, op.Description, op.Action, op.Ip = adminId, level, description, action, ip
 | 
						op.AdminId, op.Level, op.Description, op.Action, op.Ip = adminId, level, description, action, ip
 | 
				
			||||||
 | 
						op.Day = timeutil.Format("Ymd")
 | 
				
			||||||
	op.Type = LogTypeAdmin
 | 
						op.Type = LogTypeAdmin
 | 
				
			||||||
	_, err := this.Save(op)
 | 
						_, err := this.Save(op)
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 计算所有日志数量
 | 
					// 计算所有日志数量
 | 
				
			||||||
func (this *LogDAO) CountAllLogs() (int64, error) {
 | 
					func (this *LogDAO) CountLogs(dayFrom string, dayTo string, keyword string) (int64, error) {
 | 
				
			||||||
	return this.Query().
 | 
						dayFrom = this.formatDay(dayFrom)
 | 
				
			||||||
		Count()
 | 
						dayTo = this.formatDay(dayTo)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						query := this.Query()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(dayFrom) > 0 {
 | 
				
			||||||
 | 
							query.Gte("day", dayFrom)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(dayTo) > 0 {
 | 
				
			||||||
 | 
							query.Lte("day", dayTo)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(keyword) > 0 {
 | 
				
			||||||
 | 
							query.Where("(description LIKE :keyword OR ip LIKE :keyword OR action LIKE :keyword)").
 | 
				
			||||||
 | 
								Param("keyword", "%"+keyword+"%")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return query.Count()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 列出单页日志
 | 
					// 列出单页日志
 | 
				
			||||||
func (this *LogDAO) ListLogs(offset int64, size int64) (result []*Log, err error) {
 | 
					func (this *LogDAO) ListLogs(offset int64, size int64, dayFrom string, dayTo string, keyword string) (result []*Log, err error) {
 | 
				
			||||||
	_, err = this.Query().
 | 
						dayFrom = this.formatDay(dayFrom)
 | 
				
			||||||
 | 
						dayTo = this.formatDay(dayTo)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						query := this.Query()
 | 
				
			||||||
 | 
						if len(dayFrom) > 0 {
 | 
				
			||||||
 | 
							query.Gte("day", dayFrom)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(dayTo) > 0 {
 | 
				
			||||||
 | 
							query.Lte("day", dayTo)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(keyword) > 0 {
 | 
				
			||||||
 | 
							query.Where("(description LIKE :keyword OR ip LIKE :keyword OR action LIKE :keyword)").
 | 
				
			||||||
 | 
								Param("keyword", "%"+keyword+"%")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err = query.
 | 
				
			||||||
		Offset(offset).
 | 
							Offset(offset).
 | 
				
			||||||
		Limit(size).
 | 
							Limit(size).
 | 
				
			||||||
		Slice(&result).
 | 
							Slice(&result).
 | 
				
			||||||
@@ -53,3 +87,12 @@ func (this *LogDAO) ListLogs(offset int64, size int64) (result []*Log, err error
 | 
				
			|||||||
		FindAll()
 | 
							FindAll()
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 格式化日期
 | 
				
			||||||
 | 
					func (this *LogDAO) formatDay(day string) string {
 | 
				
			||||||
 | 
						if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(day) {
 | 
				
			||||||
 | 
							return ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						day = strings.ReplaceAll(day, "-", "")
 | 
				
			||||||
 | 
						return day
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,6 +12,7 @@ type Log struct {
 | 
				
			|||||||
	ProviderId  uint32 `field:"providerId"`  // 供应商ID
 | 
						ProviderId  uint32 `field:"providerId"`  // 供应商ID
 | 
				
			||||||
	Ip          string `field:"ip"`          // IP地址
 | 
						Ip          string `field:"ip"`          // IP地址
 | 
				
			||||||
	Type        string `field:"type"`        // 类型:admin, user
 | 
						Type        string `field:"type"`        // 类型:admin, user
 | 
				
			||||||
 | 
						Day         string `field:"day"`         // 日期
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type LogOperator struct {
 | 
					type LogOperator struct {
 | 
				
			||||||
@@ -25,6 +26,7 @@ type LogOperator struct {
 | 
				
			|||||||
	ProviderId  interface{} // 供应商ID
 | 
						ProviderId  interface{} // 供应商ID
 | 
				
			||||||
	Ip          interface{} // IP地址
 | 
						Ip          interface{} // IP地址
 | 
				
			||||||
	Type        interface{} // 类型:admin, user
 | 
						Type        interface{} // 类型:admin, user
 | 
				
			||||||
 | 
						Day         interface{} // 日期
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewLogOperator() *LogOperator {
 | 
					func NewLogOperator() *LogOperator {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,9 @@ type User struct {
 | 
				
			|||||||
	CreatedAt uint64 `field:"createdAt"` // 创建时间
 | 
						CreatedAt uint64 `field:"createdAt"` // 创建时间
 | 
				
			||||||
	UpdatedAt uint64 `field:"updatedAt"` // 修改时间
 | 
						UpdatedAt uint64 `field:"updatedAt"` // 修改时间
 | 
				
			||||||
	State     uint8  `field:"state"`     // 状态
 | 
						State     uint8  `field:"state"`     // 状态
 | 
				
			||||||
 | 
						Mobile    string `field:"mobile"`    // 手机号
 | 
				
			||||||
 | 
						Remark    string `field:"remark"`    // 备注
 | 
				
			||||||
 | 
						Email     string `field:"email"`     // 邮箱地址
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type UserOperator struct {
 | 
					type UserOperator struct {
 | 
				
			||||||
@@ -19,6 +22,9 @@ type UserOperator struct {
 | 
				
			|||||||
	CreatedAt interface{} // 创建时间
 | 
						CreatedAt interface{} // 创建时间
 | 
				
			||||||
	UpdatedAt interface{} // 修改时间
 | 
						UpdatedAt interface{} // 修改时间
 | 
				
			||||||
	State     interface{} // 状态
 | 
						State     interface{} // 状态
 | 
				
			||||||
 | 
						Mobile    interface{} // 手机号
 | 
				
			||||||
 | 
						Remark    interface{} // 备注
 | 
				
			||||||
 | 
						Email     interface{} // 邮箱地址
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewUserOperator() *UserOperator {
 | 
					func NewUserOperator() *UserOperator {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,7 +34,7 @@ func (this *LogService) CountLogs(ctx context.Context, req *pb.CountLogRequest)
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	count, err := models.SharedLogDAO.CountAllLogs()
 | 
						count, err := models.SharedLogDAO.CountLogs(req.DayFrom, req.DayTo, req.Keyword)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -49,7 +49,7 @@ func (this *LogService) ListLogs(ctx context.Context, req *pb.ListLogsRequest) (
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logs, err := models.SharedLogDAO.ListLogs(req.Offset, req.Size)
 | 
						logs, err := models.SharedLogDAO.ListLogs(req.Offset, req.Size, req.DayFrom, req.DayTo, req.Keyword)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user