Skip to content

Commit 2240387

Browse files
committed
feat: 添加前端短链跳转页面,支持通过后端API获取长链接后跳转
1 parent 366824c commit 2240387

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

web/src/pages/RedirectPage.vue

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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>

web/src/router/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ const StatsPage = () => import('../pages/StatsPage.vue')
55
const LoginPage = () => import('../pages/LoginPage.vue')
66
const AboutPage = () => import('../pages/AboutPage.vue')
77
const HomePage = () => import('../pages/HomePage.vue')
8+
const RedirectPage = () => import('../pages/RedirectPage.vue')
89

910
const routes = [
1011
{ path: '/', component: HomePage, meta: { public: true, hideNav: false } },
1112
{ path: '/login', component: LoginPage, meta: { public: true, hideNav: true } },
1213
{ path: '/dashboard', component: DashboardPage },
1314
{ path: '/stats/:shortCode', component: StatsPage },
1415
{ path: '/about', component: AboutPage, meta: { public: true } },
16+
// 短链跳转路由(匹配所有未定义的路径)
17+
{ path: '/:shortCode', component: RedirectPage, meta: { public: true, hideNav: true } },
1518
]
1619

1720
const router = createRouter({

0 commit comments

Comments
 (0)