From 6caa6442591e48e1ff5d7fcdf9e668f380db3b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=9F=E5=B8=85?= <133814250@qq.com> Date: Mon, 2 Sep 2024 14:03:50 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=E6=BD=9C=E5=9C=A8?= =?UTF-8?q?=E5=85=B3=E9=94=AE=E8=AF=8D=E6=9F=A5=E8=AF=A2sql=E6=B3=A8?= =?UTF-8?q?=E5=85=A5=E6=BC=8F=E6=B4=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/internal/library/hgorm/dao.go | 29 ++++++++++++++++++++++++++++ server/internal/logic/sys/log.go | 15 ++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/server/internal/library/hgorm/dao.go b/server/internal/library/hgorm/dao.go index 27fb1cf..28eda72 100644 --- a/server/internal/library/hgorm/dao.go +++ b/server/internal/library/hgorm/dao.go @@ -14,6 +14,7 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/text/gstr" "hotgo/utility/convert" + "strings" ) type daoInstance interface { @@ -197,3 +198,31 @@ func IsUnique(ctx context.Context, dao daoInstance, where g.Map, message string, } return nil } + +// FilterKeywordsWithOr 多条件关键词OR查询 +func FilterKeywordsWithOr(m *gdb.Model, filterColumns map[string]string, keyword string) *gdb.Model { + if filterColumns == nil || len(filterColumns) == 0 { + return m + } + + conditions := make([]string, 0) + args := make([]interface{}, 0) + + for col, operator := range filterColumns { + val := keyword + var condition string + switch operator { + case "LIKE": + condition = fmt.Sprintf("%s LIKE ?", col) + val = "%" + keyword + "%" + default: + condition = fmt.Sprintf("%s = ?", col) + } + + conditions = append(conditions, condition) + args = append(args, val) + } + + filter := fmt.Sprintf("(%s)", strings.Join(conditions, " OR ")) + return m.Where(filter, args...) +} diff --git a/server/internal/logic/sys/log.go b/server/internal/logic/sys/log.go index 8747f22..eb01e48 100644 --- a/server/internal/logic/sys/log.go +++ b/server/internal/logic/sys/log.go @@ -23,6 +23,7 @@ import ( "hotgo/internal/global" "hotgo/internal/library/contexts" "hotgo/internal/library/dict" + "hotgo/internal/library/hgorm" "hotgo/internal/library/hgorm/handler" "hotgo/internal/library/hgorm/hook" "hotgo/internal/library/location" @@ -372,12 +373,14 @@ func (s *sSysLog) List(ctx context.Context, in *sysin.LogListInp) (list []*sysin // 非生产环境,允许关键词查询日志 // 生成环境使用需谨慎,日志量大易产生慢日志 if !gmode.IsProduct() && in.Keyword != "" { - mod = mod.Where("(`get_data` LIKE '%" + - in.Keyword + "%' or `post_data` LIKE '%" + - in.Keyword + "%' or `header_data` LIKE '%" + - in.Keyword + "%' or `error_data` LIKE '%" + - in.Keyword + "%' or `error_msg` LIKE '%" + - in.Keyword + "%')") + filterColumns := map[string]string{ + "get_data": "LIKE", + "post_data": "LIKE", + "header_data": "LIKE", + "error_data": "LIKE", + "error_msg": "LIKE", + } + mod = hgorm.FilterKeywordsWithOr(mod, filterColumns, in.Keyword) } totalCount, err = mod.Count()