简单实现登录退出功能

master
liuyx 2 years ago
parent 788594ce2f
commit 790163fbc0

@ -1,4 +1,5 @@
@import './element.scss'; @import './element.scss';
@import './mine.scss';
* { * {
margin: 0; margin: 0;

@ -0,0 +1,3 @@
.m-content {
padding: 10px;
}

@ -13,10 +13,14 @@
<script setup> <script setup>
import { ref } from 'vue' import { ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const circleUrl = ref('https://liuyxcc.github.io/img/avatar.png') const circleUrl = ref('https://liuyxcc.github.io/img/avatar.png')
const logout = () => { const logout = () => {
localStorage.clear()
router.replace('/login')
console.log('logout') console.log('logout')
} }
</script> </script>

@ -24,6 +24,5 @@ const toggleCollapse = () => {
<style lang="scss" scoped> <style lang="scss" scoped>
.hamburger-container { .hamburger-container {
cursor: pointer; cursor: pointer;
margin-left: 10px;
} }
</style> </style>

@ -19,7 +19,7 @@ import Screenfull from './components/Screenfull.vue'
<style lang="scss" scoped> <style lang="scss" scoped>
.header-container { .header-container {
padding: 0; padding: 0 10px;
width: 100%; width: 100%;
height: 100%; height: 100%;
box-shadow: 0 2px 3px #888888; box-shadow: 0 2px 3px #888888;

@ -6,7 +6,9 @@
<el-container class="main-container" :class="{ hidderContainer: store.collapse }"> <el-container class="main-container" :class="{ hidderContainer: store.collapse }">
<el-header><Header /></el-header> <el-header><Header /></el-header>
<el-main> <el-main>
<router-view></router-view> <transition name="fade" mode="out-in">
<router-view class="m-content"></router-view>
</transition>
</el-main> </el-main>
</el-container> </el-container>
</el-container> </el-container>
@ -44,4 +46,14 @@ const store = useStore()
height: 85px; height: 85px;
padding: 0; padding: 0;
} }
//
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.5s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style> </style>

@ -52,4 +52,14 @@ const router = createRouter({
routes routes
}) })
// 未登录时跳转至登录界面
router.beforeEach((to, from, next) => {
const token = localStorage.getItem('token')
if (token || to.path === '/login') {
next()
} else {
next('/login')
}
})
export default router export default router

@ -2,7 +2,8 @@ import { defineStore } from 'pinia'
export const useStore = defineStore('store', { export const useStore = defineStore('store', {
state: () => ({ state: () => ({
collapse: false collapse: false,
token: localStorage.getItem('token') || ''
}), }),
actions: { actions: {
changeCollapse() { changeCollapse() {

@ -36,12 +36,44 @@
<script setup> <script setup>
import { ref } from 'vue' import { ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const formRef = ref() const formRef = ref()
const loginForm = ref({ const loginForm = ref({
username: '', username: 'admin',
password: '' password: 'admin'
}) })
const rules = ref({
username: [
{
required: true,
message: '请输入用户名!',
trigger: 'blur'
}
],
password: [
{
required: true,
message: '请输入密码!',
trigger: 'blur'
}
]
})
const submitForm = (formRef) => {
formRef.validate((valid) => {
if (valid) {
// token
if (loginForm.value.username === 'admin' && loginForm.value.password === 'admin') {
localStorage.setItem('token', 'i have token!')
router.replace('/')
}
} else {
console.log('error')
}
})
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

Loading…
Cancel
Save