diff --git a/src/router/index.js b/src/router/index.js index db8a3f7..c6f9759 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -43,4 +43,23 @@ const router = new VueRouter({ routes }) +// 路由守卫 +router.beforeEach((to, from, next) => { + // 登录页面不需要验证 + if (to.path === '/login') { + next() + return + } + + // 检查是否已登录 + const userInfo = localStorage.getItem('info') + if (userInfo) { + // 已登录,继续导航 + next() + } else { + // 未登录,重定向到登录页面 + next('/login') + } +}) + export default router \ No newline at end of file diff --git a/src/view/green_record.vue b/src/view/green_record.vue index d76ee21..3dc18ee 100644 --- a/src/view/green_record.vue +++ b/src/view/green_record.vue @@ -43,20 +43,19 @@ export default { getAisle({user_id:JSON.parse(localStorage.getItem('info')).ID}).then(res => { if(res.code === 0){ this.list = res.data.list - console.log(this.list) } }) }, formatDate (dateString){ - if (!dateString) return ''; - const date = new Date(dateString); - const year = date.getFullYear(); - const month = String(date.getMonth() + 1).padStart(2, '0'); - const day = String(date.getDate()).padStart(2, '0'); - const hours = String(date.getHours()).padStart(2, '0'); - const minutes = String(date.getMinutes()).padStart(2, '0'); - const seconds = String(date.getSeconds()).padStart(2, '0'); - return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; + if (!dateString) return '' + const date = new Date(dateString) + const year = date.getFullYear() + const month = String(date.getMonth() + 1).padStart(2, '0') + const day = String(date.getDate()).padStart(2, '0') + const hours = String(date.getHours()).padStart(2, '0') + const minutes = String(date.getMinutes()).padStart(2, '0') + const seconds = String(date.getSeconds()).padStart(2, '0') + return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` } } } diff --git a/src/view/index.vue b/src/view/index.vue index a44c824..baa5a6b 100644 --- a/src/view/index.vue +++ b/src/view/index.vue @@ -49,7 +49,7 @@ export default { type: 'warning' }).then(() => { this.$message.success('退出登录成功'); - localStorage.setItem('info',{}) + localStorage.setItem('info','') this.$router.replace('/login') }).catch(() => { // 取消退出 diff --git a/src/view/login.vue b/src/view/login.vue index 6c77dd0..a87df9e 100644 --- a/src/view/login.vue +++ b/src/view/login.vue @@ -51,8 +51,8 @@ export default { login({...params}).then((res) => { if(res.code === 0){ this.$message.success('登录成功'); - this.$router.replace('/index') localStorage.setItem('info',JSON.stringify(res.data)) + this.$router.replace('/index') }else { this.$message.error(res.msg || '请求失败'); }