From 52e0e636e1d01417e765aec116fc0c326c9f0717 Mon Sep 17 00:00:00 2001 From: xuewuerduo <53554701+xuewuerduo@users.noreply.github.com> Date: Thu, 18 Jul 2024 22:34:41 +0800 Subject: [PATCH] =?UTF-8?q?20.=E6=A3=80=E6=B5=8B=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=98=AF=E5=90=A6=E8=BF=87=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.js | 23 ++++++++++++++++-- src/utils/TimeDR.js | 58 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/utils/TimeDR.js diff --git a/src/router/index.js b/src/router/index.js index ac774af..d1edc11 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,5 +1,9 @@ import { createRouter, createWebHistory } from "vue-router" -import {useAdminStore} from "@/stores/admin/admin.js"; +import { useAdminStore } from "@/stores/admin/admin.js"; +import { ElMessage } from "element-plus"; +import TimeDR from "@/utils/TimeDR.js" +import LocalDR from "@/utils/LocalDR.js" + const routes= [ { path: "/", //http://localhost:5173/ @@ -50,10 +54,25 @@ router.beforeEach((to, from, next) => { console.log("未登录") router.push("/login") } + + let startTime = TimeDR.now() + let endTime = adminStore.data.exporeDate + let timeSubResult = TimeDR.timeSub(startTime,endTime) + console.log("timeSubResult:",timeSubResult) + if (timeSubResult.expire){ + ElMessage.error("登录已过期,请重新登录") + + LocalDR.remove("admin") + router.push("/login") + + } + next() + }else{ console.log("不需要身份验证") + next() } - next() + }) diff --git a/src/utils/TimeDR.js b/src/utils/TimeDR.js new file mode 100644 index 0000000..b4bf95a --- /dev/null +++ b/src/utils/TimeDR.js @@ -0,0 +1,58 @@ +/* + 作者: 邓瑞 + 版本: 1.0 + 网站: www.dengruicode.com + 日期: 2024-04-21 +*/ + +const TimeDR = { + now() { + let date = new Date() + + let yyyy = date.getFullYear() + let MM = String(date.getMonth() + 1).padStart(2, '0') + let dd = String(date.getDate()).padStart(2, '0') + let HH = String(date.getHours()).padStart(2, '0') + let mm = String(date.getMinutes()).padStart(2, '0') + let ss = String(date.getSeconds()).padStart(2, '0') + + return `${yyyy}-${MM}-${dd} ${HH}:${mm}:${ss}` + }, + + // 时间相减 + // let startTime = "2024-04-20 11:30:00" + // let endTime = "2024-04-22 12:00:00" + // let timeSubResult = TimeDR.timeSub(startTime,endTime) + timeSub(startTime, endTime) { + let startDate = new Date(startTime) + let endDate = new Date(endTime) + + let expire = false + let expireText = '距离过期还差' + let duration = endDate - startDate + if (duration < 0) { + expire = true + expireText = '已过期' + duration = -duration // 取负数时间差的绝对值 + } + + // 天数、小时数、分钟数、秒数 + let day = Math.floor(duration / (24 * 60 * 60 * 1000)) + let hour = Math.floor((duration / (60 * 60 * 1000)) % 24) + let minute = Math.floor((duration / (60 * 1000)) % 60) + let second = Math.floor(duration / 1000) % 60 + + return { + expire, + startDate, + endDate, + day, + hour, + minute, + second, + remark: `${expireText} ${day} 天 ${hour} 小时 ${minute} 分钟 ${second} 秒`, + } + } +} + +export default TimeDR