feat: 建議課程前需要分組

This commit is contained in:
skytek_xinliang
2026-08-31 10:43:34 +08:00
parent 12519f7809
commit bea7d88f0c
7 changed files with 613 additions and 363 deletions
+34 -1
View File
@@ -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({