> ## Documentation Index
> Fetch the complete documentation index at: https://docs.akria.net/llms.txt
> Use this file to discover all available pages before exploring further.

# 第3组：API 通信 | API & Communication

> 测试你对 REST, JSON, Request 等概念的理解

<a href="quiz" className="text-emerald-600 hover:underline text-sm">← 返回选单</a>

## 📝 词汇测验

export const QuizRunner = () => {
  const questions = [
    {
        "q": "'Endpoint' 是指：",
        "o": [
            "起点",
            "端点",
            "中继",
            "终结"
        ],
        "a": "端点",
        "h": "具体 API 地址。"
    },
    {
        "q": "'Payload' 意为：",
        "o": [
            "负载/载荷",
            "下载",
            "支付",
            "加载"
        ],
        "a": "负载/载荷",
        "h": "传输的 JSON 内容。"
    },
    {
        "q": "'Consumer' 是：",
        "o": [
            "生产者",
            "消费者",
            "管理者",
            "监听者"
        ],
        "a": "消费者",
        "h": "读取数据的一方。"
    },
    {
        "q": "'Producer' 是：",
        "o": [
            "消费者",
            "生产者",
            "测试者",
            "部署者"
        ],
        "a": "生产者",
        "h": "写入数据的一方。"
    },
    {
        "q": "'Request' 指：",
        "o": [
            "响应",
            "请求",
            "重写",
            "回收"
        ],
        "a": "请求",
        "h": "发送给 API 的数据。"
    },
    {
        "q": "'Response' 指：",
        "o": [
            "响应",
            "请求",
            "重复",
            "拒绝"
        ],
        "a": "响应",
        "h": "API 返回的数据。"
    },
    {
        "q": "'Header' 在 Web 请求中是：",
        "o": [
            "主体",
            "首部/头信息",
            "尾部",
            "签名"
        ],
        "a": "首部/头信息",
        "h": "包含元数据的部分。"
    },
    {
        "q": "'Token' 常用于：",
        "o": [
            "存储",
            "认证/令牌",
            "加密",
            "解密"
        ],
        "a": "认证/令牌",
        "h": "代表访问权限。"
    },
    {
        "q": "'Query' 意为：",
        "o": [
            "回答",
            "查询",
            "更新",
            "插入"
        ],
        "a": "查询",
        "h": "向数据库或 API 请求数据。"
    },
    {
        "q": "'Method' 在 REST 中指：",
        "o": [
            "属性",
            "方法/动作",
            "路径",
            "参数"
        ],
        "a": "方法/动作",
        "h": "如 GET, POST 等。"
    }
];

  const [idx, setIdx] = React.useState(0);
  const [scr, setScr] = React.useState(0);
  const [fin, setFin] = React.useState(false);
  const [sel, setSel] = React.useState(null);
  const [ok, setOk] = React.useState(null);
  const [ready, setReady] = React.useState(false);
  const [sq, setSq] = React.useState([]);

  React.useEffect(() => {
    setReady(true);
    setSq(questions.map(q => ({ ...q, o: [...q.o].sort(() => Math.random() - 0.5) })));
  }, []);

  const onAnswer = (o) => {
    if (sel) return;
    setSel(o);
    const isOk = o === questions[idx].a;
    setOk(isOk);
    if (isOk) setScr(scr + 1);
    setTimeout(() => {
      if (idx + 1 < questions.length) {
        setIdx(idx + 1);
        setSel(null);
        setOk(null);
      } else {
        setFin(true);
      }
    }, 1500);
  };

  if (!ready) return <div className="p-10 text-center opacity-50">Loading...</div>;

  if (fin) {
    return (
      <div className="p-10 rounded-2xl bg-white dark:bg-gray-900 border text-center shadow-xl">
        <div className="text-2xl font-bold text-emerald-600 mb-4">测试完成！</div>
        <div className="text-lg mb-8">得分: {scr} / {questions.length}</div>
        <div className="flex gap-4 justify-center">
            <button className="bg-emerald-600 hover:bg-emerald-700 text-white px-8 py-3 rounded-lg font-bold transition-all" onClick={() => {setIdx(0);setScr(0);setFin(false);setSel(null);setOk(null);}}>重新开始</button>
            <a href="quiz" className="bg-gray-100 hover:bg-gray-200 text-gray-700 px-8 py-3 rounded-lg font-bold transition-all no-underline">返回选单</a>
        </div>
      </div>
    );
  }

  const curr = sq[idx];
  if (!curr) return null;

  return (
    <div className="p-6 rounded-2xl bg-white dark:bg-gray-900 border shadow-lg">
      <div className="mb-4 text-xs opacity-50 font-mono text-right">进度: {idx + 1} / {questions.length}</div>
      
      <div className="h-1 bg-gray-100 rounded-full mb-6 overflow-hidden">
        <div className="h-full bg-emerald-500 transition-all duration-300" style={{width: ((idx + 1) / questions.length * 100) + '%'}}></div>
      </div>

      <div className="text-xl font-bold mb-8 text-gray-900 dark:text-gray-100">{curr.q}</div>
      
      <div className="space-y-3">
        {curr.o.map((o, i) => (
          <button 
            key={i} 
            className={"w-full text-left p-4 rounded-xl border-2 transition-all font-medium " + (sel === o ? (ok ? "bg-emerald-50 dark:bg-emerald-900/20 border-emerald-500 text-emerald-700 dark:text-emerald-400" : "bg-red-50 dark:bg-red-900/20 border-red-500 text-red-700 dark:text-red-400") : (sel && o === curr.a ? "bg-emerald-50 dark:bg-emerald-900/20 border-emerald-500 text-emerald-700 dark:text-emerald-400 opacity-80" : "bg-gray-50 dark:bg-gray-800 border-gray-100 dark:border-gray-700 hover:border-emerald-500 hover:shadow-md text-gray-700 dark:text-gray-300"))}
            onClick={() => onAnswer(o)}
          >
            {o}
          </button>
        ))}
      </div>
      
      {sel && (
        <div className={"mt-6 p-4 rounded-lg border-l-4 " + (ok ? "bg-emerald-50 dark:bg-emerald-900/20 border-emerald-500 text-emerald-800 dark:text-emerald-300" : "bg-red-50 dark:bg-red-900/20 border-red-500 text-red-800 dark:text-red-300")}>
          <div className="font-bold flex items-center gap-2">
            {ok ? "✅ 正确!" : "❌ 错误"}
            <span className="font-normal opacity-70 text-sm ml-auto">正确答案: {curr.a}</span>
          </div>
          <div className="text-sm opacity-80 mt-2">💡 提示: {curr.h}</div>
        </div>
      )}
    </div>
  );
};

<QuizRunner />
