diff --git a/src/permission.js b/src/permission.js index 4010f3b..0805ed6 100644 --- a/src/permission.js +++ b/src/permission.js @@ -16,6 +16,8 @@ import { isRelogin } from "@/utils/request"; import useUserStore from "@/store/modules/user"; import useSettingsStore from "@/store/modules/settings"; import usePermissionStore from "@/store/modules/permission"; +import { loadStudentTrainingTasks } from "@/api/trainingTask"; +import { resolveTrainingTaskKey } from "@/views/training/taskKeyMap"; NProgress.configure({ showSpinner: false }); const whiteList = ["/login", "/register", "/student-demo"]; @@ -51,6 +53,30 @@ router.beforeEach((to, from, next) => { .then(() => next()); // hack方法 确保addRoutes已完成 }; + // 学生实训页面不能只依赖侧边栏隐藏:直接输入地址时也必须按当前教学班的发布配置拦截。 + const userInfo = JSON.parse(getUserInfo() || "{}"); + const taskKey = Number(userInfo.roleId) === 4 + ? resolveTrainingTaskKey(to, to.params?.pageKey) + : ""; + if (taskKey) { + loadStudentTrainingTasks() + .then((response) => { + const tasks = Array.isArray(response?.data) ? response.data : []; + if (tasks.some((task) => task?.taskKey === taskKey)) { + continueNavigation(); + return; + } + ElMessage.warning("该实训任务暂未对当前教学班开放"); + next({ path: "/index" }); + NProgress.done(); + }) + .catch(() => { + // 后端任务详情接口仍会做最终校验,网络异常时保留原有路由错误处理。 + continueNavigation(); + }); + return; + } + // 普通页面无需等待学校配置接口;仅管理页需要用该配置做访问校验。 if (!requiresSchoolProductConfig) { continueNavigation(); diff --git a/src/views/teacherEnd/defaultTask/index.vue b/src/views/teacherEnd/defaultTask/index.vue index d502426..f4c634c 100644 --- a/src/views/teacherEnd/defaultTask/index.vue +++ b/src/views/teacherEnd/defaultTask/index.vue @@ -35,8 +35,8 @@ @@ -70,11 +70,15 @@ import { computed, getCurrentInstance, onMounted, ref } from "vue"; import { Check, FolderOpened, Refresh, RefreshRight } from "@element-plus/icons-vue"; import * as teacherApi from "@/api/teacher"; +import { listTrainingTasks } from "@/api/trainingTask"; import useUserStore from "@/store/modules/user"; const { proxy } = getCurrentInstance(); const userStore = useUserStore(); const tasks = ref([]); +const trainingTasks = ref([]); +const taskNames = computed(() => new Map(trainingTasks.value.map((task) => [task.taskKey, task.taskName]))); +const taskNameFor = (taskKey) => taskNames.value.get(taskKey) || taskKey || "?"; const status = ref("INITIALIZING"); const lastError = ref(""); const loading = ref(false); @@ -92,10 +96,14 @@ const loadTasks = async () => { } loading.value = true; try { - const res = await teacherApi.getSchoolDefaultTasks(); + const [res, catalog] = await Promise.all([ + teacherApi.getSchoolDefaultTasks(), + listTrainingTasks({ enabledOnly: false }), + ]); + trainingTasks.value = catalog.data || []; status.value = res.data?.status || "INITIALIZING"; lastError.value = res.data?.lastError || ""; - tasks.value = res.data?.tasks || []; + tasks.value = (res.data?.tasks || []).filter((task) => task.module !== "__TEACHING_CLASS_TASK_PUBLICATION__"); } finally { loading.value = false; } diff --git a/src/views/training/taskKeyMap.js b/src/views/training/taskKeyMap.js index d3e2c64..9c33d69 100644 --- a/src/views/training/taskKeyMap.js +++ b/src/views/training/taskKeyMap.js @@ -68,7 +68,7 @@ export function getStudentTrainingStepCount(taskKey, fallback = 4) { } export function resolveTrainingTaskKey(route, explicitTaskKey = "") { - return explicitTaskKey || route?.meta?.pageKey || trainingTaskKeyByPath[route?.path] || ""; + return explicitTaskKey || route?.meta?.pageKey || route?.params?.pageKey || trainingTaskKeyByPath[route?.path] || ""; } export function parseTrainingArray(value, fallback = []) { diff --git a/tests/school-default-task-identifiers.test.cjs b/tests/school-default-task-identifiers.test.cjs new file mode 100644 index 0000000..6c1e59e --- /dev/null +++ b/tests/school-default-task-identifiers.test.cjs @@ -0,0 +1,53 @@ +const test = require("node:test"); +const assert = require("node:assert/strict"); +const fs = require("node:fs"); +const path = require("node:path"); +const vm = require("node:vm"); +const { ref, computed } = require("vue"); +const { parse, compileScript, compileTemplate } = require("@vue/compiler-sfc"); + +test("school defaults display task names and submit task keys with the selected state", async () => { + const filename = path.resolve(__dirname, "../src/views/teacherEnd/defaultTask/index.vue"); + const { descriptor, errors } = parse(fs.readFileSync(filename, "utf8")); + assert.deepEqual(errors, []); + compileScript(descriptor, { id: "default-task-test" }); + assert.deepEqual(compileTemplate({ source: descriptor.template.content, filename, id: "default-task-test" }).errors, []); + assert.match(descriptor.template.content, /\{\{ taskNameFor\(row\.module\) \}\}/); + + let saved; + const context = vm.createContext({ + ref, + computed, + onMounted() {}, + getCurrentInstance: () => ({ proxy: { $modal: { msgSuccess() {} } } }), + useUserStore: () => ({ userInfo: { teacherAdmin: true } }), + teacherApi: { + getSchoolDefaultTasks: async () => ({ data: { status: "READY", tasks: [ + { module: "new-product-survey", disabledStatus: 0, sort: 1 }, + { module: "product-development-factors", disabledStatus: 1, sort: 2 }, + { module: "__TEACHING_CLASS_TASK_PUBLICATION__", disabledStatus: 1, sort: 0 }, + ] } }), + saveSchoolDefaultTasks: async (tasks) => { saved = JSON.parse(JSON.stringify(tasks)); }, + }, + listTrainingTasks: async (params) => { + assert.equal(params.enabledOnly, false); + return { data: [ + { taskKey: "new-product-survey", taskName: "新产品调查与分析" }, + { taskKey: "product-development-factors", taskName: "产品开发关键因素" }, + ] }; + }, + }); + vm.runInContext(descriptor.scriptSetup.content.replace(/^import .*;\r?\n/gm, ""), context); + const page = vm.runInContext("({ loadTasks, saveTasks, tasks, taskNameFor })", context); + + await page.loadTasks(); + assert.equal(page.taskNameFor("new-product-survey"), "新产品调查与分析"); + assert.equal(page.tasks.value.length, 2); + page.tasks.value[0].disabledStatus = 1; + await page.saveTasks(); + + assert.deepEqual(saved, [ + { module: "new-product-survey", disabledStatus: 1, sort: 1 }, + { module: "product-development-factors", disabledStatus: 1, sort: 2 }, + ]); +});