2026-01-19 23:45:08 +08:00
|
|
|
|
<!--
|
2026-01-20 08:51:04 +08:00
|
|
|
|
ApiPlayground.vue - 简化版
|
|
|
|
|
|
目标:用最简单的演示展示 API 调用的各种情况
|
2026-01-19 23:45:08 +08:00
|
|
|
|
-->
|
|
|
|
|
|
<template>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<div class="demo">
|
|
|
|
|
|
<div class="title">🎮 练习场:试试调用 API</div>
|
|
|
|
|
|
<p class="subtitle">体验一下成功和失败的情况</p>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="playground">
|
|
|
|
|
|
<div class="controls">
|
|
|
|
|
|
<div class="control-group">
|
|
|
|
|
|
<label>🔑 钥匙(API Key):</label>
|
|
|
|
|
|
<button
|
|
|
|
|
|
:class="['toggle', { active: hasKey }]"
|
|
|
|
|
|
@click="hasKey = !hasKey"
|
|
|
|
|
|
>
|
|
|
|
|
|
{{ hasKey ? '✅ 有钥匙' : '❌ 没有钥匙' }}
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<div class="control-group">
|
|
|
|
|
|
<label>📍 用户 ID:</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
v-model="userId"
|
|
|
|
|
|
class="input"
|
|
|
|
|
|
placeholder="例如:u_123"
|
|
|
|
|
|
/>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<button class="call-btn" :disabled="calling" @click="callApi">
|
|
|
|
|
|
{{ calling ? '调用中...' : '🚀 调用 API' }}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<div class="result-area">
|
|
|
|
|
|
<div v-if="!result" class="placeholder">
|
|
|
|
|
|
还没有结果。点一下"调用 API"试试!
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<div v-else class="result" :class="result.type">
|
|
|
|
|
|
<div class="result-header">
|
|
|
|
|
|
{{ result.type === 'success' ? '✅ 成功' : '❌ 失败' }}
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<div class="result-body">{{ result.message }}</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
|
|
|
|
|
|
<div class="tips">
|
|
|
|
|
|
<p><strong>💡 玩法建议:</strong></p>
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
<li>试试把"钥匙"改成"没有钥匙",看看会发生什么</li>
|
|
|
|
|
|
<li>试试把 ID 改成 <code>u_404</code>,看看会怎样</li>
|
|
|
|
|
|
<li>连续快速点击,看看"限流"提示</li>
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
import { ref } from 'vue'
|
|
|
|
|
|
|
|
|
|
|
|
const hasKey = ref(true)
|
|
|
|
|
|
const userId = ref('u_123')
|
|
|
|
|
|
const calling = ref(false)
|
|
|
|
|
|
const result = ref(null)
|
|
|
|
|
|
const callCount = ref([])
|
|
|
|
|
|
const now = ref(Date.now())
|
|
|
|
|
|
|
|
|
|
|
|
function callApi() {
|
|
|
|
|
|
calling.value = true
|
|
|
|
|
|
result.value = null
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟限流
|
|
|
|
|
|
const currentTime = Date.now()
|
|
|
|
|
|
callCount.value = callCount.value.filter(t => currentTime - t < 2000)
|
|
|
|
|
|
callCount.value.push(currentTime)
|
|
|
|
|
|
|
|
|
|
|
|
if (callCount.value.length >= 4) {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
result.value = {
|
|
|
|
|
|
type: 'error',
|
|
|
|
|
|
message: '太频繁了!请慢一点再试(限流)'
|
|
|
|
|
|
}
|
|
|
|
|
|
calling.value = false
|
|
|
|
|
|
}, 300)
|
2026-01-19 23:45:08 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
// 检查钥匙
|
|
|
|
|
|
if (!hasKey.value) {
|
|
|
|
|
|
result.value = {
|
|
|
|
|
|
type: 'error',
|
|
|
|
|
|
message: '没有钥匙!你没有权限调用这个 API(401 未授权)'
|
|
|
|
|
|
}
|
|
|
|
|
|
calling.value = false
|
|
|
|
|
|
return
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
// 检查用户 ID
|
|
|
|
|
|
const id = userId.value.trim()
|
2026-01-19 23:45:08 +08:00
|
|
|
|
if (!id) {
|
2026-01-20 08:51:04 +08:00
|
|
|
|
result.value = {
|
|
|
|
|
|
type: 'error',
|
|
|
|
|
|
message: '你还没填用户 ID!'
|
|
|
|
|
|
}
|
|
|
|
|
|
calling.value = false
|
2026-01-19 23:45:08 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-01-20 08:51:04 +08:00
|
|
|
|
|
2026-01-19 23:45:08 +08:00
|
|
|
|
if (id === 'u_404') {
|
2026-01-20 08:51:04 +08:00
|
|
|
|
result.value = {
|
|
|
|
|
|
type: 'error',
|
|
|
|
|
|
message: '找不到这个用户!ID 不存在(404)'
|
|
|
|
|
|
}
|
|
|
|
|
|
calling.value = false
|
2026-01-19 23:45:08 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
// 成功
|
|
|
|
|
|
result.value = {
|
|
|
|
|
|
type: 'success',
|
|
|
|
|
|
message: `成功获取用户信息:\nID: ${id}\n姓名: 张三\n邮箱: zhang@example.com`
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
2026-01-20 08:51:04 +08:00
|
|
|
|
calling.value = false
|
|
|
|
|
|
}, 800)
|
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;
|
|
|
|
|
|
padding: 20px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
margin: 16px 0;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.title {
|
2026-01-20 08:51:04 +08:00
|
|
|
|
font-size: 18px;
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
margin-bottom: 8px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.subtitle {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
margin-bottom: 16px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.playground {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
border: 2px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
|
padding: 20px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.controls {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
display: flex;
|
2026-01-20 08:51:04 +08:00
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 16px;
|
|
|
|
|
|
margin-bottom: 20px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.control-group {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
display: flex;
|
2026-01-20 08:51:04 +08:00
|
|
|
|
align-items: center;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
gap: 12px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.control-group label {
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
|
min-width: 120px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.toggle {
|
|
|
|
|
|
padding: 8px 16px;
|
|
|
|
|
|
border: 2px solid var(--vp-c-divider);
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
cursor: pointer;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
font-size: 13px;
|
2026-01-20 08:51:04 +08:00
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
transition: all 0.2s;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.toggle.active {
|
|
|
|
|
|
border-color: #22c55e;
|
|
|
|
|
|
background: #dcfce7;
|
|
|
|
|
|
color: #166534;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.input {
|
|
|
|
|
|
flex: 1;
|
|
|
|
|
|
padding: 8px 12px;
|
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: 6px;
|
|
|
|
|
|
font-size: 14px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.call-btn {
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
padding: 12px 20px;
|
|
|
|
|
|
background: var(--vp-c-brand-1);
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
border: none;
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
transition: all 0.2s;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.call-btn:hover:not(:disabled) {
|
|
|
|
|
|
opacity: 0.9;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.call-btn:disabled {
|
|
|
|
|
|
opacity: 0.6;
|
|
|
|
|
|
cursor: not-allowed;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.result-area {
|
|
|
|
|
|
min-height: 120px;
|
|
|
|
|
|
margin-bottom: 20px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.placeholder {
|
|
|
|
|
|
padding: 20px;
|
|
|
|
|
|
text-align: center;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
font-style: italic;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.result {
|
|
|
|
|
|
border: 2px solid;
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
overflow: hidden;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.result.success {
|
|
|
|
|
|
border-color: #22c55e;
|
|
|
|
|
|
background: #f0fdf4;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.result.error {
|
|
|
|
|
|
border-color: #ef4444;
|
|
|
|
|
|
background: #fef2f2;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.result-header {
|
|
|
|
|
|
padding: 12px 16px;
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
font-size: 14px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.result.success .result-header {
|
|
|
|
|
|
background: #dcfce7;
|
|
|
|
|
|
color: #166534;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.result.error .result-header {
|
|
|
|
|
|
background: #fee2e2;
|
|
|
|
|
|
color: #991b1b;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.result-body {
|
|
|
|
|
|
padding: 12px 16px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
font-size: 13px;
|
2026-01-20 08:51:04 +08:00
|
|
|
|
white-space: pre-line;
|
|
|
|
|
|
line-height: 1.6;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.tips {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
padding: 16px;
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
line-height: 1.6;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.tips p {
|
|
|
|
|
|
margin-bottom: 8px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.tips ul {
|
|
|
|
|
|
margin: 0;
|
|
|
|
|
|
padding-left: 20px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.tips li {
|
|
|
|
|
|
margin: 4px 0;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.tips code {
|
|
|
|
|
|
background: #1e293b;
|
|
|
|
|
|
color: #e2e8f0;
|
|
|
|
|
|
padding: 2px 6px;
|
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
font-family: 'Monaco', 'Menlo', monospace;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
font-size: 12px;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|