实现访问是是否在访问时间内的判断

This commit is contained in:
Qi 2025-05-14 20:54:01 +08:00
parent 88ace47bc9
commit dee1e0635a
4 changed files with 76 additions and 6 deletions

View File

@ -1,5 +1,11 @@
import { http } from '@/utils/request'; import { http } from '@/utils/request';
// 获取访问时间段
export async function getAccessTime() {
const response = await http.get('/h5/cees/ceesUser/getH5Time');
return response;
}
/** /**
* *
* @returns UseAxiosReturn * @returns UseAxiosReturn

View File

@ -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 routes from './routes';
import {showDialog } from 'Vant';
const router: Router = createRouter({ const router: Router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHashHistory(import.meta.env.BASE_URL),
routes: routes, routes: routes,
}); });
router.beforeEach(async (_to, _from, next) => { //router.beforeEach(async (_to, _from, next) => {
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; export default router;

View File

@ -72,7 +72,7 @@ export const routes = [
{ {
// 找不到路由重定向到404页面 // 找不到路由重定向到404页面
path: '/:pathMatch(.*)', path: '/:pathMatch(.*)',
redirect: '/Home', redirect: '/home',
}, },
]; ];

View File

@ -3,7 +3,8 @@ import { showToast } from 'vant';
// 创建 axios 实例 // 创建 axios 实例
const service: AxiosInstance = axios.create({ 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, // 是否携带跨域凭证 withCredentials: false, // 是否携带跨域凭证
timeout: 10000, // 请求超时时间 timeout: 10000, // 请求超时时间
}); });