diff --git a/src/layout/components/AppMain.vue b/src/layout/components/AppMain.vue index 378e31d..e16f200 100644 --- a/src/layout/components/AppMain.vue +++ b/src/layout/components/AppMain.vue @@ -105,7 +105,22 @@ const getCurrentTeachingClassId = async () => { } return classId; }; -router.beforeEach(async (to, from, next) => { +const updateVisitCountInBackground = (projectName) => { + getCurrentTeachingClassId() + .then((currentClassId) => { + if (!currentClassId) return; + return indexApi.updateVisitCount({ + classId: currentClassId, + projectName, + schoolId: userStore.userInfo.schoolId, + userId: userStore.userInfo.userId, + }); + }) + .catch((error) => { + console.warn("更新模块访问次数失败", error); + }); +}; +router.beforeEach((to, from, next) => { if (isDemo.value) { next(); return; } const disabledTitles = proxy.$cache.session.getJSON("disabledTitles"); const routess = Array.isArray(disabledTitles) ? disabledTitles : []; @@ -135,21 +150,9 @@ router.beforeEach(async (to, from, next) => { } start(toModule); if (module.includes(fromModule)) { - const currentClassId = await getCurrentTeachingClassId(); - if (!currentClassId) { - next(); - return; - } - // 更新访问次数 - indexApi - .updateVisitCount({ - classId: currentClassId, - projectName: fromModule, - schoolId: userStore.userInfo.schoolId, - userId: userStore.userInfo.userId, - }) - .then((res) => {}); - } + // 访问统计不影响页面跳转,避免等待当前教学班接口。 + updateVisitCountInBackground(fromModule); + } } next(); }); diff --git a/src/permission.js b/src/permission.js index 839c5a7..ab954a9 100644 --- a/src/permission.js +++ b/src/permission.js @@ -37,8 +37,25 @@ router.beforeEach((to, from, next) => { next({ path: "/index" }); NProgress.done(); } - } else { + } else { const userStore = useUserStore(); + const requiresSchoolProductConfig = to.matched.some((record) => + record.meta?.requiresFacultyManagement + || record.meta?.requiresMajorManagement + || record.meta?.requiresAdminClass + ); + const continueNavigation = () => { + usePermissionStore() + .getRoutersPermission() + .then(() => next()); // hack方法 确保addRoutes已完成 + }; + + // 普通页面无需等待学校配置接口;仅管理页需要用该配置做访问校验。 + if (!requiresSchoolProductConfig) { + continueNavigation(); + return; + } + userStore .ensureSchoolProductConfig() .then((config) => { @@ -56,12 +73,17 @@ router.beforeEach((to, from, next) => { NProgress.done(); return null; } - return usePermissionStore().getRoutersPermission(); + return true; + }) + .then((allowed) => { + if (allowed === null) return; + continueNavigation(); }) - .then((accessRoutes) => { - if (accessRoutes === null) return; - next(); // hack方法 确保addRoutes已完成 - }); + .catch(() => { + ElMessage.error("学校配置加载失败,请稍后重试"); + next(false); + NProgress.done(); + }); } } else { // 没有token