Commit aa5b7193 authored by yuguo's avatar yuguo

fix

parent 000d22a8
......@@ -4,58 +4,59 @@ import (
"context"
"fmt"
"strings"
"sync"
"time"
"internet-hospital/internal/model"
"internet-hospital/pkg/agent"
"internet-hospital/pkg/database"
)
// routeRegistry 后端路由注册表(与前端 config/routes.ts 对应)
// page_code → 默认路由路径
var routeRegistry = map[string]string{
// 管理端
"admin_dashboard": "/admin/dashboard",
"admin_patients": "/admin/patients",
"admin_doctors": "/admin/doctors",
"admin_admins": "/admin/admins",
"admin_departments": "/admin/departments",
"admin_consultations": "/admin/consultations",
"admin_prescription": "/admin/prescription",
"admin_pharmacy": "/admin/pharmacy",
"admin_users": "/admin/users",
"admin_doctor_review": "/admin/doctor-review",
"admin_statistics": "/admin/statistics",
"admin_ai_config": "/admin/ai-config",
"admin_ai_center": "/admin/ai-center",
"admin_agents": "/admin/agents",
"admin_tools": "/admin/tools",
"admin_workflows": "/admin/workflows",
"admin_tasks": "/admin/tasks",
"admin_knowledge": "/admin/knowledge",
"admin_safety": "/admin/safety",
"admin_compliance": "/admin/compliance",
"admin_roles": "/admin/roles",
"admin_menus": "/admin/menus",
// 医生端
"doctor_workbench": "/doctor/workbench",
"doctor_consult": "/doctor/consult",
"doctor_patient": "/doctor/patient",
"doctor_schedule": "/doctor/schedule",
"doctor_profile": "/doctor/profile",
"doctor_certification": "/doctor/certification",
"doctor_income": "/doctor/income",
"doctor_ai_assist": "/doctor/ai-assist",
"doctor_tasks": "/doctor/tasks",
"doctor_chronic_review": "/doctor/chronic/review",
// 患者端
"patient_home": "/patient/home",
"patient_doctors": "/patient/doctors",
"patient_consult": "/patient/consult",
"patient_prescription": "/patient/prescription",
"patient_health_records": "/patient/health-records",
"patient_chronic": "/patient/chronic",
"patient_payment": "/patient/payment",
"patient_pre_consult": "/patient/pre-consult",
"patient_profile": "/patient/profile",
// routeCache 路由缓存(从数据库 menus 表动态加载)
var (
routeCache map[string]string
routeCacheMu sync.RWMutex
routeCacheTime time.Time
routeCacheTTL = 5 * time.Minute // 缓存5分钟
)
// getRouteRegistry 从数据库查询路由注册表(带缓存)
func getRouteRegistry() map[string]string {
routeCacheMu.RLock()
if routeCache != nil && time.Since(routeCacheTime) < routeCacheTTL {
defer routeCacheMu.RUnlock()
return routeCache
}
routeCacheMu.RUnlock()
// 重新加载
routeCacheMu.Lock()
defer routeCacheMu.Unlock()
db := database.GetDB()
if db == nil {
return make(map[string]string)
}
var menus []model.Menu
db.Where("path != '' AND path IS NOT NULL").Find(&menus)
registry := make(map[string]string, len(menus))
for _, m := range menus {
if m.Path == "" {
continue
}
// 从 Path 生成 page_code: /admin/patients → admin_patients
pageCode := strings.TrimPrefix(m.Path, "/")
pageCode = strings.ReplaceAll(pageCode, "/", "_")
if pageCode != "" {
registry[pageCode] = m.Path
}
}
routeCache = registry
routeCacheTime = time.Now()
return registry
}
// NavigatePageTool 页面导航工具 — 让 AI 助手能够控制前端页面跳转(v15 升级)
......@@ -64,9 +65,10 @@ type NavigatePageTool struct{}
func (t *NavigatePageTool) Name() string { return "navigate_page" }
func (t *NavigatePageTool) Description() string { return "导航到互联网医院系统页面" }
func (t *NavigatePageTool) Parameters() []agent.ToolParameter {
// 构建 page_code 枚举列表
codes := make([]string, 0, len(routeRegistry))
for code := range routeRegistry {
// 动态从数据库构建 page_code 枚举列表
registry := getRouteRegistry()
codes := make([]string, 0, len(registry))
for code := range registry {
codes = append(codes, code)
}
return []agent.ToolParameter{
......@@ -122,12 +124,13 @@ func (t *NavigatePageTool) Execute(ctx context.Context, params map[string]interf
}
}
// 从路由注册表查找基础路径
basePath, ok := routeRegistry[pageCode]
// 从数据库查询路由注册表
registry := getRouteRegistry()
basePath, ok := registry[pageCode]
if !ok {
// 兼容旧格式: admin/doctors → admin_doctors
normalizedCode := strings.ReplaceAll(pageCode, "/", "_")
basePath, ok = routeRegistry[normalizedCode]
basePath, ok = registry[normalizedCode]
if !ok {
// 最后尝试直接当路径用
basePath = "/" + strings.ReplaceAll(pageCode, "_", "/")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment