125 lines
2.8 KiB
Vue
125 lines
2.8 KiB
Vue
<script setup>
|
||
import { ref } from 'vue'
|
||
import { useRouter } from 'vue-router'
|
||
import { useAuthStore } from '@/stores/auth'
|
||
import { login } from "@/api/login";
|
||
|
||
const router = useRouter()
|
||
const authStore = useAuthStore()
|
||
|
||
const username = ref('')
|
||
const password = ref('')
|
||
const loading = ref(false)
|
||
const errorMsg = ref('')
|
||
|
||
const handleLogin = async () => {
|
||
errorMsg.value = ''
|
||
if (!username.value || !password.value) {
|
||
errorMsg.value = '请输入用户名和密码'
|
||
return
|
||
}
|
||
loading.value = true
|
||
try {
|
||
const res = await login(username.value, password.value)
|
||
// 检查响应结构:code === 0 表示成功,数据在 data 中
|
||
if (res && res.code === 0 && res.data) {
|
||
// 保存登录信息(token 和用户信息)
|
||
authStore.setLoginInfo(res.data)
|
||
router.push({ path: '/dashboard' })
|
||
} else {
|
||
errorMsg.value = res.message || '登录失败'
|
||
}
|
||
} catch (err) {
|
||
errorMsg.value = err?.response?.data?.message || err?.message || '登录失败,请重试'
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="login-container">
|
||
<div class="login-form">
|
||
<h2>登录</h2>
|
||
<div class="form-group">
|
||
<input
|
||
v-model="username"
|
||
type="text"
|
||
placeholder="用户名"
|
||
autocomplete="username"
|
||
/>
|
||
</div>
|
||
<div class="form-group">
|
||
<input
|
||
v-model="password"
|
||
type="password"
|
||
placeholder="密码"
|
||
autocomplete="current-password"
|
||
/>
|
||
</div>
|
||
<div v-if="errorMsg" class="error-msg">{{ errorMsg }}</div>
|
||
<button class="login-btn" @click="handleLogin" :disabled="loading">
|
||
{{ loading ? '登录中...' : '登录' }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.login-container {
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
height: 100vh;
|
||
background: #f5f6fa;
|
||
}
|
||
.login-form {
|
||
background: #fff;
|
||
padding: 40px 32px;
|
||
border-radius: 10px;
|
||
box-shadow: 0 2px 16px rgba(0,0,0,0.09);
|
||
min-width: 320px;
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 16px;
|
||
}
|
||
.login-form h2 {
|
||
margin-bottom: 16px;
|
||
text-align: center;
|
||
}
|
||
.form-group {
|
||
margin-bottom: 10px;
|
||
}
|
||
input[type="text"],
|
||
input[type="password"] {
|
||
width: 100%;
|
||
padding: 10px 12px;
|
||
font-size: 16px;
|
||
border: 1px solid #dcdfe6;
|
||
border-radius: 5px;
|
||
box-sizing: border-box;
|
||
margin-bottom: 4px;
|
||
}
|
||
.login-btn {
|
||
width: 100%;
|
||
padding: 10px;
|
||
font-size: 16px;
|
||
background: #005bea;
|
||
color: #fff;
|
||
border: none;
|
||
border-radius: 5px;
|
||
cursor: pointer;
|
||
transition: background 0.2s;
|
||
}
|
||
.login-btn:disabled {
|
||
background: #8bbcfa;
|
||
cursor: not-allowed;
|
||
}
|
||
.error-msg {
|
||
color: #e74c3c;
|
||
text-align: center;
|
||
margin-bottom: 6px;
|
||
font-size: 14px;
|
||
}
|
||
</style>
|