2024-07-04 15:14:57 +00:00
|
|
|
<script setup>
|
2024-07-04 17:52:04 +00:00
|
|
|
import { onMounted, ref } from 'vue';
|
|
|
|
|
import { ElButton, ElForm, ElFormItem, ElInput, ElMessage, ElPagination, ElTable, ElTableColumn } from 'element-plus';
|
2024-07-04 15:14:57 +00:00
|
|
|
import axios from "axios";
|
2024-07-04 17:52:04 +00:00
|
|
|
import { useStore } from "vuex";
|
|
|
|
|
import { useRouter } from "vue-router";
|
2024-07-04 15:14:57 +00:00
|
|
|
import Course from "@views/course-management/Course.vue";
|
2024-07-04 15:06:05 +00:00
|
|
|
|
2024-07-04 15:14:57 +00:00
|
|
|
const router = useRouter();
|
|
|
|
|
const store = useStore();
|
2024-07-05 06:02:48 +00:00
|
|
|
const token = store.getters['authentication/token'];
|
2024-07-04 15:14:57 +00:00
|
|
|
|
|
|
|
|
const searchTitle = ref('');
|
|
|
|
|
const searchAuthor = ref('');
|
|
|
|
|
const searchDescription = ref('');
|
|
|
|
|
const sortOrder = ref('');
|
|
|
|
|
|
|
|
|
|
const allCoursesData = ref([]);
|
|
|
|
|
const coursesData = ref([]);
|
|
|
|
|
|
|
|
|
|
const currentPage = ref(1);
|
|
|
|
|
const pageSize = ref(10);
|
|
|
|
|
const coursesCount = ref(0);
|
|
|
|
|
const firstTimeLoad = ref(true);
|
|
|
|
|
|
|
|
|
|
const selections = ref([]);
|
2024-07-05 06:02:48 +00:00
|
|
|
|
2024-07-04 17:52:04 +00:00
|
|
|
const loadCourses = async (forceReload = false) => {
|
|
|
|
|
if (forceReload) {
|
|
|
|
|
firstTimeLoad.value = true;
|
|
|
|
|
allCoursesData.value = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (firstTimeLoad.value || allCoursesData.value.length < (currentPage.value * pageSize.value) && (currentPage.value * pageSize.value) <= coursesCount) {
|
2024-07-04 15:14:57 +00:00
|
|
|
let params = {
|
|
|
|
|
token: token,
|
|
|
|
|
start: allCoursesData.value.length,
|
|
|
|
|
end: allCoursesData.value.length + pageSize.value * 2
|
2024-07-05 06:02:48 +00:00
|
|
|
};
|
2024-07-04 15:14:57 +00:00
|
|
|
try {
|
2024-07-05 06:02:48 +00:00
|
|
|
const response = await axios.get('/api/courses', { params });
|
|
|
|
|
const data = response.data;
|
2024-07-04 15:14:57 +00:00
|
|
|
coursesCount.value = data.courseCount;
|
|
|
|
|
allCoursesData.value.push(...data.courseList);
|
|
|
|
|
} catch (e) {
|
2024-07-05 06:02:48 +00:00
|
|
|
console.log(e);
|
2024-07-04 15:14:57 +00:00
|
|
|
}
|
|
|
|
|
firstTimeLoad.value = false;
|
|
|
|
|
}
|
2024-07-05 06:02:48 +00:00
|
|
|
coursesData.value = allCoursesData.value.slice((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value);
|
2024-07-04 15:14:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
loadCourses();
|
2024-07-05 06:02:48 +00:00
|
|
|
});
|
2024-07-04 15:06:05 +00:00
|
|
|
|
2024-07-05 06:02:48 +00:00
|
|
|
const handleSearch = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.get('/api/courses/search', {
|
|
|
|
|
params: {
|
|
|
|
|
token: token,
|
|
|
|
|
title: searchTitle.value || '', // 如果为空则传递空字符串
|
|
|
|
|
author: searchAuthor.value || '',
|
|
|
|
|
description: searchDescription.value || '',
|
|
|
|
|
sortOrder: sortOrder.value || '',
|
|
|
|
|
start: 0,
|
|
|
|
|
end: pageSize.value
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const data = response.data;
|
|
|
|
|
coursesCount.value = data.courseCount;
|
|
|
|
|
allCoursesData.value = data.courseList;
|
|
|
|
|
coursesData.value = allCoursesData.value.slice(0, pageSize.value);
|
|
|
|
|
currentPage.value = 1;
|
|
|
|
|
firstTimeLoad.value = false;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
2024-07-04 15:14:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleReset = () => {
|
|
|
|
|
searchTitle.value = '';
|
|
|
|
|
searchAuthor.value = '';
|
|
|
|
|
searchDescription.value = '';
|
|
|
|
|
sortOrder.value = '';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleEditButton = () => {
|
|
|
|
|
selections.value.forEach(selection => {
|
2024-07-05 06:02:48 +00:00
|
|
|
router.push({ name: 'Course', params: { id: selection.id } });
|
|
|
|
|
});
|
2024-07-04 15:14:57 +00:00
|
|
|
};
|
|
|
|
|
|
2024-07-04 17:52:04 +00:00
|
|
|
const handleDeleteButton = async () => {
|
|
|
|
|
if (selections.value.length === 0) {
|
|
|
|
|
ElMessage.warning('请先选择要删除的课程');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const deletePromises = selections.value.map(selection =>
|
|
|
|
|
axios.delete(`/api/courses/${selection.id}`, { params: { token: token } })
|
|
|
|
|
);
|
|
|
|
|
await Promise.all(deletePromises);
|
|
|
|
|
ElMessage.success('删除成功');
|
|
|
|
|
selections.value = []; // 清空选中项
|
|
|
|
|
await loadCourses(true); // 强制重新加载课程数据
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ElMessage.error('删除失败');
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
2024-07-04 15:14:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSelectionChange = (newSelections) => {
|
2024-07-05 06:02:48 +00:00
|
|
|
selections.value = newSelections;
|
|
|
|
|
};
|
2024-07-04 15:14:57 +00:00
|
|
|
|
|
|
|
|
const handleEditInTable = (index) => {
|
2024-07-04 17:52:04 +00:00
|
|
|
router.push({ name: 'Course', query: { mode: 'edit', id: coursesData.value[index].id } });
|
2024-07-04 15:14:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDeleteInTable = async (index) => {
|
|
|
|
|
try {
|
2024-07-05 06:02:48 +00:00
|
|
|
await axios.delete(`/api/courses/${coursesData.value[index].id}`, { params: { token: token } });
|
2024-07-04 15:14:57 +00:00
|
|
|
coursesData.value.splice(index, 1);
|
|
|
|
|
allCoursesData.value.splice(index, 1);
|
|
|
|
|
coursesCount.value--;
|
2024-07-04 17:52:04 +00:00
|
|
|
await loadCourses(true); // 强制重新加载课程数据
|
2024-07-04 15:14:57 +00:00
|
|
|
} catch (e) {
|
2024-07-05 06:02:48 +00:00
|
|
|
ElMessage.error('删除失败');
|
|
|
|
|
console.error(e);
|
2024-07-04 15:14:57 +00:00
|
|
|
}
|
|
|
|
|
};
|
2024-07-04 15:06:05 +00:00
|
|
|
</script>
|
|
|
|
|
|
2024-07-04 15:14:57 +00:00
|
|
|
<template>
|
|
|
|
|
<div class="container">
|
|
|
|
|
<div class="search-container">
|
|
|
|
|
<el-form inline>
|
|
|
|
|
<el-form-item label="课程名称">
|
2024-07-04 17:52:04 +00:00
|
|
|
<el-input v-model="searchTitle" placeholder="请输入课程名称" />
|
2024-07-04 15:14:57 +00:00
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item label="排序">
|
2024-07-04 17:52:04 +00:00
|
|
|
<el-input v-model="sortOrder" placeholder="请输入排序" />
|
2024-07-04 15:14:57 +00:00
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item label="作者">
|
2024-07-04 17:52:04 +00:00
|
|
|
<el-input v-model="searchAuthor" placeholder="请输入作者" />
|
2024-07-04 15:14:57 +00:00
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item label="课程简介">
|
2024-07-04 17:52:04 +00:00
|
|
|
<el-input v-model="searchDescription" placeholder="请输入简介" />
|
2024-07-04 15:14:57 +00:00
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item>
|
|
|
|
|
<el-button type="primary" @click="handleSearch">搜索</el-button>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item>
|
|
|
|
|
<el-button @click="handleReset">重置</el-button>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
</el-form>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="button-container">
|
2024-07-04 17:52:04 +00:00
|
|
|
<el-button type="success" @click="router.push('/course?mode=create')">新增</el-button>
|
2024-07-04 15:14:57 +00:00
|
|
|
<el-button type="warning" @click="handleEditButton">修改</el-button>
|
|
|
|
|
<el-button type="danger" @click="handleDeleteButton">删除</el-button>
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-07-04 17:52:04 +00:00
|
|
|
<el-table :data="coursesData" style="width: 100%;" @selection-change="handleSelectionChange">
|
2024-07-04 15:14:57 +00:00
|
|
|
<el-table-column type="selection" width="55"></el-table-column>
|
2024-07-04 17:52:04 +00:00
|
|
|
<el-table-column prop="orderNo" label="排序" align="center"></el-table-column>
|
2024-07-04 15:14:57 +00:00
|
|
|
<el-table-column prop="title" label="课程名称" align="center"></el-table-column>
|
|
|
|
|
<el-table-column prop="author" label="作者" align="center"></el-table-column>
|
|
|
|
|
<el-table-column prop="description" label="课程简介" align="center"></el-table-column>
|
|
|
|
|
<el-table-column label="操作" align="center">
|
|
|
|
|
<template #default="scope">
|
|
|
|
|
<el-button @click="handleEditInTable(scope.$index)" type="text">修改</el-button>
|
|
|
|
|
<el-button @click="handleDeleteInTable(scope.$index)" type="text">删除</el-button>
|
|
|
|
|
</template>
|
|
|
|
|
</el-table-column>
|
|
|
|
|
</el-table>
|
|
|
|
|
|
|
|
|
|
<div class="pagination-container">
|
|
|
|
|
<el-pagination
|
2024-07-04 17:52:04 +00:00
|
|
|
@size-change="pageSize = $event; loadCourses(true)"
|
|
|
|
|
@current-change="currentPage = $event; loadCourses(true)"
|
2024-07-04 15:14:57 +00:00
|
|
|
:current-page="currentPage"
|
|
|
|
|
:page-size="pageSize"
|
|
|
|
|
layout="total, sizes, prev, pager, next, jumper"
|
|
|
|
|
:total="coursesCount"
|
|
|
|
|
:page-sizes="[10, 20, 50, 100]"
|
|
|
|
|
>
|
|
|
|
|
</el-pagination>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
2024-07-04 15:06:05 +00:00
|
|
|
<style scoped>
|
2024-07-04 15:14:57 +00:00
|
|
|
.container {
|
|
|
|
|
padding: 20px;
|
|
|
|
|
}
|
2024-07-04 15:06:05 +00:00
|
|
|
|
2024-07-04 15:14:57 +00:00
|
|
|
.search-container {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
gap: 5px;
|
|
|
|
|
margin-bottom: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.button-container {
|
|
|
|
|
display: flex;
|
|
|
|
|
gap: 10px;
|
|
|
|
|
margin-bottom: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.el-table {
|
|
|
|
|
margin-bottom: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.pagination-container {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
}
|
2024-07-04 15:06:05 +00:00
|
|
|
</style>
|