From 8a8a96a0127311fcf05041189e1cc809997a874c Mon Sep 17 00:00:00 2001 From: chengqian Date: Thu, 30 Jul 2026 14:26:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(admin):=20=E4=BF=AE=E5=A4=8D=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=97=A5=E5=BF=97=E5=88=97=E8=A1=A8=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=A9=BA=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/admin/audit.go | 28 ++++++++++++++++----- internal/logic/admin/audit_test.go | 36 +++++++++++++++++++++++++++ internal/logic/admin/channel_error.go | 17 ------------- 3 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 internal/logic/admin/audit_test.go diff --git a/internal/logic/admin/audit.go b/internal/logic/admin/audit.go index 66b8c20..6a77405 100644 --- a/internal/logic/admin/audit.go +++ b/internal/logic/admin/audit.go @@ -95,6 +95,15 @@ func queryPage(ctx context.Context, table, fields, where, orderBy string, page, if where != "" { m = m.Where(where, args...) } + + total, err := m.Count() + if err != nil { + return nil, 0, err + } + if total == 0 { + return []map[string]any{}, 0, nil + } + if fields != "" { m = m.Fields(fields) } @@ -102,16 +111,23 @@ func queryPage(ctx context.Context, table, fields, where, orderBy string, page, m = m.Order(orderBy) } - var items []map[string]any - var total int - err := m.Page(page, pageSize).ScanAndCount(&items, &total, false) + result, err := m.Page(page, pageSize).All() if err != nil { return nil, 0, err } - if items == nil { - items = []map[string]any{} + return toMapList(result), total, nil +} + +// toMapList converts database records to JSON-safe maps. +func toMapList(result gdb.Result) []map[string]any { + if len(result) == 0 { + return []map[string]any{} } - return items, total, nil + list := make([]map[string]any, 0, len(result)) + for _, row := range result { + list = append(list, row.Map()) + } + return list } // queryAuditPage 查询审计表分页数据,自动计数、分页、转换并批量填充关联名称。 diff --git a/internal/logic/admin/audit_test.go b/internal/logic/admin/audit_test.go new file mode 100644 index 0000000..d9b1248 --- /dev/null +++ b/internal/logic/admin/audit_test.go @@ -0,0 +1,36 @@ +package admin + +import ( + "encoding/json" + "testing" + + "github.com/gogf/gf/v2/container/gvar" + "github.com/gogf/gf/v2/database/gdb" +) + +func TestToMapListProducesJSONObjects(t *testing.T) { + t.Parallel() + + result := gdb.Result{ + gdb.Record{ + "id": gvar.New(9), + "error_message": gvar.New("request failed"), + }, + } + + list := toMapList(result) + if len(list) != 1 { + t.Fatalf("toMapList() length = %d, want 1", len(list)) + } + if list[0] == nil { + t.Fatal("toMapList() returned a nil map") + } + + data, err := json.Marshal(list) + if err != nil { + t.Fatalf("marshal list: %v", err) + } + if got, want := string(data), `[{"error_message":"request failed","id":9}]`; got != want { + t.Fatalf("marshal list = %s, want %s", got, want) + } +} diff --git a/internal/logic/admin/channel_error.go b/internal/logic/admin/channel_error.go index c00c62f..b1fb755 100644 --- a/internal/logic/admin/channel_error.go +++ b/internal/logic/admin/channel_error.go @@ -5,7 +5,6 @@ import ( "strings" "time" - "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" @@ -196,19 +195,3 @@ func (s *sAdmin) ChannelErrorCategories(ctx context.Context, req *v1.ChannelErro }, }, nil } - -// toMapList 将 Result 转为 []map[string]any -func toMapList(result gdb.Result) []map[string]any { - if result == nil || len(result) == 0 { - return []map[string]any{} - } - list := make([]map[string]any, len(result)) - for i, row := range result { - data := make(map[string]any, len(row)) - for k, v := range row { - data[k] = v.Val() - } - list[i] = data - } - return list -}