feat: 建議課程前需要分組
This commit is contained in:
@@ -10,6 +10,8 @@ import Profile from "@/pages/profile.vue";
|
||||
import Register from "@/pages/register.vue";
|
||||
import SelfAssessment from "@/pages/self-assessment.vue";
|
||||
import { useAppStore } from "@/stores/app";
|
||||
import { useCourseStore } from "@/stores/course";
|
||||
import { getApiErrorStatus } from "@/utils/api-error";
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
@@ -60,7 +62,11 @@ const router = createRouter({
|
||||
path: "/course",
|
||||
name: "course",
|
||||
component: Course,
|
||||
meta: { requiresAuth: true, title: "課程建議|運動玩轉健康力" },
|
||||
meta: {
|
||||
requiresAuth: true,
|
||||
requiresFitnessGroup: true,
|
||||
title: "課程建議|運動玩轉健康力",
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/checkin",
|
||||
@@ -88,6 +94,31 @@ router.beforeEach(async (to) => {
|
||||
query: { redirect: to.fullPath },
|
||||
};
|
||||
}
|
||||
|
||||
if (to.meta.requiresFitnessGroup) {
|
||||
const courseStore = useCourseStore();
|
||||
|
||||
try {
|
||||
await courseStore.ensureGroup();
|
||||
} catch (error) {
|
||||
if (getApiErrorStatus(error) === 401 || !store.isLoggedIn) {
|
||||
return {
|
||||
name: "login",
|
||||
query: { redirect: to.fullPath },
|
||||
};
|
||||
}
|
||||
|
||||
if (courseStore.groupMissing) {
|
||||
return {
|
||||
name: "appointment",
|
||||
query: { reason: "missing-fitness-group" },
|
||||
};
|
||||
}
|
||||
|
||||
// 技術性錯誤不應被誤導成尚未分組,返回首頁讓使用者重試。
|
||||
return { name: "home" };
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
router.afterEach((to) => {
|
||||
@@ -99,9 +130,11 @@ router.afterEach((to) => {
|
||||
if (typeof window !== "undefined") {
|
||||
window.addEventListener("auth-expired", () => {
|
||||
const store = useAppStore();
|
||||
const courseStore = useCourseStore();
|
||||
const currentRoute = router.currentRoute.value;
|
||||
const wasLoggedIn = store.isLoggedIn;
|
||||
store.clearAuthentication();
|
||||
courseStore.resetGroup();
|
||||
|
||||
if (wasLoggedIn && currentRoute.name !== "login") {
|
||||
void router.replace({
|
||||
|
||||
Reference in New Issue
Block a user