<template>
|
<div class="news-page">
|
<div class="page-card">
|
<!-- 页面头部 -->
|
<div class="page-header">
|
<div class="title-section">
|
<h1 class="page-title">新闻资讯</h1>
|
<p class="page-subtitle">管理系统新闻资讯内容</p>
|
</div>
|
</div>
|
|
<!-- 搜索工具栏 -->
|
<div class="search-toolbar">
|
<el-input
|
v-model="searchForm.title"
|
placeholder="请输入新闻标题"
|
style="width: 200px"
|
clearable
|
@keyup.enter="handleSearch"
|
@clear="handleClear"
|
/>
|
<el-select
|
v-model="searchForm.state"
|
placeholder="新闻状态"
|
style="width: 160px"
|
clearable
|
@change="handleStateChange"
|
>
|
<el-option
|
v-for="option in stateOptions"
|
:key="option.value"
|
:label="option.label"
|
:value="option.value"
|
/>
|
</el-select>
|
<el-button type="primary" @click="handleSearch">
|
<el-icon><Search /></el-icon>
|
查询
|
</el-button>
|
<el-button type="primary" @click="handleAdd">
|
<el-icon><Plus /></el-icon>
|
新增新闻
|
</el-button>
|
</div>
|
|
<!-- 新闻列表 -->
|
<el-table :data="tableData" style="width: 100%" v-loading="loading">
|
<el-table-column prop="title" label="新闻标题" min-width="200" />
|
<el-table-column label="封面图片" width="120" align="center">
|
<template #default="{ row }">
|
<el-image
|
v-if="row.coverImage"
|
:src="row.coverImage"
|
class="cover-image"
|
fit="cover"
|
:preview-src-list="[row.coverImage]"
|
preview-teleported
|
/>
|
<span v-else>无图片</span>
|
</template>
|
</el-table-column>
|
<el-table-column prop="author" label="作者" width="120" />
|
<el-table-column prop="viewCount" label="浏览量" width="100" align="center" />
|
<el-table-column prop="createTime" label="创建时间" width="180" />
|
<el-table-column prop="stateName" label="状态" width="100" align="center">
|
<template #default="{ row }">
|
<el-tag :type="getStatusType(row.stateName)">{{ row.stateName }}</el-tag>
|
</template>
|
</el-table-column>
|
<el-table-column label="操作" width="120" fixed="right" align="center">
|
<template #default="{ row }">
|
<div class="table-actions">
|
<el-button
|
text
|
:icon="Edit"
|
size="small"
|
@click="handleEdit(row)"
|
class="action-btn edit-btn"
|
title="编辑"
|
/>
|
<el-button
|
text
|
:icon="Delete"
|
size="small"
|
@click="handleDelete(row)"
|
class="action-btn delete-btn"
|
title="删除"
|
/>
|
</div>
|
</template>
|
</el-table-column>
|
</el-table>
|
|
<!-- 分页 -->
|
<div class="pagination">
|
<el-pagination
|
v-model:current-page="pagination.page"
|
v-model:page-size="pagination.size"
|
:page-sizes="[10, 20, 50, 100]"
|
:total="pagination.total"
|
layout="total, sizes, prev, pager, next, jumper"
|
@size-change="handleSizeChange"
|
@current-change="handleCurrentChange"
|
/>
|
</div>
|
</div>
|
</div>
|
</template>
|
|
<script setup lang="ts">
|
import { reactive, ref, onMounted, onActivated } from 'vue'
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
import { useRouter } from 'vue-router'
|
import { Search, Plus, Edit, Delete } from '@element-plus/icons-vue'
|
import { getNewsList, updateNewsState } from '@/api/news'
|
|
const loading = ref(false)
|
const router = useRouter()
|
|
// 搜索表单
|
const searchForm = reactive({
|
title: '',
|
state: ''
|
})
|
|
const stateOptions = [
|
{ label: '草稿', value: 0 },
|
{ label: '发布', value: 1 },
|
{ label: '关闭', value: 2 }
|
]
|
|
// 分页信息
|
const pagination = reactive({
|
page: 1,
|
size: 10,
|
total: 0
|
})
|
|
// 表格数据
|
const tableData = ref([])
|
|
// 获取状态标签类型
|
const getStatusType = (status: string) => {
|
const typeMap: Record<string, string> = {
|
草稿: 'info',
|
发布: 'success',
|
关闭: 'danger'
|
}
|
return typeMap[status] || 'info'
|
}
|
|
// 搜索
|
const handleSearch = () => {
|
pagination.page = 1
|
loadData()
|
}
|
|
const handleStateChange = () => {
|
pagination.page = 1
|
loadData()
|
}
|
|
// 清空搜索
|
const handleClear = () => {
|
searchForm.title = ''
|
loadData()
|
}
|
|
// 新增新闻
|
const handleAdd = () => {
|
router.push('/news/new')
|
}
|
|
// 编辑新闻
|
const handleEdit = (row: any) => {
|
router.push(`/news/edit/${row.id}`)
|
}
|
|
// 删除新闻
|
const handleDelete = async (row: any) => {
|
try {
|
await ElMessageBox.confirm(
|
`确定要删除新闻 “${row.title}” 吗?`,
|
'提示',
|
{
|
confirmButtonText: '确定',
|
cancelButtonText: '取消',
|
type: 'warning'
|
}
|
)
|
|
await updateNewsState(row.id, 2)
|
ElMessage.success('删除成功')
|
loadData()
|
} catch {
|
// 用户取消操作
|
}
|
}
|
|
// 分页大小改变
|
const handleSizeChange = (size: number) => {
|
pagination.size = size
|
loadData()
|
}
|
|
// 当前页改变
|
const handleCurrentChange = (page: number) => {
|
pagination.page = page
|
loadData()
|
}
|
|
// 加载数据
|
const loadData = async () => {
|
loading.value = true
|
try {
|
const keyword = (searchForm.title || '').trim()
|
const data = await getNewsList(
|
pagination.page - 1,
|
pagination.size,
|
keyword,
|
searchForm.state
|
)
|
|
tableData.value = data?.content || []
|
pagination.total = data?.totalElements || 0
|
} catch (e: any) {
|
console.error('加载数据失败:', e)
|
ElMessage.error(e?.message || '加载新闻列表失败')
|
} finally {
|
loading.value = false
|
}
|
}
|
|
onMounted(() => {
|
loadData()
|
})
|
|
// 页面激活时重新加载数据
|
onActivated(() => {
|
loadData()
|
})
|
</script>
|
|
<style scoped>
|
.news-page {
|
padding: 20px;
|
}
|
|
.page-card {
|
background: white;
|
border-radius: 8px;
|
padding: 24px;
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
}
|
|
/* 页面头部样式 */
|
.page-header {
|
display: flex;
|
justify-content: space-between;
|
align-items: flex-start;
|
margin-bottom: 24px;
|
gap: 20px;
|
}
|
|
.title-section {
|
flex: 1;
|
}
|
|
.page-title {
|
margin: 0 0 8px 0;
|
font-size: 24px;
|
font-weight: 600;
|
color: #1a1a1a;
|
line-height: 1.2;
|
}
|
|
.page-subtitle {
|
margin: 0;
|
font-size: 14px;
|
color: #666;
|
line-height: 1.4;
|
}
|
|
/* 搜索工具栏样式 */
|
.search-toolbar {
|
display: flex;
|
gap: 12px;
|
align-items: center;
|
justify-content: flex-end;
|
margin-bottom: 20px;
|
}
|
|
/* 表格操作按钮样式 */
|
.table-actions {
|
display: flex;
|
gap: 12px;
|
justify-content: center;
|
}
|
|
.action-btn {
|
padding: 4px;
|
border: none;
|
background: transparent !important;
|
transition: all 0.2s ease;
|
}
|
|
.edit-btn {
|
color: #409eff;
|
}
|
|
.edit-btn:hover {
|
color: #337ecc;
|
transform: scale(1.2);
|
background: rgba(64, 158, 255, 0.1) !important;
|
}
|
|
.delete-btn {
|
color: #f56c6c;
|
}
|
|
.delete-btn:hover {
|
color: #dd6161;
|
transform: scale(1.2);
|
background: rgba(245, 108, 108, 0.1) !important;
|
}
|
|
.pagination {
|
margin-top: 20px;
|
display: flex;
|
justify-content: flex-end;
|
}
|
|
/* 封面图片样式 */
|
.cover-image {
|
width: 80px;
|
height: 60px;
|
border-radius: 4px;
|
cursor: pointer;
|
}
|
|
/* 响应式适配 */
|
@media (max-width: 768px) {
|
.page-header {
|
flex-direction: column;
|
align-items: stretch;
|
gap: 16px;
|
}
|
|
.search-toolbar {
|
flex-wrap: wrap;
|
gap: 8px;
|
}
|
|
.search-toolbar .el-input {
|
width: 100% !important;
|
max-width: 280px;
|
}
|
}
|
</style>
|