You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
4.5 KiB

<template>
<div class="alumni-news" :aria-busy="loading">
<div class="alumni-news-list">
<div v-if="error" class="alumni-news-empty">加载失败,<button class="news-retry" @click="load(page)">点击重试</button></div>
<div v-else-if="!rows.length" class="alumni-news-empty">{{ loading ? '加载中…' : '本年度暂无已留存新闻' }}</div>
<template v-else>
<div v-for="item in rows" :key="item.id" class="alumni-news-row">
<span v-if="displayMode" :title="item.title">{{ item.title }}</span>
<a v-else-if="originalUrl(item.source_url)" :href="originalUrl(item.source_url)" target="_blank" rel="noopener noreferrer" :title="item.title">{{ item.title }}</a>
<span v-else :title="item.title + '(暂无原文链接)'">{{ item.title }}</span>
</div>
</template>
</div>
<div class="alumni-news-pagination">
<button type="button" :disabled="displayMode ? lastPage <= 1 : loading || page <= 1" @click="changePage(-1)">上一页</button>
<span>{{ page }} / {{ lastPage }} 页 · 共 {{ total }} 条</span>
<button type="button" :disabled="displayMode ? lastPage <= 1 : loading || page >= lastPage" @click="changePage(1)">下一页</button>
</div>
</div>
</template>
<script>
import { alumniNews } from '@/api/home/index'
export default {
name: 'AlumniNews',
props: {
displayMode: { type: Boolean, default: false }
},
data() { return { rows: [], page: 1, lastPage: 1, total: 0, loading: false, error: false, sequence: 0, rotationTimer: null } },
created() { this.load(1) },
mounted() {
if (this.displayMode) this.startRotation()
},
beforeDestroy() {
clearInterval(this.rotationTimer)
this.sequence++
},
methods: {
startRotation() {
clearInterval(this.rotationTimer)
this.rotationTimer = setInterval(() => {
if (!this.loading) this.load(this.page % this.lastPage + 1)
}, 15000)
},
changePage(direction) {
if (this.loading) return
const target = this.page + direction
if (this.displayMode) {
if (this.lastPage <= 1) return
this.startRotation()
this.load((target - 1 + this.lastPage) % this.lastPage + 1)
} else {
this.load(target)
}
},
originalUrl(value) {
if (typeof value !== 'string' || !/^https?:\/\//i.test(value.trim())) return ''
try { const url = new URL(value.trim()); return ['http:', 'https:'].includes(url.protocol) ? url.href : '' } catch (_) { return '' }
},
async load(page) {
const sequence = ++this.sequence
this.loading = true
this.error = false
try {
const result = await alumniNews({ page }, this.displayMode)
if (sequence !== this.sequence) return
if (page > result.last_page && result.last_page >= 1) return this.load(result.last_page)
this.rows = result.data
this.page = result.current_page
this.lastPage = Math.max(1, result.last_page)
this.total = result.total
} catch (_) { if (sequence === this.sequence) this.error = true } finally {
if (sequence === this.sequence) this.loading = false
}
}
}
}
</script>
<style scoped>
.alumni-news { display: flex; flex-direction: column; flex: 1; min-height: 0; min-width: 0; }
.alumni-news-list { flex: 1; min-height: 0; display: grid; grid-template-rows: repeat(8, minmax(0, 1fr)); overflow: hidden; }
.alumni-news-row { display: flex; align-items: center; min-width: 0; min-height: 0; overflow: hidden; padding: 0 0.8vw; font-size: 0.95vw; font-weight: bold; color: #fff; }
.alumni-news-row:nth-child(even) { background: rgba(21, 37, 123, 0.5); }
.alumni-news-row a, .alumni-news-row span { display: block; width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; line-height: 1.3; }
.alumni-news-row a:hover, .alumni-news-row a:focus-visible { color: #93ccfd; text-decoration: underline; }
.alumni-news-empty { grid-row: 1 / -1; align-self: center; text-align: center; color: #93ccfd; font-size: 0.9vw; }
.alumni-news-pagination { display: flex; align-items: center; justify-content: center; gap: 0.7vw; padding-top: 1vh; flex-shrink: 0; color: #93ccfd; font-size: 0.8vw; }
.alumni-news-pagination button, .news-retry { border: 1px solid rgba(92, 186, 255, 0.55); background: rgba(21, 37, 123, 0.5); color: #93ccfd; border-radius: 4px; padding: 0.4vh 0.5vw; cursor: pointer; font-size: inherit; }
.alumni-news-pagination button:hover:not(:disabled) { background: rgba(59, 130, 246, 0.35); }
.alumni-news-pagination button:disabled { opacity: 0.4; cursor: not-allowed; }
</style>