2026-01-19 23:45:08 +08:00
|
|
|
|
<!--
|
2026-01-20 17:53:22 +08:00
|
|
|
|
ApiConceptDemo.vue - 互动点餐版
|
|
|
|
|
|
目标:通过"点菜"的过程演示 API 的三个核心要素
|
2026-01-19 23:45:08 +08:00
|
|
|
|
-->
|
|
|
|
|
|
<template>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<div class="demo">
|
2026-01-20 17:53:22 +08:00
|
|
|
|
<div class="header">
|
|
|
|
|
|
<span class="icon">🍳</span>
|
|
|
|
|
|
<span class="title">互动演示:向 AI 厨房点菜</span>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
<div class="stepper">
|
|
|
|
|
|
<!-- Step 1: Endpoint -->
|
|
|
|
|
|
<div class="step-group">
|
|
|
|
|
|
<div class="step-label">1. 跟谁说?(Endpoint)</div>
|
|
|
|
|
|
<select v-model="endpoint" class="control">
|
|
|
|
|
|
<option value="/kitchen/chef">👨🍳 主厨 (/kitchen/chef)</option>
|
|
|
|
|
|
<option value="/kitchen/bar">🍸 调酒师 (/kitchen/bar)</option>
|
|
|
|
|
|
</select>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Step 2: Method -->
|
|
|
|
|
|
<div class="step-group">
|
|
|
|
|
|
<div class="step-label">2. 怎么说?(Method)</div>
|
|
|
|
|
|
<div class="toggle-group">
|
|
|
|
|
|
<button
|
|
|
|
|
|
:class="['toggle-btn', { active: method === 'GET' }]"
|
|
|
|
|
|
@click="method = 'GET'"
|
|
|
|
|
|
>
|
|
|
|
|
|
GET (看看有什么)
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
:class="['toggle-btn', { active: method === 'POST' }]"
|
|
|
|
|
|
@click="method = 'POST'"
|
|
|
|
|
|
>
|
|
|
|
|
|
POST (我要下单)
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
|
|
|
|
|
|
<!-- Step 3: Params -->
|
|
|
|
|
|
<div class="step-group" v-if="method === 'POST'">
|
|
|
|
|
|
<div class="step-label">3. 点什么?(Body)</div>
|
|
|
|
|
|
<div class="params-editor">
|
|
|
|
|
|
{ "food":
|
|
|
|
|
|
<select v-model="food" class="inline-select">
|
|
|
|
|
|
<option value="steak">🥩 牛排</option>
|
|
|
|
|
|
<option value="pasta">🍝 意面</option>
|
|
|
|
|
|
<option value="salad">🥗 沙拉</option>
|
|
|
|
|
|
</select>
|
|
|
|
|
|
}
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
<div class="step-group" v-else>
|
|
|
|
|
|
<div class="step-label">3. 查什么?(Params)</div>
|
|
|
|
|
|
<div class="params-editor">
|
|
|
|
|
|
?type=
|
|
|
|
|
|
<select v-model="menuType" class="inline-select">
|
|
|
|
|
|
<option value="today">📅 今日特供</option>
|
|
|
|
|
|
<option value="all">📜 全部菜单</option>
|
|
|
|
|
|
</select>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Action -->
|
|
|
|
|
|
<button class="send-btn" @click="sendRequest" :disabled="loading">
|
|
|
|
|
|
{{ loading ? '🍳 正在做...' : '🚀 发送请求' }}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Result -->
|
|
|
|
|
|
<div class="result-box" v-if="response">
|
|
|
|
|
|
<div class="result-header">
|
|
|
|
|
|
<span class="badge" :class="response.status === 200 ? 'success' : 'error'">
|
|
|
|
|
|
{{ response.status }} {{ response.statusText }}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="result-content">
|
|
|
|
|
|
{{ response.data }}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="result-explanation">
|
|
|
|
|
|
💡 <strong>解释:</strong> {{ response.explanation }}
|
|
|
|
|
|
</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
import { ref, watch } from 'vue'
|
|
|
|
|
|
|
|
|
|
|
|
const endpoint = ref('/kitchen/chef')
|
|
|
|
|
|
const method = ref('GET')
|
|
|
|
|
|
const food = ref('steak')
|
|
|
|
|
|
const menuType = ref('today')
|
|
|
|
|
|
const loading = ref(false)
|
|
|
|
|
|
const response = ref(null)
|
|
|
|
|
|
|
|
|
|
|
|
// Reset response when inputs change
|
|
|
|
|
|
watch([endpoint, method, food, menuType], () => {
|
|
|
|
|
|
response.value = null
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
function sendRequest() {
|
|
|
|
|
|
loading.value = true
|
|
|
|
|
|
response.value = null
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
loading.value = false
|
|
|
|
|
|
|
|
|
|
|
|
// Logic for different combinations
|
|
|
|
|
|
if (endpoint.value === '/kitchen/bar') {
|
|
|
|
|
|
if (method.value === 'GET') {
|
|
|
|
|
|
response.value = {
|
|
|
|
|
|
status: 200,
|
|
|
|
|
|
statusText: 'OK',
|
|
|
|
|
|
data: { menu: ['Mojito', 'Martini', 'Beer'] },
|
|
|
|
|
|
explanation: '你问调酒师有哪些酒,他给了你酒单。'
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
response.value = {
|
|
|
|
|
|
status: 400,
|
|
|
|
|
|
statusText: 'Bad Request',
|
|
|
|
|
|
data: { error: "Bar only serves drinks, not food!" },
|
|
|
|
|
|
explanation: '你试图向调酒师点菜(牛排/意面),他拒绝了你。你应该去 /kitchen/chef 点菜,或者只点酒。'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Chef logic
|
|
|
|
|
|
if (method.value === 'GET') {
|
|
|
|
|
|
response.value = {
|
|
|
|
|
|
status: 200,
|
|
|
|
|
|
statusText: 'OK',
|
|
|
|
|
|
data: { specials: ['Spicy Chicken', 'Tofu Soup'] },
|
|
|
|
|
|
explanation: '你问主厨今天有什么特供,他告诉了你。'
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// POST to Chef
|
|
|
|
|
|
const foodMap = {
|
|
|
|
|
|
steak: '🥩 滋滋作响的牛排',
|
|
|
|
|
|
pasta: '🍝 香气扑鼻的意面',
|
|
|
|
|
|
salad: '🥗 新鲜健康的沙拉'
|
|
|
|
|
|
}
|
|
|
|
|
|
response.value = {
|
|
|
|
|
|
status: 200,
|
|
|
|
|
|
statusText: 'Created',
|
|
|
|
|
|
data: { dish: foodMap[food.value], message: "Enjoy your meal!" },
|
|
|
|
|
|
explanation: `你向主厨下了单 (${food.value}),主厨为你做好了菜。`
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}, 600)
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.demo {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
border-radius: 12px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-01-20 17:53:22 +08:00
|
|
|
|
margin: 24px 0;
|
|
|
|
|
|
overflow: hidden;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.header {
|
|
|
|
|
|
padding: 12px 20px;
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
border-bottom: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 10px;
|
|
|
|
|
|
font-weight: 600;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.stepper {
|
|
|
|
|
|
padding: 20px;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 16px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.step-group {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 8px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.step-label {
|
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
text-transform: uppercase;
|
|
|
|
|
|
letter-spacing: 0.5px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.control, .inline-select {
|
|
|
|
|
|
padding: 8px 12px;
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
2026-01-20 17:53:22 +08:00
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
font-size: 14px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.toggle-group {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
background: var(--vp-c-divider);
|
|
|
|
|
|
padding: 2px;
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
width: fit-content;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.toggle-btn {
|
|
|
|
|
|
padding: 6px 16px;
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
border: none;
|
|
|
|
|
|
background: transparent;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
transition: all 0.2s;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.toggle-btn.active {
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
color: var(--vp-c-brand-1);
|
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
|
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.params-editor {
|
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
padding: 12px;
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 8px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.send-btn {
|
|
|
|
|
|
margin-top: 8px;
|
|
|
|
|
|
background: var(--vp-c-brand-1);
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
border: none;
|
|
|
|
|
|
padding: 12px;
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
transition: opacity 0.2s;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.send-btn:disabled {
|
|
|
|
|
|
opacity: 0.7;
|
|
|
|
|
|
cursor: not-allowed;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.result-box {
|
|
|
|
|
|
margin: 0 20px 20px;
|
|
|
|
|
|
background: #1e293b;
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
color: #e2e8f0;
|
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
animation: slideDown 0.3s ease;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.result-header {
|
|
|
|
|
|
padding: 8px 12px;
|
|
|
|
|
|
background: rgba(0,0,0,0.3);
|
2026-01-19 23:45:08 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.badge {
|
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
|
padding: 2px 6px;
|
|
|
|
|
|
border-radius: 4px;
|
2026-01-20 08:51:04 +08:00
|
|
|
|
font-weight: bold;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.badge.success { background: #22c55e; color: #fff; }
|
|
|
|
|
|
.badge.error { background: #ef4444; color: #fff; }
|
2026-01-19 23:45:08 +08:00
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.result-content {
|
2026-01-20 08:51:04 +08:00
|
|
|
|
padding: 16px;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
white-space: pre-wrap;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.result-explanation {
|
2026-01-20 08:51:04 +08:00
|
|
|
|
padding: 12px;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
background: #334155;
|
|
|
|
|
|
font-family: var(--vp-font-family-base);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
font-size: 13px;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
border-top: 1px solid rgba(255,255,255,0.1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@keyframes slideDown {
|
|
|
|
|
|
from { opacity: 0; transform: translateY(-10px); }
|
|
|
|
|
|
to { opacity: 1; transform: translateY(0); }
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|