2024-08-22 11:27:06 +08:00
|
|
|
<template>
|
2024-08-23 13:19:20 +08:00
|
|
|
<el-form
|
2024-11-05 11:43:27 +08:00
|
|
|
ref='loginFormRef'
|
|
|
|
|
:model='loginForm'
|
|
|
|
|
:rules='loginRules'
|
|
|
|
|
size='large'
|
2024-08-23 13:19:20 +08:00
|
|
|
>
|
2024-11-05 11:43:27 +08:00
|
|
|
<el-form-item prop='username'>
|
2025-03-07 16:26:14 +08:00
|
|
|
<el-input v-model='loginForm.username' placeholder='用户名' disabled>
|
2024-08-22 11:27:06 +08:00
|
|
|
<template #prefix>
|
2024-11-05 11:43:27 +08:00
|
|
|
<el-icon class='el-input__icon'>
|
2024-08-22 11:27:06 +08:00
|
|
|
<user />
|
|
|
|
|
</el-icon>
|
|
|
|
|
</template>
|
|
|
|
|
</el-input>
|
|
|
|
|
</el-form-item>
|
2024-11-05 11:43:27 +08:00
|
|
|
<el-form-item prop='password'>
|
2024-08-23 13:19:20 +08:00
|
|
|
<el-input
|
2024-11-05 11:43:27 +08:00
|
|
|
v-model='loginForm.password'
|
|
|
|
|
type='password'
|
|
|
|
|
placeholder='密码'
|
|
|
|
|
show-password
|
|
|
|
|
autocomplete='new-password'
|
2025-03-07 16:26:14 +08:00
|
|
|
disabled
|
2024-08-23 13:19:20 +08:00
|
|
|
>
|
2024-08-22 11:27:06 +08:00
|
|
|
<template #prefix>
|
2024-11-05 11:43:27 +08:00
|
|
|
<el-icon class='el-input__icon'>
|
2024-08-22 11:27:06 +08:00
|
|
|
<lock />
|
|
|
|
|
</el-icon>
|
|
|
|
|
</template>
|
|
|
|
|
</el-input>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
</el-form>
|
2024-11-05 11:43:27 +08:00
|
|
|
<div class='login-btn'>
|
2024-08-23 13:19:20 +08:00
|
|
|
<el-button
|
2024-11-05 11:43:27 +08:00
|
|
|
:icon='UserFilled'
|
|
|
|
|
round
|
|
|
|
|
size='large'
|
|
|
|
|
type='primary'
|
|
|
|
|
:loading='loading'
|
|
|
|
|
@click='login(loginFormRef)'
|
2024-08-23 13:19:20 +08:00
|
|
|
>
|
2024-08-22 11:27:06 +08:00
|
|
|
登录
|
|
|
|
|
</el-button>
|
2024-08-23 16:51:49 +08:00
|
|
|
<el-button
|
2024-11-05 11:43:27 +08:00
|
|
|
:icon='CircleClose'
|
|
|
|
|
round
|
|
|
|
|
size='large'
|
|
|
|
|
@click='resetForm(loginFormRef)'
|
2024-08-23 16:51:49 +08:00
|
|
|
>
|
|
|
|
|
重置
|
|
|
|
|
</el-button>
|
2024-08-22 11:27:06 +08:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
2024-11-05 11:43:27 +08:00
|
|
|
<script setup lang='ts'>
|
|
|
|
|
import { useRouter } from 'vue-router'
|
|
|
|
|
import { HOME_URL } from '@/config'
|
|
|
|
|
import { getTimeState } from '@/utils'
|
2024-11-18 09:02:57 +08:00
|
|
|
import { type Dict } from '@/api/interface'
|
|
|
|
|
import { type Login } from '@/api/user/interface/user'
|
2024-11-05 11:43:27 +08:00
|
|
|
import { ElNotification } from 'element-plus'
|
|
|
|
|
import { getDictList, loginApi } from '@/api/user/login'
|
|
|
|
|
import { useUserStore } from '@/stores/modules/user'
|
|
|
|
|
import { useTabsStore } from '@/stores/modules/tabs'
|
|
|
|
|
import { useKeepAliveStore } from '@/stores/modules/keepAlive'
|
|
|
|
|
import { initDynamicRouter } from '@/routers/modules/dynamicRouter'
|
|
|
|
|
import { CircleClose, UserFilled } from '@element-plus/icons-vue'
|
|
|
|
|
import { useAuthStore } from '@/stores/modules/auth'
|
|
|
|
|
import type { ElForm } from 'element-plus'
|
2024-11-20 11:22:05 +08:00
|
|
|
import {useDictStore} from "@/stores/modules/dict";
|
2024-11-05 11:43:27 +08:00
|
|
|
|
|
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
const userStore = useUserStore()
|
|
|
|
|
const tabsStore = useTabsStore()
|
|
|
|
|
const keepAliveStore = useKeepAliveStore()
|
|
|
|
|
|
|
|
|
|
const dictStore = useDictStore()
|
|
|
|
|
|
2024-08-22 11:27:06 +08:00
|
|
|
|
|
|
|
|
type FormInstance = InstanceType<typeof ElForm>;
|
2024-11-05 11:43:27 +08:00
|
|
|
const loginFormRef = ref<FormInstance>()
|
2024-08-22 11:27:06 +08:00
|
|
|
const loginRules = reactive({
|
2024-11-05 11:43:27 +08:00
|
|
|
username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
|
|
|
|
|
password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
|
|
|
|
|
})
|
2024-08-22 11:27:06 +08:00
|
|
|
|
2024-11-05 11:43:27 +08:00
|
|
|
const loading = ref(false)
|
2024-08-22 11:27:06 +08:00
|
|
|
const loginForm = reactive<Login.ReqLoginForm>({
|
2025-03-07 16:26:14 +08:00
|
|
|
username: 'user',
|
|
|
|
|
password: 'user12345.',
|
2024-11-05 11:43:27 +08:00
|
|
|
})
|
2024-08-22 11:27:06 +08:00
|
|
|
|
|
|
|
|
// login
|
|
|
|
|
const login = (formEl: FormInstance | undefined) => {
|
2024-11-05 11:43:27 +08:00
|
|
|
if (!formEl) return
|
2024-08-23 13:19:20 +08:00
|
|
|
formEl.validate(async (valid) => {
|
2024-11-05 11:43:27 +08:00
|
|
|
if (!valid) return
|
|
|
|
|
loading.value = true
|
2024-08-22 11:27:06 +08:00
|
|
|
try {
|
|
|
|
|
// 1.执行登录接口
|
2024-08-23 13:19:20 +08:00
|
|
|
const { data } = await loginApi({
|
|
|
|
|
...loginForm,
|
2024-11-20 11:22:05 +08:00
|
|
|
password: loginForm.password,
|
2024-11-05 11:43:27 +08:00
|
|
|
})
|
2025-02-07 10:39:30 +08:00
|
|
|
userStore.setAccessToken(data.accessToken)
|
|
|
|
|
userStore.setRefreshToken(data.refreshToken)
|
2024-11-20 11:22:05 +08:00
|
|
|
userStore.setUserInfo(data.userInfo)
|
2024-11-05 11:43:27 +08:00
|
|
|
const response = await getDictList()
|
|
|
|
|
const dictData = response.data as unknown as Dict[]
|
|
|
|
|
await dictStore.initDictData(dictData)
|
2024-08-22 11:27:06 +08:00
|
|
|
// 2.添加动态路由
|
2024-11-05 11:43:27 +08:00
|
|
|
await initDynamicRouter()
|
2024-08-22 11:27:06 +08:00
|
|
|
|
|
|
|
|
// 3.清空 tabs、keepAlive 数据
|
2024-11-05 11:43:27 +08:00
|
|
|
tabsStore.setTabs([])
|
|
|
|
|
keepAliveStore.setKeepAliveName([])
|
2024-08-22 11:27:06 +08:00
|
|
|
|
|
|
|
|
// 4.跳转到首页
|
2024-11-05 11:43:27 +08:00
|
|
|
router.push(HOME_URL)
|
2024-08-23 13:19:20 +08:00
|
|
|
// 5.登录默认不显示菜单和导航栏
|
2024-11-05 11:43:27 +08:00
|
|
|
authStore.resetAuthStore()
|
2024-08-22 11:27:06 +08:00
|
|
|
ElNotification({
|
|
|
|
|
title: getTimeState(),
|
2024-11-05 11:43:27 +08:00
|
|
|
message: '登录成功',
|
|
|
|
|
type: 'success',
|
2024-08-23 13:19:20 +08:00
|
|
|
duration: 3000,
|
2024-11-05 11:43:27 +08:00
|
|
|
})
|
2024-08-22 11:27:06 +08:00
|
|
|
} finally {
|
2024-11-05 11:43:27 +08:00
|
|
|
loading.value = false
|
2024-08-22 11:27:06 +08:00
|
|
|
}
|
2024-11-05 11:43:27 +08:00
|
|
|
})
|
|
|
|
|
}
|
2024-08-22 11:27:06 +08:00
|
|
|
|
|
|
|
|
// resetForm
|
|
|
|
|
const resetForm = (formEl: FormInstance | undefined) => {
|
2024-11-05 11:43:27 +08:00
|
|
|
if (!formEl) return
|
|
|
|
|
formEl.resetFields()
|
|
|
|
|
}
|
2024-08-22 11:27:06 +08:00
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
// 监听 enter 事件(调用登录)
|
|
|
|
|
document.onkeydown = (e: KeyboardEvent) => {
|
2024-11-05 11:43:27 +08:00
|
|
|
e = (window.event as KeyboardEvent) || e
|
|
|
|
|
if (e.code === 'Enter' || e.code === 'enter' || e.code === 'NumpadEnter') {
|
|
|
|
|
if (loading.value) return
|
|
|
|
|
login(loginFormRef.value)
|
2024-08-22 11:27:06 +08:00
|
|
|
}
|
2024-11-05 11:43:27 +08:00
|
|
|
}
|
|
|
|
|
})
|
2024-08-22 11:27:06 +08:00
|
|
|
</script>
|
|
|
|
|
|
2024-11-05 11:43:27 +08:00
|
|
|
<style scoped lang='scss'>
|
2024-08-22 11:27:06 +08:00
|
|
|
@import "../index.scss";
|
|
|
|
|
</style>
|