-
+
{{ this.test[0].name }}
{{ this.test[0].birth }}
{{ this.test[0].gender }}
diff --git a/src/layouts/DefaultLayout.vue b/src/layouts/DefaultLayout.vue
index 8d675fc..0f8aee5 100644
--- a/src/layouts/DefaultLayout.vue
+++ b/src/layouts/DefaultLayout.vue
@@ -3,6 +3,7 @@ import CustomCard from '@/components/CustomCard.vue'
import { RouterView } from 'vue-router'
import { h, ref } from 'vue'
import { AppstoreOutlined, MailOutlined } from '@ant-design/icons-vue'
+import router from '@/router/index.js'
const current = ref(['mail'])
const items = ref([
@@ -25,6 +26,23 @@ const items = ref([
title: '工具'
}
])
+
+const logout = async () => {
+ const response = await fetch('http://localhost:3000/logout', {
+ method: 'POST',
+ cache: 'default',
+ headers: {
+ 'Content-Type': 'application/json; charset=utf-8'
+ },
+ credentials: 'include'
+ })
+
+ if (response.ok) {
+ await router.push('/login')
+ } else {
+ console.error('Logout failed', response.statusText)
+ }
+}
@@ -52,6 +70,7 @@ const items = ref([
:defaultCollapsed="true"
>
+ 退出登录
footer
diff --git a/src/router/index.js b/src/router/index.js
index 6de34be..fabf854 100644
--- a/src/router/index.js
+++ b/src/router/index.js
@@ -6,6 +6,7 @@ import DefaultLayout from '../layouts/DefaultLayout.vue'
import LoginLayout from '@/layouts/LoginLayout.vue'
import LoginView from '@/views/LoginView.vue'
import HomeView from '@/views/HomeView.vue'
+import { getToken } from '@/utils/auth.js'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
@@ -43,11 +44,25 @@ const router = createRouter({
{
path: '',
name: 'Login',
- component: LoginView
+ component: LoginView,
+ meta: {
+ requiresAuth: true
+ }
}
]
}
]
})
+router.beforeEach((to, from, next) => {
+ const requiresAuth = to.matched.some((record) => record.meta.requiresAuth)
+
+ if (!requiresAuth && !getToken()) {
+ next({ path: '/login' })
+ } else {
+ console.log('next')
+ next()
+ }
+})
+
export default router
diff --git a/src/utils/auth.js b/src/utils/auth.js
index a014808..3425ddf 100644
--- a/src/utils/auth.js
+++ b/src/utils/auth.js
@@ -1,6 +1,6 @@
import Cookies from 'js-cookie'
-const TokenKey = 'Admin-Token'
+const TokenKey = 'account'
export function getToken() {
return Cookies.get(TokenKey)
diff --git a/src/utils/time.js b/src/utils/time.js
index 2617c48..e5206f2 100644
--- a/src/utils/time.js
+++ b/src/utils/time.js
@@ -21,7 +21,7 @@ function dateTimeToTimestamp(pattern, datetime) {
}
Date.prototype.Format = function (fmt) {
- var o = {
+ const o = {
'M+': this.getMonth() + 1, //月份
'd+': this.getDate(), //日
'h+': this.getHours(), //小时
@@ -32,7 +32,7 @@ Date.prototype.Format = function (fmt) {
}
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
- for (var k in o)
+ for (const k in o)
if (new RegExp('(' + k + ')').test(fmt))
fmt = fmt.replace(
RegExp.$1,
diff --git a/src/views/AboutView.vue b/src/views/AboutView.vue
index cc3e38f..6760a59 100644
--- a/src/views/AboutView.vue
+++ b/src/views/AboutView.vue
@@ -5,11 +5,4 @@ import CustomNews from '@/components/CustomNews.vue'
-
+