仰望星辰工作室

better-staridc-MNBT

Z zfhsh first commit 2 个月前
1<template>
2 <div class="site-news">
3 <section class="site-page-header" :style="{ backgroundImage: `url(${headerImage})` }">
4 <div class="hd-container">
5 <h1>新闻资讯</h1>
6 <p>了解平台最新动态、优惠活动与行业资讯</p>
7 </div>
8 </section>
9
10 <section class="site-sec">
11 <div class="hd-container">
12 <div class="site-news-layout">
13 <main class="site-news-main">
14 <!-- 分类快捷筛选 -->
15 <div class="site-news-tabs">
16 <button class="site-news-tab" :class="{ active: category === '' }" @click="switchCategory('')">全部</button>
17 <button
18 v-for="cat in categories"
19 :key="cat.category"
20 class="site-news-tab"
21 :class="{ active: category === cat.category }"
22 @click="switchCategory(cat.category)"
23 >
24 {{ cat.category }}
25 </button>
26 </div>
27
28 <t-loading :loading="loading" size="large">
29 <div v-if="newsList.length === 0 && !loading" class="site-empty">
30 <i class="mdi mdi-newspaper-variant-outline"></i>
31 <p>暂无新闻</p>
32 </div>
33 <div v-else class="site-news-cards">
34 <div v-for="n in newsList" :key="n.id" class="site-news-card" @click="$router.push('/site/news/' + n.id)">
35 <div class="site-news-body">
36 <h2 class="site-news-title">{{ n.title }}</h2>
37 <p class="site-news-excerpt">{{ excerpt(n.content) }}</p>
38 <div class="site-news-meta">
39 <span class="site-news-chip"><i class="mdi mdi-calendar-month-outline"></i>{{ formatDate(n.created_at) }}</span>
40 <span v-if="n.category" class="site-news-chip"><i class="mdi mdi-tag-outline"></i>{{ n.category }}</span>
41 <span class="site-news-chip"><i class="mdi mdi-eye-outline"></i>{{ n.views }} 浏览</span>
42 </div>
43 <div class="site-news-more">阅读全文 <i class="mdi mdi-arrow-right"></i></div>
44 </div>
45 </div>
46 </div>
47 </t-loading>
48
49 <!-- 分页 -->
50 <div v-if="totalPages > 1" class="site-pagination">
51 <button class="site-page-btn" :disabled="page <= 1" @click="goToPage(page - 1)">
52 <i class="mdi mdi-chevron-left"></i>上一页
53 </button>
54 <div class="site-page-nums">
55 <button
56 v-for="p in visiblePages"
57 :key="p"
58 class="site-page-num"
59 :class="{ active: p === page }"
60 @click="goToPage(p)"
61 >
62 {{ p }}
63 </button>
64 </div>
65 <button class="site-page-btn" :disabled="page >= totalPages" @click="goToPage(page + 1)">
66 下一页<i class="mdi mdi-chevron-right"></i>
67 </button>
68 </div>
69 </main>
70
71 <aside class="site-news-sidebar">
72 <div class="site-widget">
73 <h3><i class="mdi mdi-trending-up"></i>热门文章</h3>
74 <div v-for="(a, i) in popular" :key="a.id" class="site-popular" @click="$router.push('/site/news/' + a.id)">
75 <span class="site-popular-rank" :class="{ top: i < 3 }">{{ i + 1 }}</span>
76 <div class="site-popular-info">
77 <h4 class="site-popular-title">{{ a.title }}</h4>
78 <div class="site-popular-meta">
79 <span>{{ formatDate(a.created_at) }}</span>
80 <span>{{ a.views }} 浏览</span>
81 </div>
82 </div>
83 </div>
84 </div>
85
86 <div class="site-widget">
87 <h3><i class="mdi mdi-information-outline"></i>关于我们</h3>
88 <div class="site-widget-about">
89 <p>MNBT 专注虚拟主机、云服务器与安全防护服务,支付即开、分钟级上线。</p>
90 <router-link to="/about" class="site-widget-link">了解更多 <i class="mdi mdi-arrow-right"></i></router-link>
91 </div>
92 </div>
93 </aside>
94 </div>
95 </div>
96 </section>
97 </div>
98</template>
99
100<script setup>
101import { ref, computed, onMounted } from 'vue'
102import { useRoute, useRouter } from 'vue-router'
103import bg3 from '@/shared/assets/bg3.jpg'
104import { getSiteNews, getSiteNewsPopular } from '@/home/api/site'
105import { formatDate } from '@/home/utils/format'
106
107const headerImage = bg3
108const route = useRoute()
109const router = useRouter()
110
111const perPage = 6
112const page = ref(1)
113const category = ref('')
114const total = ref(0)
115const newsList = ref([])
116const categories = ref([])
117const popular = ref([])
118const loading = ref(true)
119
120const totalPages = computed(() => Math.max(1, Math.ceil(total.value / perPage)))
121
122const visiblePages = computed(() => {
123 const maxVisible = 5
124 const half = Math.floor(maxVisible / 2)
125 let start = Math.max(1, page.value - half)
126 let end = Math.min(totalPages.value, start + maxVisible - 1)
127 if (end - start + 1 < maxVisible) {
128 start = Math.max(1, end - maxVisible + 1)
129 }
130 const pages = []
131 for (let i = start; i <= end; i++) pages.push(i)
132 return pages
133})
134
135function excerpt(html, len = 96) {
136 if (!html) return ''
137 const text = String(html).replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim()
138 return text.length > len ? text.slice(0, len) + '…' : text
139}
140
141async function fetchNews() {
142 loading.value = true
143 const res = await getSiteNews({ page: page.value, perPage, category: category.value })
144 if (res.ok && res.data) {
145 newsList.value = res.data.news || []
146 total.value = res.data.total || 0
147 if (res.data.categories && Array.isArray(res.data.categories)) {
148 categories.value = res.data.categories
149 }
150 } else {
151 newsList.value = []
152 total.value = 0
153 }
154 loading.value = false
155}
156
157function switchCategory(cat) {
158 category.value = cat
159 page.value = 1
160 fetchNews()
161}
162
163function goToPage(p) {
164 if (p < 1 || p > totalPages.value) return
165 page.value = p
166 fetchNews()
167 window.scrollTo({ top: 0, behavior: 'smooth' })
168}
169
170onMounted(async () => {
171 const p = parseInt(route.query.page, 10)
172 if (p > 0) page.value = p
173 const popularRes = await getSiteNewsPopular()
174 if (popularRes.ok && popularRes.data?.popular) {
175 popular.value = popularRes.data.popular
176 }
177 fetchNews()
178})
179</script>
180
181<style scoped>
182.site-news {
183 background: var(--hd-bg);
184}
185
186.site-page-header {
187 background-size: cover;
188 background-position: center;
189 padding: 72px 0;
190 text-align: center;
191}
192
193.site-page-header h1 {
194 margin: 0 0 10px;
195 font-size: 2.2rem;
196 font-weight: 800;
197 letter-spacing: -0.02em;
198 color: #181818;
199 text-shadow: 0 0 10px rgba(255, 255, 255, 0.75);
200}
201
202.site-page-header p {
203 margin: 0;
204 font-size: 1.05rem;
205 color: #444;
206 text-shadow: 0 0 10px rgba(255, 255, 255, 0.75);
207}
208
209.site-sec {
210 padding: 48px 0;
211}
212
213.site-news-layout {
214 display: grid;
215 grid-template-columns: 2fr 1fr;
216 gap: 32px;
217 align-items: start;
218}
219
220/* 分类 tabs */
221.site-news-tabs {
222 display: flex;
223 flex-wrap: wrap;
224 gap: 8px;
225 margin-bottom: 20px;
226}
227
228.site-news-tab {
229 padding: 6px 16px;
230 background: var(--hd-surface);
231 border: 1px solid var(--hd-border);
232 border-radius: 999px;
233 font-size: 13px;
234 color: var(--hd-text-2);
235 cursor: pointer;
236 transition: all var(--hd-dur) var(--hd-ease);
237}
238
239.site-news-tab:hover,
240.site-news-tab.active {
241 background: var(--hd-brand);
242 border-color: var(--hd-brand);
243 color: #fff;
244}
245
246/* 新闻卡片 */
247.site-news-cards {
248 display: grid;
249 gap: 16px;
250}
251
252.site-news-card {
253 background: var(--hd-surface);
254 border: 1px solid var(--hd-border);
255 border-radius: var(--hd-radius-xl);
256 padding: 20px 22px;
257 cursor: pointer;
258 transition: box-shadow var(--hd-dur) var(--hd-ease), transform var(--hd-dur) var(--hd-ease);
259}
260
261.site-news-card:hover {
262 box-shadow: var(--hd-shadow-lg);
263 transform: translateY(-2px);
264}
265
266.site-news-title {
267 position: relative;
268 margin: 0 0 10px;
269 font-size: 1.15rem;
270 font-weight: 700;
271 color: var(--hd-text);
272 padding-bottom: 10px;
273 line-height: 1.4;
274}
275
276.site-news-title::after {
277 content: '';
278 position: absolute;
279 bottom: 0;
280 left: 0;
281 width: 34px;
282 height: 3px;
283 border-radius: 3px;
284 background: var(--hd-brand);
285}
286
287.site-news-excerpt {
288 margin: 0 0 14px;
289 color: var(--hd-text-2);
290 font-size: 14px;
291 line-height: 1.7;
292}
293
294.site-news-meta {
295 display: flex;
296 flex-wrap: wrap;
297 gap: 8px;
298 margin-bottom: 12px;
299}
300
301.site-news-chip {
302 display: inline-flex;
303 align-items: center;
304 gap: 5px;
305 padding: 3px 12px;
306 border-radius: 999px;
307 background: var(--hd-bg);
308 border: 1px solid var(--hd-border);
309 color: var(--hd-text-2);
310 font-size: 12px;
311}
312
313.site-news-more {
314 display: inline-flex;
315 align-items: center;
316 gap: 4px;
317 color: var(--hd-brand);
318 font-size: 13px;
319 font-weight: 600;
320 transition: gap var(--hd-dur) var(--hd-ease);
321}
322
323.site-news-card:hover .site-news-more {
324 gap: 8px;
325}
326
327/* 分页 */
328.site-pagination {
329 display: flex;
330 justify-content: center;
331 align-items: center;
332 gap: 10px;
333 margin-top: 28px;
334 flex-wrap: wrap;
335}
336
337.site-page-btn {
338 display: inline-flex;
339 align-items: center;
340 gap: 4px;
341 padding: 8px 14px;
342 background: var(--hd-surface);
343 border: 1px solid var(--hd-border);
344 border-radius: var(--hd-radius-lg);
345 font-size: 13px;
346 color: var(--hd-text-2);
347 cursor: pointer;
348 transition: all var(--hd-dur) var(--hd-ease);
349}
350
351.site-page-btn:hover:not(:disabled) {
352 color: var(--hd-brand);
353 border-color: var(--hd-brand);
354}
355
356.site-page-btn:disabled {
357 opacity: 0.5;
358 cursor: not-allowed;
359}
360
361.site-page-nums {
362 display: flex;
363 gap: 6px;
364}
365
366.site-page-num {
367 width: 34px;
368 height: 34px;
369 display: grid;
370 place-items: center;
371 background: var(--hd-surface);
372 border: 1px solid var(--hd-border);
373 border-radius: var(--hd-radius-lg);
374 font-size: 13px;
375 color: var(--hd-text-2);
376 cursor: pointer;
377 transition: all var(--hd-dur) var(--hd-ease);
378}
379
380.site-page-num:hover,
381.site-page-num.active {
382 background: var(--hd-brand);
383 border-color: var(--hd-brand);
384 color: #fff;
385}
386
387/* 侧边栏 */
388.site-news-sidebar {
389 position: sticky;
390 top: 76px;
391 display: grid;
392 gap: 16px;
393}
394
395.site-widget {
396 background: var(--hd-surface);
397 border: 1px solid var(--hd-border);
398 border-radius: var(--hd-radius-xl);
399 padding: 20px;
400 box-shadow: var(--hd-shadow);
401}
402
403.site-widget h3 {
404 position: relative;
405 display: flex;
406 align-items: center;
407 gap: 8px;
408 margin: 0 0 16px;
409 font-size: 15px;
410 font-weight: 700;
411 color: var(--hd-text);
412 padding-bottom: 12px;
413 border-bottom: 1px solid var(--hd-border);
414}
415
416.site-widget h3::after {
417 content: '';
418 position: absolute;
419 bottom: -1px;
420 left: 0;
421 width: 30px;
422 height: 3px;
423 border-radius: 3px;
424 background: var(--hd-brand);
425}
426
427.site-popular {
428 display: flex;
429 gap: 12px;
430 padding: 10px 6px;
431 border-radius: var(--hd-radius-lg);
432 cursor: pointer;
433 transition: background var(--hd-dur) var(--hd-ease);
434}
435
436.site-popular:hover {
437 background: var(--hd-brand-light);
438}
439
440.site-popular-rank {
441 width: 24px;
442 height: 24px;
443 flex-shrink: 0;
444 display: grid;
445 place-items: center;
446 border-radius: 50%;
447 background: var(--hd-bg);
448 color: var(--hd-text-3);
449 font-size: 12px;
450 font-weight: 700;
451 margin-top: 2px;
452}
453
454.site-popular-rank.top {
455 background: var(--hd-brand);
456 color: #fff;
457}
458
459.site-popular-title {
460 margin: 0 0 6px;
461 font-size: 14px;
462 font-weight: 600;
463 color: var(--hd-text);
464 line-height: 1.5;
465 display: -webkit-box;
466 -webkit-line-clamp: 2;
467 -webkit-box-orient: vertical;
468 overflow: hidden;
469}
470
471.site-popular:hover .site-popular-title {
472 color: var(--hd-brand);
473}
474
475.site-popular-meta {
476 display: flex;
477 gap: 12px;
478 font-size: 12px;
479 color: var(--hd-text-3);
480}
481
482.site-widget-about p {
483 margin: 0 0 12px;
484 color: var(--hd-text-2);
485 font-size: 13px;
486 line-height: 1.8;
487}
488
489.site-widget-link {
490 display: inline-flex;
491 align-items: center;
492 gap: 4px;
493 color: var(--hd-brand);
494 font-size: 13px;
495 font-weight: 600;
496 text-decoration: none;
497}
498
499.site-widget-link:hover {
500 text-decoration: none;
501 gap: 8px;
502}
503
504.site-empty {
505 text-align: center;
506 padding: 60px 16px;
507 color: var(--hd-text-3);
508}
509
510.site-empty i {
511 font-size: 40px;
512 display: block;
513 margin-bottom: 10px;
514 color: #cbd5e1;
515}
516
517@media (max-width: 860px) {
518 .site-news-layout { grid-template-columns: 1fr; }
519 .site-news-sidebar { position: static; }
520}
521</style>