fix: login

This commit is contained in:
Lan
2025-09-04 13:51:48 +08:00
parent c14c4bca62
commit cd6eba0782
2 changed files with 21 additions and 22 deletions
-1
View File
@@ -41,7 +41,6 @@ onMounted(() => {
} }
ConfigService.getUserConfig().then((res: ApiResponse<ConfigState>) => { ConfigService.getUserConfig().then((res: ApiResponse<ConfigState>) => {
if (res.code === 200 && res.detail) { if (res.code === 200 && res.detail) {
console.log(res);
localStorage.setItem('config', JSON.stringify(res.detail)) localStorage.setItem('config', JSON.stringify(res.detail))
if ( if (
res.detail.notify_title && res.detail.notify_title &&
+21 -21
View File
@@ -85,16 +85,10 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, inject } from 'vue' import { ref, inject } from 'vue'
import { BoxIcon } from 'lucide-vue-next' import { BoxIcon } from 'lucide-vue-next'
import api from '@/utils/api'
import type { ApiResponse } from '@/types'
import { useAlertStore } from '@/stores/alertStore' import { useAlertStore } from '@/stores/alertStore'
import { useAdminData } from '@/stores/adminStore' import { useAdminData } from '@/stores/adminStore'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import { AuthService } from '@/services'
// 登录响应类型定义
interface LoginResponse {
token: string
}
const alertStore = useAlertStore() const alertStore = useAlertStore()
const password = ref('') const password = ref('')
const isLoading = ref(false) const isLoading = ref(false)
@@ -116,22 +110,28 @@ const router = useRouter()
const handleSubmit = async () => { const handleSubmit = async () => {
if (!validateForm()) return if (!validateForm()) return
api
.post<ApiResponse<LoginResponse>>('/admin/login', { password: password.value })
.then((res) => {
adminStore.setToken(res.data.detail?.token || '')
router.push('/admin')
})
.catch((error) => {
alertStore.showAlert(error.response?.data?.detail || '登录失败', 'error')
})
isLoading.value = true isLoading.value = true
try { try {
await new Promise((resolve) => setTimeout(resolve, 2000)) const response = await AuthService.login(password.value)
// 处理登录成功 if (response.detail?.token) {
} catch (error) { adminStore.setToken(response.detail.token)
console.log(error) router.push('/admin')
// 处理错误 } else {
alertStore.showAlert('登录失败:未获取到有效令牌', 'error')
}
} catch (error: unknown) {
interface ErrorWithResponse {
response?: {
data?: {
detail?: string
}
}
}
const errorMessage = error && typeof error === 'object' && 'response' in error
? (error as ErrorWithResponse).response?.data?.detail || '登录失败'
: '登录失败'
alertStore.showAlert(errorMessage, 'error')
} finally { } finally {
isLoading.value = false isLoading.value = false
} }