Initial commit
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { User } from '@element-plus/icons-vue'
|
||||
import { authApi } from '../../api/auth'
|
||||
|
||||
interface Props {
|
||||
modelValue: boolean
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'update:modelValue', value: boolean): void
|
||||
(e: 'loginSuccess', data: { token: string; user: any }): void
|
||||
(e: 'showRegister'): void
|
||||
}
|
||||
|
||||
const props = defineProps<Props>()
|
||||
const emit = defineEmits<Emits>()
|
||||
|
||||
const authForm = ref({ username: '', password: '' })
|
||||
const authLoading = ref(false)
|
||||
|
||||
const visible = computed({
|
||||
get: () => props.modelValue,
|
||||
set: (value) => emit('update:modelValue', value)
|
||||
})
|
||||
|
||||
async function handleAuth() {
|
||||
if (!authForm.value.username || !authForm.value.password) return
|
||||
|
||||
authLoading.value = true
|
||||
try {
|
||||
const data = await authApi.login(authForm.value)
|
||||
localStorage.setItem('token', data.token)
|
||||
emit('loginSuccess', {
|
||||
token: data.token,
|
||||
user: {
|
||||
username: data.username,
|
||||
permissions: data.permissions
|
||||
}
|
||||
})
|
||||
ElMessage.success('登录成功')
|
||||
resetForm()
|
||||
} catch (err) {
|
||||
ElMessage.error((err as Error).message)
|
||||
} finally {
|
||||
authLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function cancelAuth() {
|
||||
visible.value = false
|
||||
resetForm()
|
||||
}
|
||||
|
||||
function resetForm() {
|
||||
authForm.value = { username: '', password: '' }
|
||||
}
|
||||
|
||||
function showRegister() {
|
||||
emit('showRegister')
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-dialog
|
||||
title="用户登录"
|
||||
v-model="visible"
|
||||
:close-on-click-modal="false"
|
||||
width="400px"
|
||||
center>
|
||||
<div style="text-align: center; padding: 20px 0;">
|
||||
<div style="margin-bottom: 30px; color: #666;">
|
||||
<el-icon size="48" color="#409EFF"><User /></el-icon>
|
||||
<p style="margin-top: 15px; font-size: 16px;">请登录以使用系统功能</p>
|
||||
</div>
|
||||
|
||||
<el-input
|
||||
v-model="authForm.username"
|
||||
placeholder="请输入用户名"
|
||||
prefix-icon="User"
|
||||
size="large"
|
||||
style="margin-bottom: 15px;"
|
||||
:disabled="authLoading"
|
||||
@keyup.enter="handleAuth">
|
||||
</el-input>
|
||||
|
||||
<el-input
|
||||
v-model="authForm.password"
|
||||
placeholder="请输入密码"
|
||||
type="password"
|
||||
size="large"
|
||||
style="margin-bottom: 20px;"
|
||||
:disabled="authLoading"
|
||||
@keyup.enter="handleAuth">
|
||||
</el-input>
|
||||
|
||||
<div>
|
||||
<el-button
|
||||
type="primary"
|
||||
size="large"
|
||||
:loading="authLoading"
|
||||
:disabled="!authForm.username || !authForm.password || authLoading"
|
||||
@click="handleAuth"
|
||||
style="width: 120px; margin-right: 10px;">
|
||||
登录
|
||||
</el-button>
|
||||
<el-button
|
||||
size="large"
|
||||
:disabled="authLoading"
|
||||
@click="cancelAuth"
|
||||
style="width: 120px;">
|
||||
取消
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<div style="margin-top: 20px; text-align: center;">
|
||||
<el-button type="text" @click="showRegister" :disabled="authLoading">
|
||||
还没有账号?点击注册
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</el-dialog>
|
||||
</template>
|
||||
Reference in New Issue
Block a user