You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dianshang-qianduan/src/views/teacherEnd/task/index.vue

179 lines
6.9 KiB
Vue

5 months ago
<template>
<div class="task app-container">
<section class="task-toolbar">
<div class="class-picker">
<span>教学班</span>
<el-select v-model="classId" placeholder="请选择教学班" class="class-select" @change="loadPublication">
<el-option v-for="item in teachingClassList" :key="item.schoolClassId" :label="item.className" :value="item.schoolClassId" />
</el-select>
</div>
<el-tag v-if="classId" :type="configured ? 'success' : 'warning'">
{{ configured ? '已保存发布配置' : '默认全部发布(未配置)' }}
</el-tag>
<span v-if="classId" class="task-tip">{{ configured ? '仅勾选的实训任务会出现在学生端。' : '现有实训任务默认勾选;“综合案例实训”默认不发布,需由教师主动勾选。' }}</span>
<el-button type="primary" :disabled="!canOperateSelectedClass || !classId" :loading="saving" @click="savePublication">保存发布配置</el-button>
</section>
<el-alert v-if="classId && !canOperateSelectedClass" title="只能配置自己创建的教学班,当前班级仅可查看。" type="info" :closable="false" show-icon />
5 months ago
<el-table
ref="tableRef"
:data="tasks"
row-key="taskKey"
5 months ago
border
v-loading="loading"
class="task-table"
@selection-change="handleSelectionChange"
5 months ago
>
<el-table-column type="selection" width="58" :reserve-selection="true" :selectable="() => canOperateSelectedClass" />
<el-table-column prop="taskName" label="实训任务" min-width="240" align="center" header-align="center" />
<el-table-column prop="projectName" label="所属模块" min-width="180" align="center" header-align="center" />
5 months ago
</el-table>
</div>
</template>
<script setup>
import { listClassTrainingTasks } from "@/api/trainingTask";
import * as indexApi from "@/api/teacher";
5 months ago
import useUserStore from "@/store/modules/user";
const { proxy } = getCurrentInstance();
5 months ago
const userStore = useUserStore();
const classList = ref([]);
const tasks = ref([]);
const classId = ref("");
const selectedTaskKeys = ref([]);
const configured = ref(false);
5 months ago
const loading = ref(false);
const saving = ref(false);
5 months ago
const tableRef = ref(null);
const comprehensiveTaskKey = "comprehensive-case-training";
let activeLoadGeneration = 0;
let selectionSyncGeneration = -1;
const currentUserId = computed(() => userStore.userInfo.userId);
const teachingClassList = computed(() => classList.value.filter((item) => item.classType === "TEACHING" || !item.classType && item.createdBy));
const selectedClass = computed(() => teachingClassList.value.find((item) => item.schoolClassId === classId.value));
const canOperateSelectedClass = computed(() => selectedClass.value?.createdBy === currentUserId.value);
async function loadClasses() {
const res = await indexApi.getClassListBySchoolId({ schoolId: userStore.userInfo.schoolId });
classList.value = res.data || [];
classId.value = teachingClassList.value[0]?.schoolClassId || "";
}
5 months ago
function isActiveLoad(requestedClassId, requestGeneration) {
return classId.value === requestedClassId && requestGeneration === activeLoadGeneration;
}
function syncTableSelection(taskKeys, requestGeneration) {
selectionSyncGeneration = requestGeneration;
tableRef.value?.clearSelection();
proxy.$nextTick(() => {
if (requestGeneration !== activeLoadGeneration) return;
const selected = new Set(taskKeys);
tasks.value.forEach((task) => tableRef.value?.toggleRowSelection(task, selected.has(task.taskKey)));
proxy.$nextTick(() => {
if (requestGeneration === activeLoadGeneration) selectionSyncGeneration = -1;
});
});
}
function sortTasksWithComprehensiveLast(taskList) {
return [...taskList].sort((left, right) => {
const leftIsComprehensive = left?.taskKey === comprehensiveTaskKey;
const rightIsComprehensive = right?.taskKey === comprehensiveTaskKey;
if (leftIsComprehensive === rightIsComprehensive) return 0;
return leftIsComprehensive ? 1 : -1;
});
}
async function loadTasks(requestGeneration) {
if (!classId.value) {
tasks.value = [];
return false;
}
const requestedClassId = classId.value;
tasks.value = [];
const res = await listClassTrainingTasks(classId.value, { enabledOnly: false });
if (!isActiveLoad(requestedClassId, requestGeneration)) return false;
tasks.value = sortTasksWithComprehensiveLast(res.data || []);
return true;
}
5 months ago
function applySelection(requestGeneration) {
syncTableSelection(selectedTaskKeys.value, requestGeneration);
}
async function loadPublication() {
const requestGeneration = ++activeLoadGeneration;
selectedTaskKeys.value = [];
configured.value = false;
syncTableSelection([], requestGeneration);
if (!classId.value) {
tasks.value = [];
return;
}
const requestedClassId = classId.value;
loading.value = true;
5 months ago
try {
await loadTasks(requestGeneration);
if (!isActiveLoad(requestedClassId, requestGeneration)) return;
const res = await indexApi.getTaskPublication({ classId: requestedClassId });
if (!isActiveLoad(requestedClassId, requestGeneration)) return;
configured.value = res.data?.configured === true;
selectedTaskKeys.value = configured.value
? (res.data?.taskKeys || [])
: tasks.value
.filter((task) => task.taskKey !== comprehensiveTaskKey)
.map((task) => task.taskKey);
applySelection(requestGeneration);
5 months ago
} finally {
if (requestGeneration === activeLoadGeneration) loading.value = false;
5 months ago
}
}
function handleSelectionChange(selection) {
if (selectionSyncGeneration === activeLoadGeneration) return;
selectedTaskKeys.value = selection.map((task) => task.taskKey);
}
async function savePublication() {
if (!canOperateSelectedClass.value || !classId.value) return;
saving.value = true;
try {
const taskAllocationList = selectedTaskKeys.value.map((taskKey, index) => ({ module: taskKey, disabledStatus: 0, sort: index + 1 }));
await indexApi.addTask({
taskAllocationList,
classId: classId.value,
schoolId: userStore.userInfo.schoolId,
userId: currentUserId.value,
});
configured.value = true;
proxy.$modal.msgSuccess("发布配置已保存");
} finally {
saving.value = false;
}
}
onMounted(async () => {
loading.value = true;
try {
await loadClasses();
await loadPublication();
} finally {
loading.value = false;
}
});
</script>
<style lang="scss" scoped>
.task { min-height: calc(100vh - 70px); }
.task-toolbar { display:flex; align-items:center; flex-wrap:wrap; gap:12px; margin-bottom:16px; padding:16px 18px; border:1px solid rgba(31,185,244,.32); border-radius:10px; background:rgba(3,31,48,.7); }
.class-picker { display:flex; align-items:center; gap:10px; color:#d7f7ff; font-weight:700; }
.class-select { width:260px; }
.task-tip { flex:1; min-width:280px; color:#8cc9df; font-size:13px; }
.task-table { width:100%; }
@media (max-width: 760px) { .class-picker,.class-select { width:100%; } .task-toolbar :deep(.el-button) { width:100%; } }
</style>