import { useEffect, useState } from 'react' import { Link, useNavigate } from 'react-router-dom' import { useCMSTexts } from '@/services/cms' type Q = { id:string, group_key:string, group_name:string, type:string, title:string, options:any[], sort_order:number } const STORAGE_KEY='diagnosis_progress' export default function Diagnosis(){ const { data: texts } = useCMSTexts() const [questions,setQuestions]=useState([]) const [answers,setAnswers]=useState>({}) const [idx,setIdx]=useState(0) const nav=useNavigate() const [loading,setLoading]=useState(true) useEffect(()=>{ fetch('/api/diagnosis/questions').then(r=>r.json()).then(j=>{ const list = (j.data||[]).sort((a:Q,b:Q)=> b.sort_order - a.sort_order) setQuestions(list) // 断点续填 const saved = localStorage.getItem(STORAGE_KEY) if(saved){ try{ const p=JSON.parse(saved); if(p.answers) setAnswers(p.answers); if(typeof p.idx==='number') setIdx(p.idx)}catch{} } setLoading(false) }) },[]) useEffect(()=>{ localStorage.setItem(STORAGE_KEY, JSON.stringify({answers, idx})) },[answers, idx]) const q = questions[idx] const progress = questions.length? Math.round(((idx)/questions.length)*100):0 const answeredCount = Object.keys(answers).length const handleAnswer=(val:any)=>{ const next={...answers, [q.id]:val} setAnswers(next) } const nextQ=()=>{ if(idx setIdx(Math.max(0, idx-1)) const submit=async()=>{ // 创建会话并提交 const sess = await fetch('/api/diagnosis/sessions',{method:'POST'}).then(r=>r.json()) const token = sess.data.token await fetch(`/api/diagnosis/sessions/${token}`,{method:'PUT',headers:{'Content-Type':'application/json'},body:JSON.stringify({answers})}) const rep = await fetch(`/api/diagnosis/sessions/${token}/submit`,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({answers})}).then(r=>r.json()) localStorage.removeItem(STORAGE_KEY) nav(`/diagnosis/report/${rep.data.token}`) } if(loading) return
加载问卷... <2s
if(!q) return
暂无可用题目,请稍后再试。
const isAnswered = answers[q.id]!==undefined return (

企业进化诊断

15-20题 · 按四大价值分组 · 区间选择无需精确填写 · 免责:初步分析仅供参考

{idx+1}/{questions.length} · 已答 {answeredCount} 题 · 本地自动保存,刷新不丢失
{q.group_name} · {q.type==='scale'?'量表 1-5':q.type==='multiple'?'多选':'单选'}

{idx+1}. {q.title}

{q.options.map((opt:any)=> { const selected = q.type==='multiple' ? (Array.isArray(answers[q.id]) && answers[q.id].includes(opt.label)) : answers[q.id]===opt.label return ( ) })}
{texts['diagnosis.input_note'] || '采用区间选择,不收集敏感财务数据。'}
返回轻量对照
) }