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

190 lines
5.8 KiB
Vue

5 months ago
<template>
<div class="task app-container">
<div class="task-toolbar">
<div class="online">
<span>教学班:</span>
<el-select placeholder="请选择教学班" v-model="classId" clearable class="ml10 class-select" @change="getTaskList">
<el-option v-for="school in teachingClassList" :key="school.schoolClassId" :label="school.className" :value="school.schoolClassId" />
</el-select>
</div>
<el-tag v-if="selectedClass" :type="canOperateSelectedClass ? 'success' : 'info'">
{{ canOperateSelectedClass ? "可分配" : "仅查看" }}
</el-tag>
5 months ago
</div>
<el-table
:data="tableData"
border
v-loading="loading"
class="task-table"
5 months ago
height="600"
ref="tableRef"
:header-cell-style="{
color: '#fff',
fontSize: '14px',
backgroundColor: '#6A7DE9!important',
}"
row-key="id"
@select="handleSelect"
@select-all="handleSelectAll"
>
<el-table-column label="勾选" type="selection" width="100" align="center" :reserve-selection="true" :selectable="canSelectTask"></el-table-column>
5 months ago
<el-table-column label="实训任务" prop="module" align="center"></el-table-column>
</el-table>
</div>
</template>
<script setup>
const { proxy } = getCurrentInstance();
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
import * as indexApi from "@/api/teacher";
import { onMounted } from "vue";
const classList = ref([]);
const tableData = ref([]);
const loading = ref(false);
const tableRef = ref(null);
const classId = ref("");
const currentUserId = computed(() => userStore.userInfo.userId);
const selectedClass = computed(() => classList.value.find((item) => item.schoolClassId === classId.value));
const teachingClassList = computed(() => classList.value.filter((item) => normalizeClassType(item) === "TEACHING"));
const canOperateSelectedClass = computed(() => selectedClass.value?.createdBy === currentUserId.value);
const normalizeClassType = (schoolClass) => {
if (schoolClass?.classType) return schoolClass.classType;
return schoolClass?.createdBy ? "TEACHING" : "ADMIN";
};
5 months ago
// 获取班级列表
const getClassList = () => {
indexApi.getClassListBySchoolId({ schoolId: userStore.userInfo.schoolId }).then((res) => {
classList.value = res.data || [];
classId.value = teachingClassList.value[0]?.schoolClassId || "";
5 months ago
getTaskList();
});
};
// 获取任务列表
const getTaskList = () => {
tableRef.value?.clearSelection?.();
if (!classId.value) {
tableData.value = [];
return;
}
5 months ago
loading.value = true;
indexApi
.getTaskList({ classId: classId.value })
.then((res) => {
tableData.value = res.data || [];
setInitialSelection();
})
.finally(() => {
loading.value = false;
});
5 months ago
};
// 其他引入和定义保持不变
const isInitializing = ref(true); // 添加初始化标志
// 修改初始化方法
const setInitialSelection = () => {
isInitializing.value = true;
proxy.$nextTick(() => {
tableData.value.forEach((row) => {
if (row.disabledStatus == 0) {
tableRef.value.toggleRowSelection(row, true);
}
});
isInitializing.value = false; // 初始化完成
});
};
// 修改单选处理方法
const handleSelect = async (selection, row) => {
if (isInitializing.value) return; // 初始化时不处理
if (!canOperateSelectedClass.value) return;
5 months ago
row.disabledStatus = row.disabledStatus == 0 ? 1 : 0;
handleTaskDistribution(tableData.value);
};
// 修改全选处理方法
const handleSelectAll = async (selection) => {
if (isInitializing.value) return; // 初始化时不处理
if (!canOperateSelectedClass.value) return;
5 months ago
const newStatus = selection.length > 0 ? 0 : 1; // 全选为0,取消为1
try {
loading.value = true;
// 更新本地状态
tableData.value.forEach((item) => {
item.disabledStatus = newStatus;
});
handleTaskDistribution(tableData.value);
selection.length = 0;
} catch (error) {
console.error("批量更新失败:", error);
// 恢复选中状态
proxy.$nextTick(() => {
if (newStatus === 0) {
tableRef.value.clearSelection();
} else {
tableRef.value.toggleAllSelection();
}
});
} finally {
loading.value = false;
}
// let { missingInArr2, missingInArr1 } = findMissingItems(selection, tableData.value);
// missingInArr1.forEach((item) => {
// item.disabledStatus = 1;
// });
// selection.forEach((item) => {
// item.disabledStatus = 0;
// });
// console.log(missingInArr1, selection, selection.length > 0 ? "selection" : "missingInArr1");
// handleTaskDistribution(selection.length > 0 ? selection : missingInArr1);
};
function findMissingItems(arr1, arr2) {
// 找出arr1中有但arr2中没有的元素
const missingInArr2 = arr1.filter((item) => !arr2.includes(item));
// 找出arr2中有但arr1中没有的元素
const missingInArr1 = arr2.filter((item) => !arr1.includes(item));
return {
missingInArr2,
missingInArr1,
};
}
// 修改任务分配
const handleTaskDistribution = (selection) => {
if (!canOperateSelectedClass.value) {
return;
}
5 months ago
const uniqueUsers = [...new Map(selection.map((item) => [item.module, item])).values()];
indexApi.addTask({ taskAllocationList: uniqueUsers, classId: classId.value, schoolId: userStore.userInfo.schoolId, userId: currentUserId.value }).then((res) => {
5 months ago
getTaskList();
proxy.$modal.msgSuccess("分配成功");
});
};
const canSelectTask = () => canOperateSelectedClass.value;
5 months ago
// 恢复选中状态
onMounted(() => {
getClassList();
});
</script>
<style lang='scss' scoped>
.task {
height: calc(100vh - 70px);
background: #ffffff;
.task-toolbar {
display: flex;
align-items: center;
gap: 14px;
}
.class-select {
width: 240px;
}
.task-table {
width: 80%;
margin-top: 20px;
}
5 months ago
}
</style>