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

549 lines
16 KiB
Vue

5 months ago
<template>
<div class="task">
<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-icon>
<Check />
</el-icon>
保存发布配置
</el-button>
</section>
<el-alert v-if="classId && !canOperateSelectedClass" title="只能配置自己创建的教学班,当前班级仅可查看。" type="info" :closable="false" show-icon />
<!-- 树形结构展示 -->
<section v-loading="loading" class="task-tree">
<div v-for="group in groupedTasks" :key="group.projectName" class="tree-project">
<!-- 模块标题行 -->
<div class="project-row">
<div class="project-checkbox" @click.stop>
<input
type="checkbox"
:checked="isProjectFullySelected(group)"
:indeterminate="isProjectPartiallySelected(group)"
:disabled="!canOperateSelectedClass"
@change="toggleProjectSelection(group)"
/>
</div>
<div class="project-header" @click="toggleProject(group.projectName)">
<svg class="collapse-icon" :class="{ collapsed: !isProjectExpanded(group.projectName) }" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="6 9 12 15 18 9" />
</svg>
<span class="project-number">{{ group.projectNumber }}</span>
<span class="project-label">{{ group.projectName }}</span>
<span class="project-count">({{ group.tasks.length }})</span>
</div>
</div>
<!-- 任务列表 -->
<div v-show="isProjectExpanded(group.projectName)" class="task-list">
<div v-for="task in group.tasks" :key="task.taskKey" class="task-row" :class="{ disabled: !canOperateSelectedClass }">
<div class="task-checkbox">
<input
type="checkbox"
:checked="isTaskSelected(task.taskKey)"
:disabled="!canOperateSelectedClass"
@change="toggleTaskSelection(task.taskKey)"
/>
</div>
<span class="task-number">{{ task.taskNumber }}</span>
<span class="task-name">{{ task.taskName }}</span>
<span class="task-hint" v-if="task.taskKey === comprehensiveTaskKey">(默认不发布)</span>
<div class="reference-answer-control" @click.stop>
<span class="reference-answer-label">参考答案</span>
<el-switch
:model-value="isReferenceAnswerEnabled(task)"
:loading="Boolean(referenceAnswerSaving[task.taskKey])"
:disabled="!canOperateSelectedClass || !task.referenceAnswerUrl || Boolean(referenceAnswerSaving[task.taskKey])"
active-text="开"
inactive-text="关"
inline-prompt
@change="toggleReferenceAnswer(task, $event)"
/>
</div>
</div>
</div>
</div>
<el-empty v-if="!groupedTasks.length && !loading" class="task-empty" description="暂无实训任务">
<template #image>
<el-icon class="empty-icon">
<FolderOpened />
</el-icon>
</template>
</el-empty>
</section>
5 months ago
</div>
</template>
<script setup>
import { listClassTrainingTasks } from "@/api/trainingTask";
import * as indexApi from "@/api/teacher";
5 months ago
import useUserStore from "@/store/modules/user";
import { Check, FolderOpened } from "@element-plus/icons-vue";
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);
const comprehensiveTaskKey = "comprehensive-case-training";
const expandedProjects = ref(new Set()); // 追踪展开的项目
const referenceAnswerSaving = reactive({});
let activeLoadGeneration = 0;
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);
// 按项目分组任务
const groupedTasks = computed(() => {
const projectMap = new Map();
tasks.value.forEach((task) => {
const projectName = task.projectName || "其他";
if (!projectMap.has(projectName)) {
projectMap.set(projectName, {
projectName,
tasks: []
});
}
projectMap.get(projectName).tasks.push(task);
});
return Array.from(projectMap.values()).map((group, projectIndex) => ({
...group,
projectNumber: String(projectIndex + 1),
tasks: group.tasks.map((task, taskIndex) => ({
...task,
taskNumber: `${projectIndex + 1}.${taskIndex + 1}`,
})),
}));
});
function defaultPublishedTaskKeys() {
return groupedTasks.value[0]?.tasks.slice(0, 3).map((task) => task.taskKey) || [];
}
// 判断项目是否展开
function isProjectExpanded(projectName) {
return expandedProjects.value.has(projectName);
}
// 切换项目展开/收起
function toggleProject(projectName) {
if (expandedProjects.value.has(projectName)) {
expandedProjects.value.delete(projectName);
} else {
expandedProjects.value.add(projectName);
}
}
// 判断任务是否被选中
function isTaskSelected(taskKey) {
return selectedTaskKeys.value.includes(taskKey);
}
// 判断项目是否完全选中
function isProjectFullySelected(group) {
return group.tasks.every(task => isTaskSelected(task.taskKey));
}
// 判断项目是否部分选中
function isProjectPartiallySelected(group) {
const selectedCount = group.tasks.filter(task => isTaskSelected(task.taskKey)).length;
return selectedCount > 0 && selectedCount < group.tasks.length;
}
// 切换项目选中状态(全选/全不选)
function toggleProjectSelection(group) {
if (!canOperateSelectedClass.value) return;
const isFullySelected = isProjectFullySelected(group);
group.tasks.forEach(task => {
const index = selectedTaskKeys.value.indexOf(task.taskKey);
if (isFullySelected) {
// 如果已全选,则全不选
if (index > -1) {
selectedTaskKeys.value.splice(index, 1);
}
} else {
// 如果未全选,则全选
if (index === -1) {
selectedTaskKeys.value.push(task.taskKey);
}
}
});
}
// 切换任务选中状态
function toggleTaskSelection(taskKey) {
if (!canOperateSelectedClass.value) return;
const index = selectedTaskKeys.value.indexOf(taskKey);
if (index > -1) {
selectedTaskKeys.value.splice(index, 1);
} else {
selectedTaskKeys.value.push(taskKey);
}
}
function isReferenceAnswerEnabled(task) {
return task?.referenceAnswerEnabled === true;
}
async function toggleReferenceAnswer(task, enabled) {
if (!canOperateSelectedClass.value || !classId.value || !task?.taskKey) return;
const previous = task.referenceAnswerEnabled;
task.referenceAnswerEnabled = enabled === true;
referenceAnswerSaving[task.taskKey] = true;
try {
await indexApi.updateTaskReferenceAnswerVisibility({
teachingClassId: classId.value,
taskKey: task.taskKey,
enabled: task.referenceAnswerEnabled,
});
proxy.$modal.msgSuccess(task.referenceAnswerEnabled ? "参考答案已开放" : "参考答案已关闭");
} catch (error) {
task.referenceAnswerEnabled = previous;
} finally {
delete referenceAnswerSaving[task.taskKey];
}
}
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 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 || []);
// 初始化展开状态:默认展开所有模块
const projectNames = [...new Set(tasks.value.map(t => t.projectName || "其他"))];
expandedProjects.value = new Set(projectNames);
return true;
}
5 months ago
async function loadPublication() {
const requestGeneration = ++activeLoadGeneration;
selectedTaskKeys.value = [];
configured.value = false;
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 || [])
: defaultPublishedTaskKeys();
5 months ago
} finally {
if (requestGeneration === activeLoadGeneration) loading.value = false;
5 months ago
}
}
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 {
/* ===== 筛选 / 操作条(扁平,直接落在画布上;外观归 teacher-light.scss 全局接管)===== */
.task-toolbar {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: var(--sp-3);
margin-bottom: var(--sp-4);
.class-picker {
display: flex;
align-items: center;
gap: var(--sp-2);
color: var(--text-strong);
font-weight: 600;
}
.class-select {
width: var(--field-w);
}
.task-tip {
flex: 1;
min-width: 280px;
color: var(--text-body);
font-size: var(--fs-body);
}
}
/* ===== 树形结构 ===== */
.task-tree {
background: #fff;
border-radius: 8px;
border: 1px solid #e5e7eb;
padding: 16px;
min-height: 300px;
}
.tree-project {
margin-bottom: 12px;
&:last-child {
margin-bottom: 0;
}
}
.project-row {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 12px;
border-radius: 6px;
transition: background-color 0.2s;
&:hover {
background: #f9fafb;
}
}
.project-checkbox {
flex-shrink: 0;
display: flex;
align-items: center;
input[type="checkbox"] {
width: 16px;
height: 16px;
cursor: pointer;
accent-color: #3b82f6;
&:disabled {
cursor: not-allowed;
}
&:indeterminate {
accent-color: #60a5fa;
}
}
}
.project-header {
flex: 1;
display: flex;
align-items: center;
gap: 8px;
cursor: pointer;
user-select: none;
}
.collapse-icon {
flex-shrink: 0;
color: #6b7280;
transition: transform 0.2s;
&.collapsed {
transform: rotate(-90deg);
}
}
.project-label {
font-size: 15px;
font-weight: 600;
color: #1f2937;
flex: 1;
}
.project-number,
.task-number {
flex-shrink: 0;
color: var(--edu-primary);
font-variant-numeric: tabular-nums;
font-weight: 700;
}
.project-number {
min-width: 24px;
font-size: 15px;
}
.project-count {
font-size: 13px;
color: #9ca3af;
font-weight: normal;
}
.task-list {
margin-left: 24px;
margin-top: 4px;
}
.task-row {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border-radius: 4px;
transition: background-color 0.2s;
&:hover:not(.disabled) {
background: #f3f4f6;
}
&.disabled {
opacity: 0.6;
cursor: not-allowed;
}
}
.task-checkbox {
flex-shrink: 0;
display: flex;
align-items: center;
input[type="checkbox"] {
width: 16px;
height: 16px;
cursor: pointer;
accent-color: #3b82f6;
&:disabled {
cursor: not-allowed;
}
}
}
.task-badge {
flex-shrink: 0;
width: 28px;
height: 28px;
display: inline-flex;
align-items: center;
justify-content: center;
background: var(--edu-primary-soft);
color: var(--edu-primary);
border-radius: var(--r-control);
font-size: var(--fs-body);
font-weight: 600;
}
.task-name {
flex: 1;
font-size: 14px;
color: #374151;
font-weight: 600;
}
.task-number {
min-width: 38px;
font-size: 13px;
}
.reference-answer-control {
display: inline-flex;
align-items: center;
gap: 8px;
flex-shrink: 0;
color: var(--text-body);
font-size: 12px;
}
.task-hint {
font-size: 12px;
color: #9ca3af;
}
/* ===== 自定义空状态 ===== */
.task-empty {
padding: var(--sp-5) 0;
.empty-icon {
font-size: 28px;
color: var(--text-muted);
}
}
}
@media (max-width: 760px) {
.task .task-toolbar .class-picker,
.task .task-toolbar .class-select {
width: 100%;
}
.task .task-toolbar :deep(.el-button) {
width: 100%;
}
}
</style>