fix: avoid blocking route navigation

fix/non-blocking-route-navigation
chenyuan 1 week ago
parent f07d09635f
commit 5c51a585f9

@ -105,7 +105,22 @@ const getCurrentTeachingClassId = async () => {
} }
return classId; 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; } if (isDemo.value) { next(); return; }
const disabledTitles = proxy.$cache.session.getJSON("disabledTitles"); const disabledTitles = proxy.$cache.session.getJSON("disabledTitles");
const routess = Array.isArray(disabledTitles) ? disabledTitles : []; const routess = Array.isArray(disabledTitles) ? disabledTitles : [];
@ -135,21 +150,9 @@ router.beforeEach(async (to, from, next) => {
} }
start(toModule); start(toModule);
if (module.includes(fromModule)) { if (module.includes(fromModule)) {
const currentClassId = await getCurrentTeachingClassId(); // 访
if (!currentClassId) { updateVisitCountInBackground(fromModule);
next(); }
return;
}
// 访
indexApi
.updateVisitCount({
classId: currentClassId,
projectName: fromModule,
schoolId: userStore.userInfo.schoolId,
userId: userStore.userInfo.userId,
})
.then((res) => {});
}
} }
next(); next();
}); });

@ -37,8 +37,25 @@ router.beforeEach((to, from, next) => {
next({ path: "/index" }); next({ path: "/index" });
NProgress.done(); NProgress.done();
} }
} else { } else {
const userStore = useUserStore(); 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 userStore
.ensureSchoolProductConfig() .ensureSchoolProductConfig()
.then((config) => { .then((config) => {
@ -56,12 +73,17 @@ router.beforeEach((to, from, next) => {
NProgress.done(); NProgress.done();
return null; return null;
} }
return usePermissionStore().getRoutersPermission(); return true;
})
.then((allowed) => {
if (allowed === null) return;
continueNavigation();
}) })
.then((accessRoutes) => { .catch(() => {
if (accessRoutes === null) return; ElMessage.error("学校配置加载失败,请稍后重试");
next(); // hack方法 确保addRoutes已完成 next(false);
}); NProgress.done();
});
} }
} else { } else {
// 没有token // 没有token

Loading…
Cancel
Save