{"record":{"id":"acc9d8b54deef0db","repo":"flipped-aurora/gin-vue-admin","slug":"error-acc9d8","errorCode":null,"errorMessage":"找不到默认路由,无法切换本角色","messagePattern":"找不到默认路由,无法切换本角色","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_user.go","lineNumber":186,"sourceCode":"\n\tfor i := range authorityMenu {\n\t\tauthorityMenuIDs = append(authorityMenuIDs, authorityMenu[i].MenuId)\n\t}\n\n\tvar authorityMenus []system.SysBaseMenu\n\terr = global.GVA_DB.WithContext(ctx).Preload(\"Parameters\").Where(\"id in (?)\", authorityMenuIDs).Find(&authorityMenus).Error\n\tif err != nil {\n\t\treturn err\n\t}\n\thasMenu := false\n\tfor i := range authorityMenus {\n\t\tif authorityMenus[i].Name == authority.DefaultRouter {\n\t\t\thasMenu = true\n\t\t\tbreak\n\t\t}\n\t}\n\tif !hasMenu {\n\t\treturn errors.New(\"找不到默认路由,无法切换本角色\")\n\t}\n\n\terr = global.GVA_DB.WithContext(ctx).Model(&system.SysUser{}).Where(\"id = ?\", id).Update(\"authority_id\", authorityId).Error\n\treturn err\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: SetUserAuthorities\n//@description: 设置一个用户的权限\n//@param: id uint, authorityIds []string\n//@return: err error\n\nfunc (userService *UserService) SetUserAuthorities(ctx context.Context, adminAuthorityID, id uint, authorityIds []uint) (err error) {\n\treturn global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {\n\t\tvar user system.SysUser\n\t\tTxErr := tx.Where(\"id = ?\", id).First(&user).Error\n\t\tif TxErr != nil {\n\t\t\tlogger.WithCtx(ctx).Mod(\"biz\").Debug(TxErr.Error())","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_user.go#L168-L204","documentation":"When switching a user's active authority, SetUserAuthority checks that the target role's DefaultRouter is among that role's menus (authorityMenus). If no menu name matches authority.DefaultRouter, it returns errors.New(\"找不到默认路由,无法切换本角色\"), because after switching, the user would land on a route they cannot access.","triggerScenarios":"Calling SetUserAuthority (sys_user.go:186) with a role whose DefaultRouter field names a menu that is not assigned to the role (missing from sys_authority_menus), or where the menu name does not exactly equal DefaultRouter.","commonSituations":"Admin creating a new role but forgetting to set a default router or not assigning the default-route menu to the role; renaming a menu without updating DefaultRouter; copying a role but not its menus.","solutions":["Open role management and assign the menu matching DefaultRouter to the target authority.","Or edit the authority's DefaultRouter to the Name of one of its assigned menus.","Verify menu Name (route name) matches DefaultRouter exactly — case-sensitive, no leading slashes.","Re-run menu/authority initialization data if the role's menu relations were seeded incorrectly."],"exampleFix":"// before\nauthority.DefaultRouter = \"dashboard\" // no menu named \"dashboard\" assigned\n// after\nauthority.DefaultRouter = assignedMenus[0].Name // e.g. an actually assigned menu name","handlingStrategy":"validation","validationCode":"// client pre-check before switching\nconst target = authorities.find(a => a.authorityId === id)\nif (!target.menus.some(m => m.name === target.defaultRouter)) {\n  ElMessage.warning('该角色未配置默认路由菜单')\n}","typeGuard":null,"tryCatchPattern":"try {\n  await setUserAuthority({ id, authorityId })\n} catch (e) {\n  if (e.message.includes('找不到默认路由')) ElMessage.error('请先为该角色设置默认路由并分配对应菜单')\n  else throw e\n}","preventionTips":["When creating a role, always assign at least one menu and set DefaultRouter to its exact Name.","Keep DefaultRouter in sync whenever menus are renamed.","Add a role-save validation that DefaultRouter exists in the assigned menu list."],"tags":["roles","authority","menu","router","business-rule"],"backgroundTag":"default-router-menu-missing","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}