|
| 1 | +<template> |
| 2 | + <div class="min-h-screen flex items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-50"> |
| 3 | + <div class="text-center space-y-6 p-8"> |
| 4 | + <!-- 加载动画 --> |
| 5 | + <div v-if="loading" class="space-y-4"> |
| 6 | + <div class="inline-block"> |
| 7 | + <svg class="animate-spin h-16 w-16 text-blue-600" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"> |
| 8 | + <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle> |
| 9 | + <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path> |
| 10 | + </svg> |
| 11 | + </div> |
| 12 | + <div class="text-xl font-medium text-gray-700">正在跳转...</div> |
| 13 | + <div class="text-sm text-gray-500">{{ shortCode }}</div> |
| 14 | + </div> |
| 15 | + |
| 16 | + <!-- 错误提示 --> |
| 17 | + <div v-if="error" class="space-y-4"> |
| 18 | + <div class="text-6xl">😢</div> |
| 19 | + <div class="text-2xl font-semibold text-gray-800">链接不存在</div> |
| 20 | + <div class="text-gray-600">短码 <span class="font-mono bg-gray-100 px-2 py-1 rounded">{{ shortCode }}</span> 未找到</div> |
| 21 | + <button @click="goHome" class="mt-4 px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"> |
| 22 | + 返回首页 |
| 23 | + </button> |
| 24 | + </div> |
| 25 | + </div> |
| 26 | + </div> |
| 27 | +</template> |
| 28 | + |
| 29 | +<script setup> |
| 30 | +import { ref, onMounted } from 'vue' |
| 31 | +import { useRoute, useRouter } from 'vue-router' |
| 32 | +import axios from 'axios' |
| 33 | +import { API_BASE } from '../composables/shortBase' |
| 34 | +
|
| 35 | +const route = useRoute() |
| 36 | +const router = useRouter() |
| 37 | +const shortCode = ref(route.params.shortCode) |
| 38 | +const loading = ref(true) |
| 39 | +const error = ref(false) |
| 40 | +
|
| 41 | +onMounted(async () => { |
| 42 | + try { |
| 43 | + // 调用后端跳转接口 |
| 44 | + const response = await axios.get(`${API_BASE}/api/redirect/${shortCode.value}`, { |
| 45 | + maxRedirects: 0, // 禁止 axios 自动跟随重定向 |
| 46 | + validateStatus: (status) => status === 302 || status === 301 || status < 400 |
| 47 | + }) |
| 48 | +
|
| 49 | + // 从响应头获取 Location |
| 50 | + const location = response.headers.location || response.headers.Location |
| 51 | + |
| 52 | + if (location) { |
| 53 | + // 跳转到目标网址 |
| 54 | + window.location.href = location |
| 55 | + } else { |
| 56 | + // 如果没有 Location 头,可能后端直接返回了长链接 |
| 57 | + if (response.data && typeof response.data === 'string' && response.data.startsWith('http')) { |
| 58 | + window.location.href = response.data |
| 59 | + } else { |
| 60 | + error.value = true |
| 61 | + loading.value = false |
| 62 | + } |
| 63 | + } |
| 64 | + } catch (e) { |
| 65 | + console.error('短链跳转失败:', e) |
| 66 | + error.value = true |
| 67 | + loading.value = false |
| 68 | + } |
| 69 | +}) |
| 70 | +
|
| 71 | +function goHome() { |
| 72 | + router.push('/') |
| 73 | +} |
| 74 | +</script> |
| 75 | + |
| 76 | +<style scoped> |
| 77 | +/* 可以添加自定义样式 */ |
| 78 | +</style> |
0 commit comments