175 lines
4.1 KiB
Vue
175 lines
4.1 KiB
Vue
<template>
|
||
<div class="admin-login">
|
||
<div class="login-container">
|
||
<div class="login-header">
|
||
<h1>后台管理系统</h1>
|
||
<p>欢迎回来,请登录您的账户</p>
|
||
</div>
|
||
|
||
<a-card class="login-card">
|
||
<a-form
|
||
ref="formRef"
|
||
:model="formState"
|
||
:rules="formRules"
|
||
layout="vertical"
|
||
@finish="handleLogin"
|
||
>
|
||
<a-form-item label="用户名" name="account">
|
||
<a-input
|
||
v-model:value="formState.account"
|
||
size="large"
|
||
placeholder="请输入用户名"
|
||
:prefix-icon="UserOutlined"
|
||
/>
|
||
</a-form-item>
|
||
|
||
<a-form-item label="密码" name="password">
|
||
<a-input-password
|
||
v-model:value="formState.password"
|
||
size="large"
|
||
placeholder="请输入密码"
|
||
:prefix-icon="LockOutlined"
|
||
@pressEnter="handleLogin"
|
||
/>
|
||
</a-form-item>
|
||
|
||
<a-form-item>
|
||
<a-button
|
||
type="primary"
|
||
size="large"
|
||
html-type="submit"
|
||
:loading="loading"
|
||
block
|
||
>
|
||
登录
|
||
</a-button>
|
||
</a-form-item>
|
||
</a-form>
|
||
|
||
<div class="login-footer">
|
||
<a @click="goToFrontend">返回前台</a>
|
||
</div>
|
||
</a-card>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, reactive } from 'vue'
|
||
import { useRouter } from 'vue-router'
|
||
import { message } from 'ant-design-vue'
|
||
import type { FormInstance } from 'ant-design-vue'
|
||
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
|
||
import { adminAuthApi } from '@/api/admin'
|
||
|
||
const router = useRouter()
|
||
const formRef = ref<FormInstance>()
|
||
const loading = ref(false)
|
||
|
||
const formState = reactive({
|
||
account: '',
|
||
password: ''
|
||
})
|
||
|
||
const formRules = {
|
||
account: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
|
||
password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
|
||
}
|
||
|
||
const handleLogin = async () => {
|
||
try {
|
||
await formRef.value?.validate()
|
||
loading.value = true
|
||
|
||
const res = await adminAuthApi.login({
|
||
account: formState.account,
|
||
password: formState.password
|
||
})
|
||
|
||
const { accessToken, refreshToken, userInfo } = res as any
|
||
|
||
localStorage.setItem('adminAccessToken', accessToken)
|
||
localStorage.setItem('adminRefreshToken', refreshToken)
|
||
localStorage.setItem('adminUserInfo', JSON.stringify(userInfo))
|
||
sessionStorage.setItem('adminAccessToken', accessToken)
|
||
|
||
message.success('登录成功')
|
||
router.push('/admin/dashboard')
|
||
} catch (error: any) {
|
||
console.error('登录失败:', error)
|
||
message.error(error.message || '登录失败,请检查用户名和密码')
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
const goToFrontend = () => {
|
||
localStorage.removeItem('adminAccessToken')
|
||
localStorage.removeItem('adminRefreshToken')
|
||
localStorage.removeItem('adminUserInfo')
|
||
localStorage.removeItem('accessToken')
|
||
localStorage.removeItem('refreshToken')
|
||
localStorage.removeItem('userInfo')
|
||
sessionStorage.removeItem('adminAccessToken')
|
||
sessionStorage.removeItem('accessToken')
|
||
|
||
setTimeout(() => {
|
||
window.location.href = '/auth'
|
||
}, 100)
|
||
}
|
||
</script>
|
||
|
||
<style scoped>
|
||
.admin-login {
|
||
min-height: 100vh;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
padding: 24px;
|
||
}
|
||
|
||
.login-container {
|
||
width: 100%;
|
||
max-width: 420px;
|
||
}
|
||
|
||
.login-header {
|
||
text-align: center;
|
||
margin-bottom: 32px;
|
||
color: white;
|
||
}
|
||
|
||
.login-header h1 {
|
||
font-size: 32px;
|
||
font-weight: 600;
|
||
margin: 0 0 8px 0;
|
||
}
|
||
|
||
.login-header p {
|
||
font-size: 14px;
|
||
opacity: 0.9;
|
||
margin: 0;
|
||
}
|
||
|
||
.login-card {
|
||
border-radius: 12px;
|
||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
||
}
|
||
|
||
.login-footer {
|
||
text-align: center;
|
||
margin-top: 16px;
|
||
}
|
||
|
||
.login-footer a {
|
||
color: #1890ff;
|
||
text-decoration: none;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.login-footer a:hover {
|
||
text-decoration: underline;
|
||
}
|
||
</style>
|