This commit is contained in:
2025-07-21 21:43:15 +08:00
parent 95c12f0b82
commit e25f252bed
4 changed files with 20 additions and 217 deletions

View File

@@ -22,13 +22,20 @@ export const useAdminStore = defineStore('admin', () => {
const isLoggedIn = computed(() => !!token.value && !!admin.value)
// 初始化认证状态
const initAuth = () => {
const initAuth = async () => {
const storedToken = adminAuth.getToken()
const storedAdmin = adminAuth.getAdminInfo()
if (storedToken && storedAdmin) {
token.value = storedToken
admin.value = storedAdmin
try {
await getStats()
} catch (error) {
// token失效清除本地存储
logout()
console.error('Admin token is invalid, logging out')
}
}
}

View File

@@ -15,15 +15,20 @@ export const useAuthStore = defineStore('auth', () => {
const isLoggedIn = computed(() => !!token.value && !!user.value)
// 初始化认证状态
const initAuth = () => {
const initAuth = async () => {
const storedToken = userAuth.getToken()
const storedUser = userAuth.getUserInfo()
if (storedToken && storedUser) {
token.value = storedToken
user.value = storedUser
} else {
try {
await getProfile()
} catch (error) {
// token失效清除本地存储
logout()
console.error('Token is invalid, logging out')
}
}
}

View File

@@ -1,212 +0,0 @@
<template>
<div class="min-h-screen bg-gray-50 dark:bg-gray-900 p-8">
<div class="max-w-4xl mx-auto">
<h1 class="text-3xl font-bold text-gray-900 dark:text-white mb-8">
API 集成测试
</h1>
<!-- 认证状态 -->
<div class="card mb-8">
<div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
<h2 class="text-lg font-medium text-gray-900 dark:text-white">
认证状态
</h2>
</div>
<div class="p-6">
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<p class="text-sm text-gray-500 dark:text-gray-400">登录状态</p>
<p class="text-lg font-medium text-gray-900 dark:text-white">
{{ authStore.isLoggedIn ? '已登录' : '未登录' }}
</p>
</div>
<div>
<p class="text-sm text-gray-500 dark:text-gray-400">用户信息</p>
<p class="text-lg font-medium text-gray-900 dark:text-white">
{{ authStore.user?.username || '无' }}
</p>
</div>
</div>
</div>
</div>
<!-- API 测试 -->
<div class="card mb-8">
<div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
<h2 class="text-lg font-medium text-gray-900 dark:text-white">
API 测试
</h2>
</div>
<div class="p-6 space-y-4">
<!-- 获取路径 -->
<div class="flex items-center space-x-4">
<button
@click="testGetPaths"
:disabled="loading"
class="btn-primary"
>
{{ loading ? '测试中...' : '测试获取路径' }}
</button>
<span class="text-sm text-gray-600 dark:text-gray-400">
结果: {{ pathsResult }}
</span>
</div>
<!-- 用户注册 -->
<div class="flex items-center space-x-4">
<button
@click="testRegister"
:disabled="loading"
class="btn-primary"
>
{{ loading ? '测试中...' : '测试用户注册' }}
</button>
<span class="text-sm text-gray-600 dark:text-gray-400">
结果: {{ registerResult }}
</span>
</div>
<!-- 用户登录 -->
<div class="flex items-center space-x-4">
<button
@click="testLogin"
:disabled="loading"
class="btn-primary"
>
{{ loading ? '测试中...' : '测试用户登录' }}
</button>
<span class="text-sm text-gray-600 dark:text-gray-400">
结果: {{ loginResult }}
</span>
</div>
<!-- 获取用户信息 -->
<div class="flex items-center space-x-4">
<button
@click="testGetProfile"
:disabled="loading"
class="btn-primary"
>
{{ loading ? '测试中...' : '测试获取用户信息' }}
</button>
<span class="text-sm text-gray-600 dark:text-gray-400">
结果: {{ profileResult }}
</span>
</div>
</div>
</div>
<!-- 响应数据 -->
<div class="card">
<div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
<h2 class="text-lg font-medium text-gray-900 dark:text-white">
响应数据
</h2>
</div>
<div class="p-6">
<pre class="bg-gray-100 dark:bg-gray-800 p-4 rounded-lg text-sm overflow-auto max-h-96">{{ responseData }}</pre>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { authAPI, pathAPI } from '@/utils/api'
import { useToast } from 'vue-toastification'
const authStore = useAuthStore()
const toast = useToast()
const loading = ref(false)
const pathsResult = ref('未测试')
const registerResult = ref('未测试')
const loginResult = ref('未测试')
const profileResult = ref('未测试')
const responseData = ref('')
// 测试获取路径
const testGetPaths = async () => {
loading.value = true
pathsResult.value = '测试中...'
try {
const response = await pathAPI.getPaths()
pathsResult.value = '成功'
responseData.value = JSON.stringify(response, null, 2)
toast.success('获取路径成功')
} catch (error: any) {
pathsResult.value = '失败'
responseData.value = JSON.stringify(error.response?.data || error.message, null, 2)
toast.error('获取路径失败')
} finally {
loading.value = false
}
}
// 测试用户注册
const testRegister = async () => {
loading.value = true
registerResult.value = '测试中...'
try {
const response = await authAPI.register({
username: 'testuser',
password: 'password123',
confirmPassword: 'password123'
})
registerResult.value = '成功'
responseData.value = JSON.stringify(response, null, 2)
toast.success('注册测试成功')
} catch (error: any) {
registerResult.value = '失败'
responseData.value = JSON.stringify(error.response?.data || error.message, null, 2)
toast.error('注册测试失败')
} finally {
loading.value = false
}
}
// 测试用户登录
const testLogin = async () => {
loading.value = true
loginResult.value = '测试中...'
try {
const response = await authAPI.login({
username: 'testuser',
password: 'password123'
})
loginResult.value = '成功'
responseData.value = JSON.stringify(response, null, 2)
toast.success('登录测试成功')
} catch (error: any) {
loginResult.value = '失败'
responseData.value = JSON.stringify(error.response?.data || error.message, null, 2)
toast.error('登录测试失败')
} finally {
loading.value = false
}
}
// 测试获取用户信息
const testGetProfile = async () => {
loading.value = true
profileResult.value = '测试中...'
try {
const response = await authAPI.getProfile()
profileResult.value = '成功'
responseData.value = JSON.stringify(response, null, 2)
toast.success('获取用户信息成功')
} catch (error: any) {
profileResult.value = '失败'
responseData.value = JSON.stringify(error.response?.data || error.message, null, 2)
toast.error('获取用户信息失败')
} finally {
loading.value = false
}
}
</script>