修改访问时间

This commit is contained in:
Qi 2025-05-17 10:13:32 +08:00
parent fa1e3a68e9
commit 2f7a8874c4
2 changed files with 65 additions and 66 deletions

View File

@ -8,69 +8,9 @@ const router: Router = createRouter({
routes: routes,
});
//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(); // 已经检查过了,继续访问
}
router.beforeEach(async (_to, _from, next) => {
next();
});
export default router;

View File

@ -39,10 +39,12 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { showNotify, showConfirmDialog } from 'vant';
import { getMajor, checkUser } from '@/api';
import { showDialog,showNotify, showConfirmDialog } from 'vant';
import { getAccessTime,getMajor, checkUser } from '@/api';
import { useUserStore } from '@/store';
const store = useUserStore();
const router = useRouter();
const inputCode = ref(''); //
@ -72,7 +74,64 @@
* 2. 根据身份码获取对应的学科信息
* 3. 显示确认对话框用户确认后执行登录操作
*/
function login() {
let accessChecked = false;
async function login() {
loading.value = true;
show.value = true;
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 formatToChineseTime = (date) => {
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
return `${month}${day}${hour}`;
};
const startTimeString = formatToChineseTime(startTime);
const endTimeString = formatToChineseTime(endTime);
const formattedTime = `${startTimeString}-${endTimeString}`;
console.log(formattedTime);
if (currentTime >= startTime && currentTime <= endTime) {
accessChecked = true;
// Continue with login process
await performLogin();
} else {
loading.value = false;
show.value = false;
await showDialog({
title: '访问提示',
message: `请在规定的时间内进行报名。\n报名时间\n${startTimeString} —— ${endTimeString}`,
});
return false;
}
} catch (err) {
console.error('Error getting access time:', err);
loading.value = false;
show.value = false;
await showDialog({
title: '访问错误',
message: '获取访问时间失败,请稍后再试。',
});
return false;
}
} else {
// Already checked, continue with login
await performLogin();
}
}
function performLogin() {
loading.value = true;
show.value = true;