Files
schoolNews/schoolNewsWeb/src/views/user/resource-center/ResourceCenterView.vue
2025-12-04 11:00:20 +08:00

203 lines
5.1 KiB
Vue

<template>
<div class="resource-center-view">
<CenterHead
title="资源中心"
:category-name="currentCategoryName"
:show-article-mode="showArticle"
/>
<!-- <div class="search-wrapper">
<Search @search="handleSearch" />
</div> -->
<div class="content-wrapper">
<div class="content-container">
<ResourceSideBar
:activeTagID="currentCategoryId"
@category-change="handleCategoryChange"
/>
<ResourceList
v-show="!showArticle"
ref="resourceListRef"
:tagID="currentCategoryId"
:search-keyword="searchKeyword"
@resource-click="handleResourceClick"
@list-updated="handleListUpdated"
/>
<ResourceArticle
v-if="showArticle"
:resource-id="currentResourceId"
:tagID="currentCategoryId"
:resource-list="resourceList"
@resource-change="handleResourceChange"
@navigate="handleArticleNavigate"
@back-to-list="backToList"
/>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ResourceSideBar, ResourceList, ResourceArticle } from './components';
import { Search, CenterHead } from '@/components/base';
import type { Resource, Tag } from '@/types/resource';
import { resourceApi, resourceTagApi } from '@/apis/resource';
const route = useRoute();
const router = useRouter();
const showArticle = ref(false);
const currentCategoryId = ref('tag_article_001');
const currentCategoryName = ref('党史学习');
const currentResourceId = ref('');
const searchKeyword = ref('');
const resourceListRef = ref();
const resourceList = ref<Resource[]>([]);
// 组件加载时检查 URL 参数
onMounted(async () => {
const tagID = route.query.tagID as string;
if (tagID) {
await loadTagInfo(tagID);
}
});
// 监听路由参数变化
watch(() => route.query.tagID, async (newTagID) => {
if (newTagID && typeof newTagID === 'string') {
await loadTagInfo(newTagID);
}
});
// 加载标签信息
async function loadTagInfo(tagID: string) {
try {
const res = await resourceTagApi.getTagsByType(1); // 1 = 文章分类标签
if (res.success && res.dataList) {
const tag = res.dataList.find((t: Tag) => t.tagID === tagID);
if (tag) {
currentCategoryId.value = tag.tagID || '';
currentCategoryName.value = tag.name || '';
searchKeyword.value = '';
showArticle.value = false;
}
}
} catch (error) {
console.error('加载标签信息失败:', error);
}
}
function handleCategoryChange(category: Tag) {
currentCategoryId.value = category.tagID || category.id || '';
currentCategoryName.value = category.name || '';
searchKeyword.value = '';
showArticle.value = false;
// 清除 URL 中的 tagID 参数,确保 URL 与实际显示的分类一致
if (route.query.tagID) {
router.replace({ path: route.path, query: {} });
}
}
function handleSearch(keyword: string) {
searchKeyword.value = keyword;
showArticle.value = false;
// 清除 URL 中的 tagID 参数
if (route.query.tagID) {
router.replace({ path: route.path, query: {} });
}
}
function handleListUpdated(list: Resource[]) {
resourceList.value = list;
}
function handleResourceClick(resource: Resource) {
// 增加浏览次数
resourceApi.incrementViewCount(resource.resourceID || '');
currentResourceId.value = resource.resourceID || '';
showArticle.value = true;
}
function handleResourceChange(resourceId: string) {
currentResourceId.value = resourceId;
// ArticleShowView 会自动重新加载
}
function backToList() {
showArticle.value = false;
currentResourceId.value = '';
}
// 文章内前后切换时,靠近列表头部或尾部触发列表翻页
async function handleArticleNavigate(direction: 'prev' | 'next', resourceId: string) {
const list = resourceListRef.value?.getResources?.() || [];
const index = list.findIndex((r: any) => r.resourceID === resourceId);
if (index === -1) return;
const nearHead = index <= 2;
const nearTail = index >= list.length - 3;
if (nearHead && direction === 'prev') {
await resourceListRef.value?.loadPrevPage?.();
} else if (nearTail && direction === 'next') {
await resourceListRef.value?.loadNextPage?.();
}
}
</script>
<style scoped lang="scss">
.resource-center-view {
background: #F9F9F9;
height: 100%;
overflow-y: auto;
}
.search-wrapper {
width: 100%;
display: flex;
justify-content: center;
padding: 20px 0;
:deep(.resource-search) {
// width: 1200px;
width: 90%;
height: 60px;
padding: 0;
.search-box {
height: 60px;
input {
font-size: 20px;
padding: 0 100px 0 40px;
}
.search-button {
width: 72px;
height: 60px;
img {
width: 24px;
height: 24px;
}
}
}
}
}
.content-wrapper {
width: 100%;
display: flex;
justify-content: center;
}
.content-container {
width: 90%;
display: flex;
align-items: flex-start;
gap: 24px;
height: 100%;
}
</style>