From 057cb12eba315b5a7ae54da72f34a128b3f97696 Mon Sep 17 00:00:00 2001 From: dark Date: Sat, 14 Feb 2026 22:50:27 +0800 Subject: [PATCH] fix: seedRoleMenus adds missing menus + SSE error display - seedRoleMenus now adds new menu items to existing roles - AIChatPage shows SSE error messages in assistant bubble --- backend/internal/svc/servicecontext.go | 28 +++++++++++-------- .../react-shadcn/pc/src/pages/AIChatPage.tsx | 7 ++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/backend/internal/svc/servicecontext.go b/backend/internal/svc/servicecontext.go index a4f32ab..4545392 100644 --- a/backend/internal/svc/servicecontext.go +++ b/backend/internal/svc/servicecontext.go @@ -353,13 +353,6 @@ func seedRoleMenus(db *gorm.DB) { } for _, r := range roles { - // 检查角色是否已有菜单关联 - var count int64 - db.Model(&model.RoleMenu{}).Where("role_id = ?", r.Id).Count(&count) - if count > 0 { - continue - } - var menuIds []int64 switch r.Code { case model.RoleSuperAdmin, model.RoleAdmin: @@ -368,12 +361,23 @@ func seedRoleMenus(db *gorm.DB) { menuIds = defaultMenuIds } - if len(menuIds) > 0 { - records := make([]model.RoleMenu, 0, len(menuIds)) - for _, menuId := range menuIds { - records = append(records, model.RoleMenu{RoleId: r.Id, MenuId: menuId}) + // 获取已有的菜单关联 + var existingMenuIds []int64 + db.Model(&model.RoleMenu{}).Where("role_id = ?", r.Id).Pluck("menu_id", &existingMenuIds) + existingSet := make(map[int64]bool) + for _, id := range existingMenuIds { + existingSet[id] = true + } + + // 添加缺失的菜单关联 + var newRecords []model.RoleMenu + for _, menuId := range menuIds { + if !existingSet[menuId] { + newRecords = append(newRecords, model.RoleMenu{RoleId: r.Id, MenuId: menuId}) } - db.Create(&records) + } + if len(newRecords) > 0 { + db.Create(&newRecords) } } log.Println("[Seed] RoleMenus seeded successfully") diff --git a/frontend/react-shadcn/pc/src/pages/AIChatPage.tsx b/frontend/react-shadcn/pc/src/pages/AIChatPage.tsx index 3130a03..5b89873 100644 --- a/frontend/react-shadcn/pc/src/pages/AIChatPage.tsx +++ b/frontend/react-shadcn/pc/src/pages/AIChatPage.tsx @@ -148,7 +148,12 @@ export function AIChatPage() { for await (const chunk of stream) { try { const parsed = JSON.parse(chunk) - if (parsed.content) { + if (parsed.error) { + accumulated = `Error: ${parsed.error}` + setMessages(prev => prev.map(m => + m.id === assistantMsg.id ? { ...m, content: accumulated, isStreaming: false } : m + )) + } else if (parsed.content) { accumulated += parsed.content setMessages(prev => prev.map(m => m.id === assistantMsg.id ? { ...m, content: accumulated } : m