From dee1e0635a2191ddf93c25ae99fa7dbbd9e25455 Mon Sep 17 00:00:00 2001 From: Qi <3194726156@qq.com> Date: Wed, 14 May 2025 20:54:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=AE=BF=E9=97=AE=E6=98=AF?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E5=9C=A8=E8=AE=BF=E9=97=AE=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=86=85=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/index.ts | 6 ++++ src/router/index.ts | 71 +++++++++++++++++++++++++++++++++++--- src/router/routes.ts | 2 +- src/utils/request/index.ts | 3 +- 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index d250b10..f015d1f 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,5 +1,11 @@ import { http } from '@/utils/request'; + +// 获取访问时间段 +export async function getAccessTime() { + const response = await http.get('/h5/cees/ceesUser/getH5Time'); + return response; +} /** * 账号密码登录 * @returns UseAxiosReturn diff --git a/src/router/index.ts b/src/router/index.ts index 31814a9..46757b6 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,13 +1,76 @@ -import { createRouter, createWebHistory, Router } from 'vue-router'; +import { createRouter, createWebHashHistory, Router } from 'vue-router'; +import {getAccessTime} from '@/api/index'; import routes from './routes'; +import {showDialog } from 'Vant'; const router: Router = createRouter({ - history: createWebHistory(import.meta.env.BASE_URL), + history: createWebHashHistory(import.meta.env.BASE_URL), routes: routes, }); -router.beforeEach(async (_to, _from, next) => { - next(); +//router.beforeEach(async (_to, _from, next) => { +// next(); +//}); + + +// 防止重复检查访问时间 +let accessChecked = false; + +router.beforeEach(async (to, from, next) => { + if (!accessChecked) { + try { + const res = await getAccessTime(); + const currentTime = new Date(); + const startTime = new Date(res.result.startTime); + const endTime = new Date(res.result.endTime); + + console.log("startTime", startTime); + console.log("endTime", endTime); + // 格式化日期,保留年月日和时分,去掉秒 + const startTimeString = startTime.toLocaleString('zh-CN', { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + }); + const endTimeString = endTime.toLocaleString('zh-CN', { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + }); + + // 判断当前时间是否在允许的访问时间范围内 + if (currentTime >= startTime && currentTime <= endTime) { + accessChecked = true; + next(); // 在允许的访问时间内,继续访问 + } else { + + // 如果不在访问时间内,弹出警告框 + showDialog({ + title: '访问提示', + message: `当前不在访问时间段内,请稍后再试。\n访问时间段:\n${startTimeString} —— ${endTimeString}`, + }).then(() => { + // 用户点击“确定”后的处理,关闭页面 + window.close(); // 关闭当前浏览器窗口 + }); + next(false); // 阻止跳转 + } + } catch (err) { + // 获取访问时间失败,弹出错误提示 + showDialog({ + title: '访问错误', + message: '获取访问时间失败,请稍后再试。', + }).then(() => { + window.close(); // 关闭当前浏览器窗口 + }); + next(false); // 阻止跳转 + } + } else { + next(); // 已经检查过了,继续访问 + } }); export default router; diff --git a/src/router/routes.ts b/src/router/routes.ts index 6750cba..d1b7313 100644 --- a/src/router/routes.ts +++ b/src/router/routes.ts @@ -72,7 +72,7 @@ export const routes = [ { // 找不到路由重定向到404页面 path: '/:pathMatch(.*)', - redirect: '/Home', + redirect: '/home', }, ]; diff --git a/src/utils/request/index.ts b/src/utils/request/index.ts index 50e2db5..79a370c 100644 --- a/src/utils/request/index.ts +++ b/src/utils/request/index.ts @@ -3,7 +3,8 @@ import { showToast } from 'vant'; // 创建 axios 实例 const service: AxiosInstance = axios.create({ - baseURL: 'http://127.0.0.1:10001/jeecg-boot', // 基础 URL + //baseURL: 'http://62.234.217.137:10001/jeecg-boot', // 基础 URL + baseURL: 'http://localhost:10001/jeecg-boot', // 基础 URL withCredentials: false, // 是否携带跨域凭证 timeout: 10000, // 请求超时时间 });