Browse Source

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
master
dark 1 month ago
parent
commit
057cb12eba
  1. 26
      backend/internal/svc/servicecontext.go
  2. 7
      frontend/react-shadcn/pc/src/pages/AIChatPage.tsx

26
backend/internal/svc/servicecontext.go

@ -353,13 +353,6 @@ func seedRoleMenus(db *gorm.DB) {
} }
for _, r := range roles { 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 var menuIds []int64
switch r.Code { switch r.Code {
case model.RoleSuperAdmin, model.RoleAdmin: case model.RoleSuperAdmin, model.RoleAdmin:
@ -368,12 +361,23 @@ func seedRoleMenus(db *gorm.DB) {
menuIds = defaultMenuIds menuIds = defaultMenuIds
} }
if len(menuIds) > 0 { // 获取已有的菜单关联
records := make([]model.RoleMenu, 0, len(menuIds)) 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 { for _, menuId := range menuIds {
records = append(records, model.RoleMenu{RoleId: r.Id, MenuId: menuId}) 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") log.Println("[Seed] RoleMenus seeded successfully")

7
frontend/react-shadcn/pc/src/pages/AIChatPage.tsx

@ -148,7 +148,12 @@ export function AIChatPage() {
for await (const chunk of stream) { for await (const chunk of stream) {
try { try {
const parsed = JSON.parse(chunk) 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 accumulated += parsed.content
setMessages(prev => prev.map(m => setMessages(prev => prev.map(m =>
m.id === assistantMsg.id ? { ...m, content: accumulated } : m m.id === assistantMsg.id ? { ...m, content: accumulated } : m

Loading…
Cancel
Save