peng
2025-11-07 f64693c0da5483d8670220bf3a5bf89a32e94a20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<template>
  <div class="news-detail">
    <el-card v-loading="loading">
      <template #header>
        <div class="news-header">
          <h1 class="news-title">{{ news.title }}</h1>
          <div class="news-meta">
            <span class="author">作者:{{ news.author }}</span>
            <span class="time">发布时间:{{ formatDate(news.createTime) }}</span>
            <span class="views">浏览量:{{ news.viewCount }}</span>
          </div>
        </div>
      </template>
      
      <!-- 封面图片 -->
      <div v-if="news.coverImage" class="cover-image-container">
        <el-image
          :src="news.coverImage"
          class="cover-image"
          fit="cover"
          :preview-src-list="[news.coverImage]"
          preview-teleported
        />
      </div>
      
      <div class="news-content" v-html="news.content"></div>
      
      <div class="news-footer">
        <el-button @click="goBack">返回列表</el-button>
      </div>
    </el-card>
  </div>
</template>
 
<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getPublishedNews } from '@/api/news'
 
const route = useRoute()
const router = useRouter()
 
const loading = ref(false)
const news = ref({
  id: null,
  title: '',
  content: '',
  summary: '',
  coverImage: '',
  author: '',
  viewCount: 0,
  state: 1,
  createTime: '',
  updateTime: ''
})
 
// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}
 
// 加载新闻数据
const loadNews = async () => {
  try {
    loading.value = true
    const data = await getPublishedNews(route.params.id)
    
    if (data) {
      news.value = data
    } else {
      ElMessage.error('新闻不存在或已下架')
      router.push('/news/list')
    }
  } catch (error) {
    console.error('加载新闻失败:', error)
    ElMessage.error('加载新闻失败')
    router.push('/news/list')
  } finally {
    loading.value = false
  }
}
 
// 返回
const goBack = () => {
  router.go(-1)
}
 
onMounted(() => {
  loadNews()
})
</script>
 
<style scoped>
.news-detail {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}
 
.news-header {
  text-align: center;
}
 
.news-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
}
 
.news-meta {
  display: flex;
  justify-content: center;
  gap: 20px;
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
}
 
/* 封面图片样式 */
.cover-image-container {
  text-align: center;
  margin-bottom: 20px;
}
 
.cover-image {
  max-width: 100%;
  max-height: 400px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
 
.news-content {
  line-height: 1.8;
  font-size: 16px;
  color: #333;
}
 
.news-content :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 10px auto;
}
 
.news-content :deep(p) {
  margin: 10px 0;
}
 
.news-footer {
  margin-top: 30px;
  text-align: center;
}
 
@media (max-width: 768px) {
  .news-detail {
    padding: 10px;
  }
  
  .news-meta {
    flex-direction: column;
    gap: 5px;
  }
  
  .news-title {
    font-size: 20px;
  }
  
  .cover-image {
    max-height: 200px;
  }
}
</style>