2024-07-03 08:48:27 +00:00
|
|
|
<script setup>
|
2024-07-04 04:20:45 +00:00
|
|
|
import {onMounted, ref} from 'vue';
|
2024-07-04 10:02:06 +00:00
|
|
|
import {ElButton, ElForm, ElFormItem, ElInput, ElMessage, ElPagination, ElTable, ElTableColumn} from 'element-plus';
|
2024-07-04 04:20:45 +00:00
|
|
|
import axios from "axios";
|
|
|
|
|
import {useStore} from "vuex";
|
2024-07-04 10:02:06 +00:00
|
|
|
import {useRouter} from "vue-router";
|
|
|
|
|
|
|
|
|
|
const router = useRouter();
|
2024-07-04 04:20:45 +00:00
|
|
|
|
|
|
|
|
const store = useStore();
|
|
|
|
|
const token = store.getters['authentication/token']
|
2024-07-03 08:48:27 +00:00
|
|
|
|
|
|
|
|
const searchTitle = ref('');
|
|
|
|
|
const searchPath = ref('');
|
|
|
|
|
const searchAuthor = ref('');
|
|
|
|
|
const searchSummary = ref('');
|
|
|
|
|
const sortOrder = ref('');
|
|
|
|
|
|
2024-07-04 04:20:45 +00:00
|
|
|
const allNewsData = ref([]);
|
|
|
|
|
const newsData = ref([]);
|
2024-07-03 08:48:27 +00:00
|
|
|
|
|
|
|
|
const currentPage = ref(1);
|
|
|
|
|
const pageSize = ref(10);
|
2024-07-04 04:20:45 +00:00
|
|
|
const newsCount = ref(0);
|
|
|
|
|
const firstTimeLoad = ref(true);
|
2024-07-04 10:02:06 +00:00
|
|
|
|
|
|
|
|
const selections = ref([]);
|
2024-07-04 04:20:45 +00:00
|
|
|
const loadNews = async () => {
|
|
|
|
|
if (firstTimeLoad.value || allNewsData.value.length < (currentPage.value * pageSize) && (currentPage.value * pageSize) <= newsCount) {
|
2024-07-04 10:02:06 +00:00
|
|
|
// console.error(allNewsData.value.length, currentPage.value, pageSize.value, newsCount.value)
|
2024-07-04 04:20:45 +00:00
|
|
|
let params = {
|
|
|
|
|
token: token,
|
|
|
|
|
start: allNewsData.value.length,
|
|
|
|
|
end: allNewsData.value.length + pageSize.value * 2
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.get('/api/news', {params})
|
|
|
|
|
const data = response.data
|
|
|
|
|
newsCount.value = data.newsCount;
|
|
|
|
|
allNewsData.value.push(...data.newsList);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e)
|
|
|
|
|
}
|
|
|
|
|
firstTimeLoad.value = false;
|
|
|
|
|
}
|
|
|
|
|
newsData.value = allNewsData.value.slice((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value)
|
2024-07-04 10:02:06 +00:00
|
|
|
// console.error((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value, newsData.value);
|
2024-07-04 04:20:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
loadNews();
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
2024-07-03 08:48:27 +00:00
|
|
|
|
|
|
|
|
const handleSearch = () => {
|
|
|
|
|
// 搜索逻辑
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleReset = () => {
|
|
|
|
|
searchTitle.value = '';
|
|
|
|
|
searchPath.value = '';
|
|
|
|
|
searchAuthor.value = '';
|
|
|
|
|
searchSummary.value = '';
|
|
|
|
|
sortOrder.value = '';
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-04 10:02:06 +00:00
|
|
|
const handleEditButton = () => {
|
|
|
|
|
//读取每一个selection中的选项id并新建标签页分别跳转打开编辑页面
|
|
|
|
|
selections.value.forEach(selection => {
|
2024-07-04 16:14:33 +00:00
|
|
|
const url = router.resolve({name: 'editNews', query: {mode: 'edit', id: selection.id}}).href;
|
|
|
|
|
window.open(url, '_blank');
|
|
|
|
|
});
|
2024-07-03 08:48:27 +00:00
|
|
|
};
|
|
|
|
|
|
2024-07-04 10:02:06 +00:00
|
|
|
const handleDeleteButton = () => {
|
2024-07-03 08:48:27 +00:00
|
|
|
// 删除逻辑
|
|
|
|
|
};
|
2024-07-04 10:02:06 +00:00
|
|
|
|
|
|
|
|
const handleSelectionChange = (newSelections) => {
|
|
|
|
|
selections.value = newSelections
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleEditInTable = (index) => {
|
|
|
|
|
router.push({name: 'editNews', query: {mode: 'edit', id: newsData.value[index].id}})
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDeleteInTable = async (index) => {
|
|
|
|
|
try {
|
|
|
|
|
await axios.delete(`/api/news/${newsData.value[index].id}`, {params: {token: token,}})
|
|
|
|
|
newsData.value.splice(index, 1);
|
|
|
|
|
allNewsData.value.splice(index, 1);
|
|
|
|
|
newsCount.value--;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ElMessage.error('删除失败')
|
|
|
|
|
console.error(e)
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-07-03 08:48:27 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<div class="container">
|
|
|
|
|
<div class="search-container">
|
|
|
|
|
<el-form inline>
|
|
|
|
|
<el-form-item label="新闻标题">
|
|
|
|
|
<el-input v-model="searchTitle" placeholder="请输入标题"/>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item label="新闻图片路径">
|
|
|
|
|
<el-input v-model="searchPath" placeholder="请输入路径"/>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item label="排序">
|
|
|
|
|
<el-input v-model="sortOrder" placeholder="请输入排序"/>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item label="作者">
|
|
|
|
|
<el-input v-model="searchAuthor" placeholder="请输入作者"/>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item label="新闻简介">
|
|
|
|
|
<el-input v-model="searchSummary" placeholder="请输入简介"/>
|
|
|
|
|
</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 10:02:06 +00:00
|
|
|
<el-button type="success" @click="router.push('/news/edit?mode=create')">新增</el-button>
|
|
|
|
|
<el-button type="warning" @click="handleEditButton">修改</el-button>
|
|
|
|
|
<el-button type="danger" @click="handleDeleteButton">删除</el-button>
|
2024-07-03 08:48:27 +00:00
|
|
|
<el-button type="info">导出</el-button>
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-07-04 10:02:06 +00:00
|
|
|
<el-table :data="newsData" style="width: 100%;"
|
|
|
|
|
@selection-change="handleSelectionChange">
|
2024-07-03 08:48:27 +00:00
|
|
|
<el-table-column type="selection" width="55"></el-table-column>
|
|
|
|
|
<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="summary" label="新闻简介" align="center"></el-table-column>
|
|
|
|
|
<el-table-column label="操作" align="center">
|
|
|
|
|
<template #default="scope">
|
2024-07-04 10:02:06 +00:00
|
|
|
<el-button @click="handleEditInTable(scope.$index)" type="text">修改</el-button>
|
|
|
|
|
<el-button @click="handleDeleteInTable(scope.$index)" type="text">删除</el-button>
|
2024-07-03 08:48:27 +00:00
|
|
|
</template>
|
|
|
|
|
</el-table-column>
|
|
|
|
|
</el-table>
|
|
|
|
|
|
|
|
|
|
<div class="pagination-container">
|
|
|
|
|
<el-pagination
|
2024-07-04 04:20:45 +00:00
|
|
|
@size-change="pageSize = $event;loadNews()"
|
|
|
|
|
@current-change="currentPage = $event;loadNews()"
|
2024-07-03 08:48:27 +00:00
|
|
|
:current-page="currentPage"
|
|
|
|
|
:page-size="pageSize"
|
|
|
|
|
layout="total, sizes, prev, pager, next, jumper"
|
2024-07-04 04:20:45 +00:00
|
|
|
:total="newsCount"
|
2024-07-03 08:48:27 +00:00
|
|
|
:page-sizes="[10, 20, 50, 100]"
|
|
|
|
|
>
|
|
|
|
|
</el-pagination>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.container {
|
|
|
|
|
padding: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.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;
|
|
|
|
|
}
|
|
|
|
|
</style>
|