feat(article): 文章模块功能开发
- 新增 ArticleCard 组件,支持可配置封面图显示 (showCover prop) - 新增文章详情页 (article-detail) - 新增文章收藏页 (article-favorites) - 优化分类列表页 (category-list) 支持混合渲染模式: * max_depth > 1: 显示二级分类卡片 (SectionCard) * max_depth === 1: 直接显示文章列表 (ArticleCard) - 过滤空状态项 (max_depth === 1 且 list 为空) - 统一样式规范:标题区域使用渐变背景,内容区域增加间距 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Showing
8 changed files
with
1025 additions
and
52 deletions
docs/tasks/2026-02-27/文章模块改造实施计划.md
0 → 100644
This diff is collapsed. Click to expand it.
docs/tasks/2026-02-27/文章模块改造速记.md
0 → 100644
| 1 | +1. 把以前的文档列表接口换成了文章列表接口. 就是把接口a: file, t: file_list 改成 a:article, t:list, 改成文章列表的主要改动点在list字段的结构, 原来的list字段是一个文件列表, 现在变成了文章列表.根据新的list字段改造列表显示卡片的显示和逻辑. | ||
| 2 | + | ||
| 3 | +post_link 外部链接字段暂时不要使用,因为没有web-view的功能, 以后有需求再考虑. | ||
| 4 | + | ||
| 5 | +2. 新增文章详情页面,有新增文章详情接口, 你先根据后端字段规划页面结构和逻辑生成计划, 不急着生成页面.post_link 外部链接字段暂时不要使用,因为没有web-view的功能, 以后有需求再考虑. | ||
| 6 | +3. 新增文章收藏列表的页面,有新增文章收藏列表接口, 现在我的页面里面有个我的收藏项, 暂时先再配置上屏蔽,新增一个叫我的收藏文章的列表项目, 点击显示用户收藏的文章列表.页面的样式和查询逻辑和原来的收藏列表页面保持一致.post_link 外部链接字段暂时不要使用,因为没有web-view的功能, 以后有需求再考虑. | ||
| 7 | +4. 本周热门资料接口要改掉,改成热门文章接口,等于就是改接口名称, list字段内容可能要重新适配,post_link 外部链接字段暂时不要使用,因为没有web-view的功能, 以后有需求再考虑. |
src/components/cards/ArticleCard.vue
0 → 100644
| 1 | +<template> | ||
| 2 | + <view class="flex flex-row bg-white rounded-[24rpx] p-[24rpx] shadow-sm border border-gray-100 article-card" @tap="handleCardClick"> | ||
| 3 | + <!-- 左侧封面图 --> | ||
| 4 | + <view v-if="showCover" class="w-[160rpx] h-[120rpx] mr-[24rpx] flex-shrink-0 rounded-[16rpx] overflow-hidden bg-gray-100 self-start"> | ||
| 5 | + <image | ||
| 6 | + v-if="coverUrl" | ||
| 7 | + :src="coverUrl" | ||
| 8 | + class="w-full h-full" | ||
| 9 | + mode="aspectFill" | ||
| 10 | + /> | ||
| 11 | + <view v-else class="w-full h-full flex items-center justify-center bg-gradient-to-br from-blue-50 to-blue-100"> | ||
| 12 | + <text class="text-gray-400 text-[40rpx]">📄</text> | ||
| 13 | + </view> | ||
| 14 | + </view> | ||
| 15 | + | ||
| 16 | + <!-- 内容区域 --> | ||
| 17 | + <view class="flex-1 min-w-0"> | ||
| 18 | + <!-- 标题 --> | ||
| 19 | + <view class="text-[#1F2937] text-[30rpx] font-bold leading-[1.4] mb-[8rpx] line-clamp-2"> | ||
| 20 | + {{ title }} | ||
| 21 | + </view> | ||
| 22 | + | ||
| 23 | + <!-- 简介 --> | ||
| 24 | + <view v-if="excerpt" class="text-[#6B7280] text-[24rpx] leading-[1.4] mb-[8rpx] line-clamp-1"> | ||
| 25 | + {{ excerpt }} | ||
| 26 | + </view> | ||
| 27 | + | ||
| 28 | + <!-- 元信息:日期 + 学习人数 --> | ||
| 29 | + <view class="flex items-center gap-[12rpx] mb-[16rpx]"> | ||
| 30 | + <view class="text-[#9CA3AF] text-[22rpx]"> | ||
| 31 | + 📅 {{ formattedDate }} | ||
| 32 | + </view> | ||
| 33 | + <!-- 学习人数 --> | ||
| 34 | + <view v-if="learners" class="inline-flex items-center justify-center px-[8rpx] py-[2rpx] bg-orange-50 text-orange-600 text-[20rpx] font-medium rounded-[6rpx]"> | ||
| 35 | + <text>{{ learners }}人学习</text> | ||
| 36 | + </view> | ||
| 37 | + <!-- 热度百分比 --> | ||
| 38 | + <view v-if="readPeoplePercent !== undefined && readPeoplePercent !== null" class="inline-flex items-center justify-center px-[8rpx] py-[2rpx] bg-green-50 text-green-600 text-[20rpx] font-medium rounded-[6rpx]"> | ||
| 39 | + <text>{{ readPeoplePercent }}%热度</text> | ||
| 40 | + </view> | ||
| 41 | + </view> | ||
| 42 | + | ||
| 43 | + <!-- 分割线 --> | ||
| 44 | + <view class="h-[1rpx] bg-gray-100 my-[20rpx]"></view> | ||
| 45 | + | ||
| 46 | + <!-- 操作按钮 --> | ||
| 47 | + <ListItemActions | ||
| 48 | + :viewable="true" | ||
| 49 | + :collectable="true" | ||
| 50 | + :deletable="false" | ||
| 51 | + :collected="collected" | ||
| 52 | + :item-id="String(id)" | ||
| 53 | + @view="handleView" | ||
| 54 | + @collect="handleCollect" | ||
| 55 | + /> | ||
| 56 | + </view> | ||
| 57 | + </view> | ||
| 58 | +</template> | ||
| 59 | + | ||
| 60 | +<script setup> | ||
| 61 | +/** | ||
| 62 | + * 文章卡片组件 | ||
| 63 | + * | ||
| 64 | + * @description 文章列表项卡片,展示封面图、标题、简介、日期和操作按钮 | ||
| 65 | + * @component ArticleCard | ||
| 66 | + */ | ||
| 67 | + | ||
| 68 | +import { defineProps, defineEmits, computed } from 'vue'; | ||
| 69 | +import { useGo } from '@/hooks/useGo'; | ||
| 70 | +import ListItemActions from '@/components/list/ListItemActions/index.vue'; | ||
| 71 | +import { useCollectOperation } from '@/composables/useCollectOperation'; | ||
| 72 | +import dayjs from 'dayjs'; | ||
| 73 | + | ||
| 74 | +/** | ||
| 75 | + * 组件属性 | ||
| 76 | + */ | ||
| 77 | +const props = defineProps({ | ||
| 78 | + /** 文章 ID */ | ||
| 79 | + id: { | ||
| 80 | + type: [Number, String], | ||
| 81 | + required: true | ||
| 82 | + }, | ||
| 83 | + /** 文章标题 */ | ||
| 84 | + title: { | ||
| 85 | + type: String, | ||
| 86 | + required: true | ||
| 87 | + }, | ||
| 88 | + /** 文章简介 */ | ||
| 89 | + excerpt: { | ||
| 90 | + type: String, | ||
| 91 | + default: '' | ||
| 92 | + }, | ||
| 93 | + /** 封面图 URL */ | ||
| 94 | + coverUrl: { | ||
| 95 | + type: String, | ||
| 96 | + default: '' | ||
| 97 | + }, | ||
| 98 | + /** 发布日期(格式:YYYY-MM-DD HH:mm:ss) */ | ||
| 99 | + date: { | ||
| 100 | + type: String, | ||
| 101 | + default: '' | ||
| 102 | + }, | ||
| 103 | + /** 学习人数文本 */ | ||
| 104 | + learners: { | ||
| 105 | + type: [String, Number], | ||
| 106 | + default: '' | ||
| 107 | + }, | ||
| 108 | + /** 学习人数百分比(热度) */ | ||
| 109 | + readPeoplePercent: { | ||
| 110 | + type: Number, | ||
| 111 | + default: null | ||
| 112 | + }, | ||
| 113 | + /** 是否已收藏 */ | ||
| 114 | + collected: { | ||
| 115 | + type: Boolean, | ||
| 116 | + default: false | ||
| 117 | + }, | ||
| 118 | + /** 是否显示封面图 */ | ||
| 119 | + showCover: { | ||
| 120 | + type: Boolean, | ||
| 121 | + default: true | ||
| 122 | + } | ||
| 123 | +}); | ||
| 124 | + | ||
| 125 | +/** | ||
| 126 | + * 组件事件 | ||
| 127 | + */ | ||
| 128 | +const emit = defineEmits(['viewed', 'collectChanged']); | ||
| 129 | + | ||
| 130 | +const go = useGo(); | ||
| 131 | + | ||
| 132 | +/** | ||
| 133 | + * 格式化日期显示 | ||
| 134 | + * | ||
| 135 | + * @description 将 2025-08-19 06:25:46 格式化为 08-19 | ||
| 136 | + * @returns {string} 格式化后的日期 | ||
| 137 | + */ | ||
| 138 | +const formattedDate = computed(() => { | ||
| 139 | + if (!props.date) return ''; | ||
| 140 | + try { | ||
| 141 | + return dayjs(props.date).format('MM-DD'); | ||
| 142 | + } catch { | ||
| 143 | + return props.date; | ||
| 144 | + } | ||
| 145 | +}); | ||
| 146 | + | ||
| 147 | +/** | ||
| 148 | + * 使用收藏操作 composable | ||
| 149 | + */ | ||
| 150 | +const { toggleCollect } = useCollectOperation(); | ||
| 151 | + | ||
| 152 | +/** | ||
| 153 | + * 处理卡片点击 | ||
| 154 | + * | ||
| 155 | + * @description 跳转到文章详情页 | ||
| 156 | + */ | ||
| 157 | +const handleCardClick = () => { | ||
| 158 | + go('/pages/article-detail/index', { id: props.id }); | ||
| 159 | +}; | ||
| 160 | + | ||
| 161 | +/** | ||
| 162 | + * 处理查看点击 | ||
| 163 | + * | ||
| 164 | + * @description 跳转到文章详情页 | ||
| 165 | + */ | ||
| 166 | +const handleView = () => { | ||
| 167 | + handleCardClick(); | ||
| 168 | + // 通知父组件查看完成 | ||
| 169 | + emit('viewed', { id: props.id }); | ||
| 170 | +}; | ||
| 171 | + | ||
| 172 | +/** | ||
| 173 | + * 处理收藏点击 | ||
| 174 | + * | ||
| 175 | + * @description 调用收藏操作并通知父组件 | ||
| 176 | + */ | ||
| 177 | +const handleCollect = () => { | ||
| 178 | + // 调用收藏操作 | ||
| 179 | + toggleCollect({ | ||
| 180 | + id: props.id, | ||
| 181 | + title: props.title, | ||
| 182 | + excerpt: props.excerpt, | ||
| 183 | + coverUrl: props.coverUrl, | ||
| 184 | + date: props.date, | ||
| 185 | + collected: props.collected | ||
| 186 | + }); | ||
| 187 | + | ||
| 188 | + // 通知父组件收藏状态改变 | ||
| 189 | + emit('collectChanged', { | ||
| 190 | + id: props.id, | ||
| 191 | + title: props.title, | ||
| 192 | + excerpt: props.excerpt, | ||
| 193 | + coverUrl: props.coverUrl, | ||
| 194 | + date: props.date, | ||
| 195 | + collected: !props.collected // 新状态(取反) | ||
| 196 | + }); | ||
| 197 | +}; | ||
| 198 | +</script> | ||
| 199 | + | ||
| 200 | +<style lang="less" scoped> | ||
| 201 | +.article-card { | ||
| 202 | + transition: all 0.2s ease; | ||
| 203 | + | ||
| 204 | + &:active { | ||
| 205 | + transform: scale(0.98); | ||
| 206 | + background-color: #F9FAFB; | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + // 多行文本省略 | ||
| 210 | + .line-clamp-1 { | ||
| 211 | + display: -webkit-box; | ||
| 212 | + -webkit-box-orient: vertical; | ||
| 213 | + -webkit-line-clamp: 1; | ||
| 214 | + line-clamp: 1; | ||
| 215 | + overflow: hidden; | ||
| 216 | + word-break: break-all; | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + .line-clamp-2 { | ||
| 220 | + display: -webkit-box; | ||
| 221 | + -webkit-box-orient: vertical; | ||
| 222 | + -webkit-line-clamp: 2; | ||
| 223 | + line-clamp: 2; | ||
| 224 | + overflow: hidden; | ||
| 225 | + word-break: break-all; | ||
| 226 | + } | ||
| 227 | +} | ||
| 228 | +</style> |
src/pages/article-detail/index.config.js
0 → 100644
src/pages/article-detail/index.vue
0 → 100644
| 1 | +<!-- | ||
| 2 | + * @Date: 2026-02-27 | ||
| 3 | + * @Description: 文章详情页 | ||
| 4 | +--> | ||
| 5 | +<template> | ||
| 6 | + <view class="article-detail-page"> | ||
| 7 | + <!-- 导航栏 --> | ||
| 8 | + <NavHeader title="文章详情" /> | ||
| 9 | + | ||
| 10 | + <!-- 滚动容器 --> | ||
| 11 | + <view class="flex-1 pb-safe"> | ||
| 12 | + <!-- 加载状态 - 自定义 Tailwind CSS 加载动画 --> | ||
| 13 | + <view v-if="loading" class="flex items-center justify-center h-screen"> | ||
| 14 | + <view class="flex flex-col items-center"> | ||
| 15 | + <view class="w-[64rpx] h-[64rpx] border-4 border-gray-200 border-t-blue-600 rounded-full animate-spin mb-[24rpx]"></view> | ||
| 16 | + <text class="text-gray-600 text-[28rpx]">加载中...</text> | ||
| 17 | + </view> | ||
| 18 | + </view> | ||
| 19 | + | ||
| 20 | + <!-- 文章内容 --> | ||
| 21 | + <view v-else-if="article" class="article-content"> | ||
| 22 | + <!-- 封面图 --> | ||
| 23 | + <view v-if="article.coverUrl" class="cover-image-wrapper"> | ||
| 24 | + <image :src="article.coverUrl" mode="widthFix" class="cover-image" /> | ||
| 25 | + </view> | ||
| 26 | + | ||
| 27 | + <!-- 文章信息 --> | ||
| 28 | + <view class="article-info px-[32rpx]"> | ||
| 29 | + <!-- 标题 --> | ||
| 30 | + <view class="article-title">{{ article.title }}</view> | ||
| 31 | + | ||
| 32 | + <!-- 作者和日期 --> | ||
| 33 | + <view class="article-meta"> | ||
| 34 | + <text v-if="article.authorName" class="meta-item">{{ article.authorName }}</text> | ||
| 35 | + <text v-if="article.authorName && article.date" class="meta-separator">·</text> | ||
| 36 | + <text v-if="article.date" class="meta-item">{{ formattedDate }}</text> | ||
| 37 | + </view> | ||
| 38 | + </view> | ||
| 39 | + | ||
| 40 | + <!-- 分割线 --> | ||
| 41 | + <view class="divider"></view> | ||
| 42 | + | ||
| 43 | + <!-- 富文本内容 --> | ||
| 44 | + <view class="article-body px-[32rpx]"> | ||
| 45 | + <rich-text :nodes="processedContent" class="rich-text-content" /> | ||
| 46 | + </view> | ||
| 47 | + </view> | ||
| 48 | + | ||
| 49 | + <!-- 加载失败 - 自定义空状态 --> | ||
| 50 | + <view v-else-if="error" class="error-state"> | ||
| 51 | + <view class="flex flex-col items-center justify-center h-screen"> | ||
| 52 | + <view class="text-[#9CA3AF] text-[120rpx] mb-[24rpx]">⚠️</view> | ||
| 53 | + <text class="text-gray-600 text-[28rpx] mb-[32rpx]">加载失败</text> | ||
| 54 | + <view | ||
| 55 | + class="px-[48rpx] py-[20rpx] bg-blue-600 text-white rounded-full" | ||
| 56 | + @tap="fetchArticleDetail" | ||
| 57 | + > | ||
| 58 | + <text class="text-[28rpx]">重试</text> | ||
| 59 | + </view> | ||
| 60 | + </view> | ||
| 61 | + </view> | ||
| 62 | + | ||
| 63 | + <!-- 底部安全区域 --> | ||
| 64 | + <view class="safe-area-bottom"></view> | ||
| 65 | + </view> | ||
| 66 | + | ||
| 67 | + <!-- 底部收藏按钮 --> | ||
| 68 | + <view class="footer-bar"> | ||
| 69 | + <view class="footer-content"> | ||
| 70 | + <view | ||
| 71 | + v-if="article" | ||
| 72 | + :class="['collect-button', article.is_favorite ? 'collected' : '']" | ||
| 73 | + @tap="toggleCollect" | ||
| 74 | + > | ||
| 75 | + <text class="collect-icon">{{ article.is_favorite ? '★' : '☆' }}</text> | ||
| 76 | + <text class="collect-text">{{ article.is_favorite ? '已收藏' : '收藏' }}</text> | ||
| 77 | + </view> | ||
| 78 | + </view> | ||
| 79 | + </view> | ||
| 80 | + </view> | ||
| 81 | +</template> | ||
| 82 | + | ||
| 83 | +<script setup> | ||
| 84 | +import { ref, computed } from 'vue' | ||
| 85 | +import { useLoad } from '@tarojs/taro' | ||
| 86 | +import NavHeader from '@/components/navigation/NavHeader.vue' | ||
| 87 | +import { articleDetailAPI } from '@/api/article' | ||
| 88 | +import { addAPI, delAPI } from '@/api/favorite' | ||
| 89 | +import { mockArticleDetailAPI } from '@/utils/mockData' | ||
| 90 | +import eventBus, { Events } from '@/utils/eventBus' | ||
| 91 | +import dayjs from 'dayjs' | ||
| 92 | +import Taro from '@tarojs/taro' | ||
| 93 | +import { USE_MOCK_DATA } from '@/config/app' | ||
| 94 | + | ||
| 95 | +/** | ||
| 96 | + * 文章数据 | ||
| 97 | + */ | ||
| 98 | +const article = ref(null) | ||
| 99 | + | ||
| 100 | +/** | ||
| 101 | + * 加载状态 | ||
| 102 | + */ | ||
| 103 | +const loading = ref(true) | ||
| 104 | + | ||
| 105 | +/** | ||
| 106 | + * 错误状态 | ||
| 107 | + */ | ||
| 108 | +const error = ref(false) | ||
| 109 | + | ||
| 110 | +/** | ||
| 111 | + * 文章 ID | ||
| 112 | + */ | ||
| 113 | +const articleId = ref(null) | ||
| 114 | + | ||
| 115 | +/** | ||
| 116 | + * 格式化日期显示 | ||
| 117 | + */ | ||
| 118 | +const formattedDate = computed(() => { | ||
| 119 | + if (!article.value?.post_date) return '' | ||
| 120 | + try { | ||
| 121 | + return dayjs(article.value.post_date).format('YYYY年MM月DD日') | ||
| 122 | + } catch { | ||
| 123 | + return article.value.post_date | ||
| 124 | + } | ||
| 125 | +}) | ||
| 126 | + | ||
| 127 | +/** | ||
| 128 | + * 处理富文本内容,适配图片宽度 | ||
| 129 | + */ | ||
| 130 | +const processedContent = computed(() => { | ||
| 131 | + if (!article.value?.content) return '' | ||
| 132 | + // 给 img 标签添加内联样式,确保宽度不超过容器 | ||
| 133 | + return article.value.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:12px auto;"') | ||
| 134 | +}) | ||
| 135 | + | ||
| 136 | +/** | ||
| 137 | + * 获取文章详情 | ||
| 138 | + */ | ||
| 139 | +const fetchArticleDetail = async () => { | ||
| 140 | + if (!articleId.value) return | ||
| 141 | + | ||
| 142 | + loading.value = true | ||
| 143 | + error.value = false | ||
| 144 | + | ||
| 145 | + try { | ||
| 146 | + console.log('[Article Detail] 获取文章详情:', articleId.value) | ||
| 147 | + console.log('[Article Detail] 使用 Mock 数据:', USE_MOCK_DATA) | ||
| 148 | + | ||
| 149 | + const res = USE_MOCK_DATA | ||
| 150 | + ? await mockArticleDetailAPI({ i: articleId.value }) | ||
| 151 | + : await articleDetailAPI({ i: articleId.value }) | ||
| 152 | + | ||
| 153 | + if (res.code === 1 && res.data) { | ||
| 154 | + console.log('[Article Detail] 数据:', res.data) | ||
| 155 | + | ||
| 156 | + article.value = { | ||
| 157 | + id: res.data.id, | ||
| 158 | + title: res.data.post_title || '未命名文章', | ||
| 159 | + content: res.data.post_content || '', | ||
| 160 | + excerpt: res.data.post_excerpt || '', | ||
| 161 | + coverUrl: res.data.cover_url || res.data.post_thumbnail || '', | ||
| 162 | + date: res.data.post_date || '', | ||
| 163 | + authorName: res.data.author_name || '', | ||
| 164 | + is_favorite: res.data.is_favorite === 1 || res.data.is_favorite === '1' | ||
| 165 | + } | ||
| 166 | + } else { | ||
| 167 | + error.value = true | ||
| 168 | + Taro.showToast({ | ||
| 169 | + title: res.msg || '获取文章详情失败', | ||
| 170 | + icon: 'none' | ||
| 171 | + }) | ||
| 172 | + } | ||
| 173 | + } catch (err) { | ||
| 174 | + console.error('[Article Detail] 获取文章详情失败:', err) | ||
| 175 | + error.value = true | ||
| 176 | + Taro.showToast({ | ||
| 177 | + title: '加载失败', | ||
| 178 | + icon: 'error' | ||
| 179 | + }) | ||
| 180 | + } finally { | ||
| 181 | + loading.value = false | ||
| 182 | + } | ||
| 183 | +} | ||
| 184 | + | ||
| 185 | +/** | ||
| 186 | + * 切换收藏状态 | ||
| 187 | + */ | ||
| 188 | +const toggleCollect = async () => { | ||
| 189 | + if (!article.value) return | ||
| 190 | + | ||
| 191 | + try { | ||
| 192 | + const newCollectStatus = !article.value.is_favorite | ||
| 193 | + | ||
| 194 | + // 调用收藏 API(使用与文件相同的收藏 API) | ||
| 195 | + const res = newCollectStatus | ||
| 196 | + ? await addAPI({ meta_id: article.value.id }) | ||
| 197 | + : await delAPI({ meta_id: article.value.id }) | ||
| 198 | + | ||
| 199 | + if (res.code === 1) { | ||
| 200 | + // 更新本地状态 | ||
| 201 | + article.value.is_favorite = newCollectStatus | ||
| 202 | + | ||
| 203 | + Taro.showToast({ | ||
| 204 | + title: newCollectStatus ? '已收藏' : '已取消收藏', | ||
| 205 | + icon: 'success', | ||
| 206 | + duration: 1000 | ||
| 207 | + }) | ||
| 208 | + | ||
| 209 | + // 发送收藏更新事件 | ||
| 210 | + eventBus.emit(Events.FAVORITES_UPDATE, { | ||
| 211 | + metaId: article.value.id, | ||
| 212 | + collected: newCollectStatus, | ||
| 213 | + timestamp: Date.now() | ||
| 214 | + }) | ||
| 215 | + } else { | ||
| 216 | + Taro.showToast({ | ||
| 217 | + title: res.msg || '操作失败', | ||
| 218 | + icon: 'none', | ||
| 219 | + duration: 2000 | ||
| 220 | + }) | ||
| 221 | + } | ||
| 222 | + } catch (err) { | ||
| 223 | + console.error('[Article Detail] 收藏操作失败:', err) | ||
| 224 | + Taro.showToast({ | ||
| 225 | + title: '网络错误,请重试', | ||
| 226 | + icon: 'none', | ||
| 227 | + duration: 2000 | ||
| 228 | + }) | ||
| 229 | + } | ||
| 230 | +} | ||
| 231 | + | ||
| 232 | +/** | ||
| 233 | + * 滚动到底部事件 | ||
| 234 | + */ | ||
| 235 | +const onScrollToLower = () => { | ||
| 236 | + // 可以在这里加载相关文章推荐等 | ||
| 237 | + console.log('[Article Detail] 滚动到底部') | ||
| 238 | +} | ||
| 239 | + | ||
| 240 | +/** | ||
| 241 | + * 页面加载时获取文章详情 | ||
| 242 | + */ | ||
| 243 | +useLoad((options) => { | ||
| 244 | + console.log('[Article Detail] 页面参数:', options) | ||
| 245 | + | ||
| 246 | + if (options.id) { | ||
| 247 | + articleId.value = options.id | ||
| 248 | + fetchArticleDetail() | ||
| 249 | + } else { | ||
| 250 | + error.value = true | ||
| 251 | + loading.value = false | ||
| 252 | + Taro.showToast({ | ||
| 253 | + title: '文章ID不存在', | ||
| 254 | + icon: 'none' | ||
| 255 | + }) | ||
| 256 | + } | ||
| 257 | +}) | ||
| 258 | +</script> | ||
| 259 | + | ||
| 260 | +<style lang="less"> | ||
| 261 | +.article-detail-page { | ||
| 262 | + display: flex; | ||
| 263 | + flex-direction: column; | ||
| 264 | + min-height: 100vh; | ||
| 265 | + background-color: #F9FAFB; | ||
| 266 | +} | ||
| 267 | + | ||
| 268 | +.cover-image-wrapper { | ||
| 269 | + width: 100%; | ||
| 270 | + background-color: #F3F4F6; | ||
| 271 | +} | ||
| 272 | + | ||
| 273 | +.cover-image { | ||
| 274 | + width: 100%; | ||
| 275 | + display: block; | ||
| 276 | +} | ||
| 277 | + | ||
| 278 | +.article-info { | ||
| 279 | + padding-top: 32rpx; | ||
| 280 | + padding-bottom: 24rpx; | ||
| 281 | +} | ||
| 282 | + | ||
| 283 | +.article-title { | ||
| 284 | + font-size: 40rpx; | ||
| 285 | + font-weight: bold; | ||
| 286 | + color: #1F2937; | ||
| 287 | + line-height: 1.4; | ||
| 288 | + margin-bottom: 16rpx; | ||
| 289 | +} | ||
| 290 | + | ||
| 291 | +.article-meta { | ||
| 292 | + display: flex; | ||
| 293 | + align-items: center; | ||
| 294 | + font-size: 24rpx; | ||
| 295 | + color: #9CA3AF; | ||
| 296 | +} | ||
| 297 | + | ||
| 298 | +.meta-item { | ||
| 299 | + margin-right: 8rpx; | ||
| 300 | +} | ||
| 301 | + | ||
| 302 | +.meta-separator { | ||
| 303 | + margin-right: 8rpx; | ||
| 304 | +} | ||
| 305 | + | ||
| 306 | +.divider { | ||
| 307 | + height: 1rpx; | ||
| 308 | + background-color: #E5E7EB; | ||
| 309 | + margin: 0 0 32rpx 0; | ||
| 310 | +} | ||
| 311 | + | ||
| 312 | +.article-body { | ||
| 313 | + padding-bottom: 32rpx; | ||
| 314 | +} | ||
| 315 | + | ||
| 316 | +.rich-text-content { | ||
| 317 | + font-size: 30rpx; | ||
| 318 | + color: #374151; | ||
| 319 | + line-height: 1.8; | ||
| 320 | + word-wrap: break-word; | ||
| 321 | + overflow: hidden; | ||
| 322 | + | ||
| 323 | + /* 富文本图片样式:确保宽度适配移动端 */ | ||
| 324 | + :deep(img) { | ||
| 325 | + max-width: 100% !important; | ||
| 326 | + width: auto !important; | ||
| 327 | + height: auto !important; | ||
| 328 | + display: block !important; | ||
| 329 | + margin: 24rpx auto !important; | ||
| 330 | + border-radius: 12rpx; | ||
| 331 | + } | ||
| 332 | + | ||
| 333 | + /* 标题样式优化 */ | ||
| 334 | + :deep(h1), | ||
| 335 | + :deep(h2), | ||
| 336 | + :deep(h3), | ||
| 337 | + :deep(h4), | ||
| 338 | + :deep(h5), | ||
| 339 | + :deep(h6) { | ||
| 340 | + margin: 24rpx 0 16rpx; | ||
| 341 | + font-weight: bold; | ||
| 342 | + color: #1F2937; | ||
| 343 | + } | ||
| 344 | + | ||
| 345 | + :deep(h1) { | ||
| 346 | + font-size: 40rpx; | ||
| 347 | + } | ||
| 348 | + | ||
| 349 | + :deep(h2) { | ||
| 350 | + font-size: 36rpx; | ||
| 351 | + } | ||
| 352 | + | ||
| 353 | + :deep(h3) { | ||
| 354 | + font-size: 32rpx; | ||
| 355 | + } | ||
| 356 | + | ||
| 357 | + /* 段落样式 */ | ||
| 358 | + :deep(p) { | ||
| 359 | + margin: 16rpx 0; | ||
| 360 | + } | ||
| 361 | + | ||
| 362 | + /* 列表样式 */ | ||
| 363 | + :deep(ul), | ||
| 364 | + :deep(ol) { | ||
| 365 | + padding-left: 32rpx; | ||
| 366 | + margin: 16rpx 0; | ||
| 367 | + } | ||
| 368 | + | ||
| 369 | + :deep(li) { | ||
| 370 | + margin: 8rpx 0; | ||
| 371 | + } | ||
| 372 | + | ||
| 373 | + /* 引用样式 */ | ||
| 374 | + :deep(blockquote) { | ||
| 375 | + padding: 16rpx 24rpx; | ||
| 376 | + margin: 16rpx 0; | ||
| 377 | + background-color: #F3F4F6; | ||
| 378 | + border-left: 4rpx solid #D1D5DB; | ||
| 379 | + color: #6B7280; | ||
| 380 | + } | ||
| 381 | + | ||
| 382 | + /* 链接样式 */ | ||
| 383 | + :deep(a) { | ||
| 384 | + color: #2563EB; | ||
| 385 | + text-decoration: underline; | ||
| 386 | + } | ||
| 387 | +} | ||
| 388 | + | ||
| 389 | +.safe-area-bottom { | ||
| 390 | + height: 120rpx; | ||
| 391 | +} | ||
| 392 | + | ||
| 393 | +.footer-bar { | ||
| 394 | + position: fixed; | ||
| 395 | + bottom: 0; | ||
| 396 | + left: 0; | ||
| 397 | + right: 0; | ||
| 398 | + background-color: #fff; | ||
| 399 | + border-top: 1rpx solid #E5E7EB; | ||
| 400 | + padding-bottom: env(safe-area-inset-bottom); | ||
| 401 | +} | ||
| 402 | + | ||
| 403 | +.footer-content { | ||
| 404 | + display: flex; | ||
| 405 | + justify-content: center; | ||
| 406 | + align-items: center; | ||
| 407 | + padding: 20rpx 32rpx; | ||
| 408 | +} | ||
| 409 | + | ||
| 410 | +.collect-button { | ||
| 411 | + display: flex; | ||
| 412 | + align-items: center; | ||
| 413 | + justify-content: center; | ||
| 414 | + padding: 16rpx 48rpx; | ||
| 415 | + border-radius: 999rpx; | ||
| 416 | + background-color: #F3F4F6; | ||
| 417 | + transition: all 0.2s ease; | ||
| 418 | + | ||
| 419 | + &.collected { | ||
| 420 | + background-color: #FEF3C7; | ||
| 421 | + | ||
| 422 | + .collect-icon { | ||
| 423 | + color: #F59E0B; | ||
| 424 | + } | ||
| 425 | + | ||
| 426 | + .collect-text { | ||
| 427 | + color: #F59E0B; | ||
| 428 | + } | ||
| 429 | + } | ||
| 430 | +} | ||
| 431 | + | ||
| 432 | +.collect-icon { | ||
| 433 | + font-size: 32rpx; | ||
| 434 | + color: #9CA3AF; | ||
| 435 | + margin-right: 8rpx; | ||
| 436 | +} | ||
| 437 | + | ||
| 438 | +.collect-text { | ||
| 439 | + font-size: 28rpx; | ||
| 440 | + color: #6B7280; | ||
| 441 | +} | ||
| 442 | + | ||
| 443 | +.error-state { | ||
| 444 | + display: flex; | ||
| 445 | + justify-content: center; | ||
| 446 | + align-items: center; | ||
| 447 | + min-height: 400rpx; | ||
| 448 | +} | ||
| 449 | +</style> |
src/pages/article-favorites/index.config.js
0 → 100644
| 1 | +/* | ||
| 2 | + * @Date: 2026-02-27 13:03:03 | ||
| 3 | + * @LastEditors: hookehuyr hookehuyr@gmail.com | ||
| 4 | + * @LastEditTime: 2026-02-27 14:42:14 | ||
| 5 | + * @FilePath: /manulife-weapp/src/pages/article-favorites/index.config.js | ||
| 6 | + * @Description: 我的收藏文章页面配置 | ||
| 7 | + */ | ||
| 8 | +export default definePageConfig({ | ||
| 9 | + navigationBarTitleText: '我的收藏文章', | ||
| 10 | + backgroundColor: '#F9FAFB', | ||
| 11 | + navigationStyle: 'custom' | ||
| 12 | +}) |
src/pages/article-favorites/index.vue
0 → 100644
| 1 | +<!-- | ||
| 2 | + * @Date: 2026-02-27 | ||
| 3 | + * @Description: 文章收藏列表页 - 复用 favorites 页面结构 | ||
| 4 | +--> | ||
| 5 | +<template> | ||
| 6 | + <view class="h-screen bg-gray-50 flex flex-col overflow-hidden"> | ||
| 7 | + <view class="bg-gray-50 z-10"> | ||
| 8 | + <NavHeader title="我的收藏文章" /> | ||
| 9 | + </view> | ||
| 10 | + | ||
| 11 | + <!-- LoadMoreList 组件 --> | ||
| 12 | + <LoadMoreList | ||
| 13 | + :list="currentList" | ||
| 14 | + :page="currentPage" | ||
| 15 | + :page-size="pageSize" | ||
| 16 | + :has-more="hasMore" | ||
| 17 | + :loading="loading" | ||
| 18 | + :loading-more="loadingMore" | ||
| 19 | + key-field="id" | ||
| 20 | + :show-header="false" | ||
| 21 | + :has-footer="false" | ||
| 22 | + @load-more="handleLoadMore" | ||
| 23 | + > | ||
| 24 | + <!-- 列表项 --> | ||
| 25 | + <template #item="{ item }"> | ||
| 26 | + <ArticleCard | ||
| 27 | + :id="item.id" | ||
| 28 | + :title="item.title" | ||
| 29 | + :excerpt="item.excerpt" | ||
| 30 | + :cover-url="item.coverUrl" | ||
| 31 | + :date="item.date" | ||
| 32 | + :collected="item.collected" | ||
| 33 | + @viewed="onView(item)" | ||
| 34 | + @collect-changed="handleCollectChanged(item, $event)" | ||
| 35 | + /> | ||
| 36 | + </template> | ||
| 37 | + </LoadMoreList> | ||
| 38 | + </view> | ||
| 39 | +</template> | ||
| 40 | + | ||
| 41 | +<script setup> | ||
| 42 | +import { ref, onMounted, onUnmounted } from 'vue' | ||
| 43 | +import Taro, { useLoad } from '@tarojs/taro' | ||
| 44 | +import LoadMoreList from '@/components/list/LoadMoreList' | ||
| 45 | +import ArticleCard from '@/components/cards/ArticleCard.vue' | ||
| 46 | +import NavHeader from '@/components/navigation/NavHeader.vue' | ||
| 47 | +import { favoriteAPI } from '@/api/article' | ||
| 48 | +import { mockArticleFavoriteAPI } from '@/utils/mockData' | ||
| 49 | +import eventBus, { Events } from '@/utils/eventBus' | ||
| 50 | +import { USE_MOCK_DATA } from '@/config/app' | ||
| 51 | + | ||
| 52 | +// ⚠️ MOCK 数据开关 - 统一从 @/config/app 导入 | ||
| 53 | +// const USE_MOCK_DATA = process.env.NODE_ENV === 'development' | ||
| 54 | + | ||
| 55 | +/** | ||
| 56 | + * 当前列表数据 | ||
| 57 | + */ | ||
| 58 | +const currentList = ref([]) | ||
| 59 | + | ||
| 60 | +/** | ||
| 61 | + * 当前页码(从0开始) | ||
| 62 | + */ | ||
| 63 | +const currentPage = ref(0) | ||
| 64 | + | ||
| 65 | +/** | ||
| 66 | + * 每页数量 | ||
| 67 | + */ | ||
| 68 | +const pageSize = 20 | ||
| 69 | + | ||
| 70 | +/** | ||
| 71 | + * 是否还有更多数据 | ||
| 72 | + */ | ||
| 73 | +const hasMore = ref(true) | ||
| 74 | + | ||
| 75 | +/** | ||
| 76 | + * 首次加载状态 | ||
| 77 | + */ | ||
| 78 | +const loading = ref(false) | ||
| 79 | + | ||
| 80 | +/** | ||
| 81 | + * 加载更多状态 | ||
| 82 | + */ | ||
| 83 | +const loadingMore = ref(false) | ||
| 84 | + | ||
| 85 | +/** | ||
| 86 | + * 获取文章收藏列表 | ||
| 87 | + * | ||
| 88 | + * @param {Object} params - 请求参数 | ||
| 89 | + * @param {number} params.page - 页码(从0开始) | ||
| 90 | + * @param {number} params.limit - 每页数量 | ||
| 91 | + * @param {boolean} isLoadMore - 是否为加载更多 | ||
| 92 | + */ | ||
| 93 | +const fetchFavoritesList = async (params = {}, isLoadMore = false) => { | ||
| 94 | + try { | ||
| 95 | + if (isLoadMore) { | ||
| 96 | + loadingMore.value = true | ||
| 97 | + } else { | ||
| 98 | + loading.value = true | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + console.log('[Article Favorites] 请求参数:', params) | ||
| 102 | + console.log('[Article Favorites] 使用 Mock 数据:', USE_MOCK_DATA) | ||
| 103 | + | ||
| 104 | + const res = USE_MOCK_DATA | ||
| 105 | + ? await mockArticleFavoriteAPI(params) | ||
| 106 | + : await favoriteAPI({ | ||
| 107 | + page: String(params.page), | ||
| 108 | + limit: String(params.limit) | ||
| 109 | + }) | ||
| 110 | + | ||
| 111 | + if (res.code === 1 && res.data && res.data.list) { | ||
| 112 | + console.log('[Article Favorites] 数据:', res.data.list) | ||
| 113 | + | ||
| 114 | + const processedList = res.data.list.map(item => ({ | ||
| 115 | + id: item.id, | ||
| 116 | + title: item.post_title || '未命名文章', | ||
| 117 | + excerpt: item.post_excerpt || '', | ||
| 118 | + coverUrl: '', | ||
| 119 | + date: item.post_date || '', | ||
| 120 | + collected: true // 收藏列表中的文章都是已收藏的 | ||
| 121 | + })) | ||
| 122 | + | ||
| 123 | + if (isLoadMore) { | ||
| 124 | + currentList.value = [...currentList.value, ...processedList] | ||
| 125 | + } else { | ||
| 126 | + currentList.value = processedList | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + hasMore.value = res.data.list.length >= params.limit | ||
| 130 | + } else { | ||
| 131 | + if (!isLoadMore) { | ||
| 132 | + currentList.value = [] | ||
| 133 | + } | ||
| 134 | + Taro.showToast({ | ||
| 135 | + title: res.msg || '获取收藏列表失败', | ||
| 136 | + icon: 'none' | ||
| 137 | + }) | ||
| 138 | + } | ||
| 139 | + } catch (err) { | ||
| 140 | + console.error('[Article Favorites] 获取收藏列表失败:', err) | ||
| 141 | + if (!isLoadMore) { | ||
| 142 | + currentList.value = [] | ||
| 143 | + } | ||
| 144 | + Taro.showToast({ | ||
| 145 | + title: '网络错误,请稍后重试', | ||
| 146 | + icon: 'none' | ||
| 147 | + }) | ||
| 148 | + } finally { | ||
| 149 | + if (isLoadMore) { | ||
| 150 | + loadingMore.value = false | ||
| 151 | + } else { | ||
| 152 | + loading.value = false | ||
| 153 | + } | ||
| 154 | + } | ||
| 155 | +} | ||
| 156 | + | ||
| 157 | +/** | ||
| 158 | + * 处理收藏状态改变 | ||
| 159 | + */ | ||
| 160 | +const handleCollectChanged = (item, newStatus) => { | ||
| 161 | + console.log('[Article Favorites] 收藏状态改变:', item.title, newStatus.collected) | ||
| 162 | + | ||
| 163 | + if (!newStatus.collected) { | ||
| 164 | + // 取消收藏,从列表中移除 | ||
| 165 | + const index = currentList.value.findIndex(i => i.id === item.id) | ||
| 166 | + if (index !== -1) { | ||
| 167 | + currentList.value.splice(index, 1) | ||
| 168 | + } | ||
| 169 | + } | ||
| 170 | +} | ||
| 171 | + | ||
| 172 | +/** | ||
| 173 | + * 查看文章 | ||
| 174 | + */ | ||
| 175 | +const onView = (item) => { | ||
| 176 | + console.log('[Article Favorites] 查看文章:', item.title) | ||
| 177 | + // 跳转到文章详情页 | ||
| 178 | + Taro.navigateTo({ | ||
| 179 | + url: `/pages/article-detail/index?id=${item.id}` | ||
| 180 | + }) | ||
| 181 | +} | ||
| 182 | + | ||
| 183 | +/** | ||
| 184 | + * 处理加载更多事件 | ||
| 185 | + */ | ||
| 186 | +const handleLoadMore = async (page) => { | ||
| 187 | + console.log('[Article Favorites] 加载更多,页码:', page) | ||
| 188 | + currentPage.value = page | ||
| 189 | + await fetchFavoritesList({ page: page, limit: pageSize }, true) | ||
| 190 | +} | ||
| 191 | + | ||
| 192 | +/** | ||
| 193 | + * 刷新收藏列表 | ||
| 194 | + */ | ||
| 195 | +const refreshList = async () => { | ||
| 196 | + console.log('[Article Favorites] 刷新列表') | ||
| 197 | + currentPage.value = 0 | ||
| 198 | + hasMore.value = true | ||
| 199 | + await fetchFavoritesList({ page: 0, limit: pageSize }) | ||
| 200 | +} | ||
| 201 | + | ||
| 202 | +/** | ||
| 203 | + * 页面加载时获取列表 | ||
| 204 | + */ | ||
| 205 | +useLoad(() => { | ||
| 206 | + console.log('[Article Favorites] 页面加载,获取列表') | ||
| 207 | + currentPage.value = 0 | ||
| 208 | + hasMore.value = true | ||
| 209 | + fetchFavoritesList({ page: 0, limit: pageSize }) | ||
| 210 | +}) | ||
| 211 | + | ||
| 212 | +/** | ||
| 213 | + * 监听收藏更新事件 | ||
| 214 | + */ | ||
| 215 | +onMounted(() => { | ||
| 216 | + console.log('[Article Favorites] 注册事件监听') | ||
| 217 | + | ||
| 218 | + const unsubscribe = eventBus.on(Events.FAVORITES_UPDATE, async () => { | ||
| 219 | + console.log('[Article Favorites] 收到收藏更新事件') | ||
| 220 | + await refreshList() | ||
| 221 | + }) | ||
| 222 | + | ||
| 223 | + onUnmounted(() => { | ||
| 224 | + unsubscribe() | ||
| 225 | + console.log('[Article Favorites] 取消事件监听') | ||
| 226 | + }) | ||
| 227 | +}) | ||
| 228 | +</script> | ||
| 229 | + | ||
| 230 | +<style lang="less"> | ||
| 231 | +/* LoadMoreList 组件已内置样式 */ | ||
| 232 | +</style> |
| ... | @@ -9,20 +9,51 @@ | ... | @@ -9,20 +9,51 @@ |
| 9 | </div> | 9 | </div> |
| 10 | 10 | ||
| 11 | <!-- Content List --> | 11 | <!-- Content List --> |
| 12 | - <div v-else-if="sections.length > 0" class="px-[40rpx] mt-[40rpx] relative z-10"> | 12 | + <div v-else-if="filteredChildren.length > 0" class="px-[40rpx] mt-[40rpx] relative z-10"> |
| 13 | - <SectionCard | 13 | + <template v-for="item in filteredChildren" :key="item.id"> |
| 14 | - v-for="(section, index) in sections" | 14 | + <!-- 有子分类:显示 SectionCard --> |
| 15 | - :key="index" | 15 | + <SectionCard |
| 16 | - :title="section.title" | 16 | + v-if="item.max_depth > 1" |
| 17 | - :items="section.items" | 17 | + :title="item.category_name" |
| 18 | - @item-click="handleItemClick" | 18 | + :items="convertToItems(item.children)" |
| 19 | - /> | 19 | + @item-click="handleItemClick" |
| 20 | + /> | ||
| 21 | + | ||
| 22 | + <!-- 无子分类且有文章:直接显示 ArticleCard 列表 --> | ||
| 23 | + <template v-else-if="item.max_depth === 1 && item.list?.length"> | ||
| 24 | + <view class="bg-white rounded-[32rpx] mb-[32rpx] overflow-hidden shadow-sm"> | ||
| 25 | + <!-- 标题区域 - 与 SectionCard 一致 --> | ||
| 26 | + <view class="px-[40rpx] py-[32rpx]" style="background: linear-gradient(90deg, #EFF6FF 0%, #DBEAFE 100%)"> | ||
| 27 | + <text class="text-[#1f2937] text-[32rpx] font-normal">{{ item.category_name }}</text> | ||
| 28 | + </view> | ||
| 29 | + | ||
| 30 | + <!-- 文章列表 --> | ||
| 31 | + <view class="px-[32rpx] pt-[24rpx] pb-[32rpx]"> | ||
| 32 | + <view v-for="(article, index) in item.list" :key="article.id"> | ||
| 33 | + <ArticleCard | ||
| 34 | + :id="article.id" | ||
| 35 | + :title="article.post_title" | ||
| 36 | + :excerpt="article.post_excerpt" | ||
| 37 | + :date="article.formatted_post_date || article.post_date" | ||
| 38 | + :collected="article.is_favorite === 1" | ||
| 39 | + :show-cover="false" | ||
| 40 | + @collect-changed="handleCollectChanged" | ||
| 41 | + /> | ||
| 42 | + <!-- 卡片间距(最后一项不显示) --> | ||
| 43 | + <view v-if="index < item.list.length - 1" class="h-[16rpx]"></view> | ||
| 44 | + </view> | ||
| 45 | + </view> | ||
| 46 | + </view> | ||
| 47 | + </template> | ||
| 48 | + </template> | ||
| 20 | </div> | 49 | </div> |
| 21 | 50 | ||
| 22 | <!-- Empty State --> | 51 | <!-- Empty State --> |
| 23 | <div v-else class="px-[40rpx] mt-[80rpx] flex items-center justify-center"> | 52 | <div v-else class="px-[40rpx] mt-[80rpx] flex items-center justify-center"> |
| 24 | - <!-- <text class="text-[#9CA3AF] text-[28rpx]">暂无分类</text> --> | 53 | + <view class="flex flex-col items-center"> |
| 25 | - <nut-empty description="暂无分类" image="empty" /> | 54 | + <view class="text-[#9CA3AF] text-[120rpx] mb-[24rpx]">📂</view> |
| 55 | + <text class="text-gray-600 text-[28rpx]">暂无分类</text> | ||
| 56 | + </view> | ||
| 26 | </div> | 57 | </div> |
| 27 | </div> | 58 | </div> |
| 28 | </template> | 59 | </template> |
| ... | @@ -32,9 +63,12 @@ import { ref, computed } from 'vue' | ... | @@ -32,9 +63,12 @@ import { ref, computed } from 'vue' |
| 32 | import { useLoad } from '@tarojs/taro' | 63 | import { useLoad } from '@tarojs/taro' |
| 33 | import NavHeader from '@/components/navigation/NavHeader.vue' | 64 | import NavHeader from '@/components/navigation/NavHeader.vue' |
| 34 | import SectionCard from '@/components/list/SectionCard.vue' | 65 | import SectionCard from '@/components/list/SectionCard.vue' |
| 35 | -import { fileListAPI } from '@/api/file' | 66 | +import ArticleCard from '@/components/cards/ArticleCard.vue' |
| 67 | +import { listAPI } from '@/api/article' | ||
| 68 | +import { mockArticleListAPI } from '@/utils/mockData' | ||
| 36 | import { useGo } from '@/hooks/useGo' | 69 | import { useGo } from '@/hooks/useGo' |
| 37 | import Taro from '@tarojs/taro' | 70 | import Taro from '@tarojs/taro' |
| 71 | +import { USE_MOCK_DATA } from '@/config/app' | ||
| 38 | 72 | ||
| 39 | const go = useGo() | 73 | const go = useGo() |
| 40 | 74 | ||
| ... | @@ -50,45 +84,23 @@ const data = ref({}) | ... | @@ -50,45 +84,23 @@ const data = ref({}) |
| 50 | const pageTitle = ref('分类列表') | 84 | const pageTitle = ref('分类列表') |
| 51 | 85 | ||
| 52 | /** | 86 | /** |
| 53 | - * 最大层级 | 87 | + * 过滤后的子分类列表 |
| 88 | + * @description 过滤掉 max_depth === 1 且 list 为空的项 | ||
| 54 | */ | 89 | */ |
| 55 | -const maxLevel = computed(() => data.value?.max_level || 0) | 90 | +const filteredChildren = computed(() => { |
| 56 | - | ||
| 57 | -/** | ||
| 58 | - * 将 API 返回的 children 数据转换为 SectionCard 需要的格式 | ||
| 59 | - * @description 将嵌套的 children 数组转换为 sections 格式 | ||
| 60 | - * | ||
| 61 | - * 数据结构: | ||
| 62 | - * - 第一层(大标题):level=1,如"入职前"、"入职中"、"入职后" | ||
| 63 | - * - 第二层(小标题):level=2,如"考试报名"、"资格考试报名入口" | ||
| 64 | - * | ||
| 65 | - * @returns {Array<{title: string, items: Array}>} | ||
| 66 | - */ | ||
| 67 | -const sections = computed(() => { | ||
| 68 | const children = data.value?.children || [] | 91 | const children = data.value?.children || [] |
| 69 | - | 92 | + return children.filter(item => { |
| 70 | - if (children.length === 0) { | 93 | + // 保留有子分类的项 |
| 71 | - return [] | 94 | + if (item.max_depth > 1) return true |
| 72 | - } | 95 | + // 保留有文章列表的项 |
| 73 | - | 96 | + if (item.max_depth === 1 && item.list?.length > 0) return true |
| 74 | - // 为每个第一层分类创建一个 section | 97 | + // 过滤掉空项 |
| 75 | - return children.map(level1Category => ({ | 98 | + return false |
| 76 | - title: level1Category.category_name, // 大标题:"入职前" | 99 | + }) |
| 77 | - items: (level1Category.children || []).map(level2Category => ({ | ||
| 78 | - id: level2Category.id, | ||
| 79 | - title: level2Category.category_name, // 小标题:"考试报名" | ||
| 80 | - subtitle: level2Category.list?.length ? `${level2Category.list.length} 个文件` : '', | ||
| 81 | - icon: level2Category.icon || '', | ||
| 82 | - level: level2Category.level, | ||
| 83 | - maxDepth: level2Category.max_depth, | ||
| 84 | - // 保留原始数据供点击事件使用 | ||
| 85 | - _raw: level2Category | ||
| 86 | - })) | ||
| 87 | - })) | ||
| 88 | }) | 100 | }) |
| 89 | 101 | ||
| 90 | /** | 102 | /** |
| 91 | - * 获取文档分类列表 | 103 | + * 获取文章分类列表 |
| 92 | * @param {Object} options - 页面参数 | 104 | * @param {Object} options - 页面参数 |
| 93 | * @param {string} options.cid - 分类ID(首次进入) | 105 | * @param {string} options.cid - 分类ID(首次进入) |
| 94 | * @param {string} options.id - 子分类ID(后续层级) | 106 | * @param {string} options.id - 子分类ID(后续层级) |
| ... | @@ -107,15 +119,16 @@ const fetchCategoryList = async (options) => { | ... | @@ -107,15 +119,16 @@ const fetchCategoryList = async (options) => { |
| 107 | } | 119 | } |
| 108 | 120 | ||
| 109 | console.log('[Category List] 请求参数:', params) | 121 | console.log('[Category List] 请求参数:', params) |
| 122 | + console.log('[Category List] 使用 Mock 数据:', USE_MOCK_DATA) | ||
| 110 | 123 | ||
| 111 | - // 调用接口(直接调用,不使用 fn() 包装) | 124 | + // 调用文章列表接口(支持分类结构) |
| 112 | - const res = await fileListAPI(params) | 125 | + const res = USE_MOCK_DATA |
| 126 | + ? await mockArticleListAPI(params) | ||
| 127 | + : await listAPI(params) | ||
| 113 | 128 | ||
| 114 | if (res.code === 1 && res.data) { | 129 | if (res.code === 1 && res.data) { |
| 115 | data.value = res.data | 130 | data.value = res.data |
| 116 | - // console.log('[Category List] 分类数据:', res.data) | 131 | + console.log('[Category List] 分类数据:', res.data) |
| 117 | - // console.log('[Category List] 最大层级:', maxLevel.value) | ||
| 118 | - // console.log('[Category List] 转换后的 sections:', JSON.stringify(sections.value, null, 2)) | ||
| 119 | } else { | 132 | } else { |
| 120 | Taro.showToast({ | 133 | Taro.showToast({ |
| 121 | title: res.msg || '获取分类列表失败', | 134 | title: res.msg || '获取分类列表失败', |
| ... | @@ -152,8 +165,8 @@ const handleItemClickWithNav = (item, go) => { | ... | @@ -152,8 +165,8 @@ const handleItemClickWithNav = (item, go) => { |
| 152 | console.log('[Category List] 分类层级:', item.level) | 165 | console.log('[Category List] 分类层级:', item.level) |
| 153 | console.log('[Category List] 最大深度:', item.maxDepth) | 166 | console.log('[Category List] 最大深度:', item.maxDepth) |
| 154 | 167 | ||
| 155 | - // 当前点击的是第二层(level=2),直接跳转到文档列表 | 168 | + // 当前点击的是第二层(level=2),直接跳转到文章列表 |
| 156 | - console.log('[Category List] 跳转到文档列表') | 169 | + console.log('[Category List] 跳转到文章列表') |
| 157 | go('/pages/material-list/index', { | 170 | go('/pages/material-list/index', { |
| 158 | id: item.id, | 171 | id: item.id, |
| 159 | title: item.title | 172 | title: item.title |
| ... | @@ -164,6 +177,33 @@ const handleItemClickWithNav = (item, go) => { | ... | @@ -164,6 +177,33 @@ const handleItemClickWithNav = (item, go) => { |
| 164 | const handleItemClick = (item) => handleItemClickWithNav(item, go) | 177 | const handleItemClick = (item) => handleItemClickWithNav(item, go) |
| 165 | 178 | ||
| 166 | /** | 179 | /** |
| 180 | + * 将子分类数组转换为 SectionCard items 格式 | ||
| 181 | + * @param {Array} children - 子分类数组 | ||
| 182 | + * @returns {Array} SectionCard items 格式 | ||
| 183 | + */ | ||
| 184 | +const convertToItems = (children) => { | ||
| 185 | + if (!children || children.length === 0) return [] | ||
| 186 | + return children.map(category => ({ | ||
| 187 | + id: category.id, | ||
| 188 | + title: category.category_name, | ||
| 189 | + subtitle: category.list?.length ? `${category.list.length} 篇文章` : '', | ||
| 190 | + icon: category.icon || '', | ||
| 191 | + level: category.level, | ||
| 192 | + maxDepth: category.max_depth, | ||
| 193 | + _raw: category | ||
| 194 | + })) | ||
| 195 | +} | ||
| 196 | + | ||
| 197 | +/** | ||
| 198 | + * 处理收藏状态变化 | ||
| 199 | + * @param {Object} payload - 收藏事件数据 | ||
| 200 | + */ | ||
| 201 | +const handleCollectChanged = (payload) => { | ||
| 202 | + console.log('[Category List] 收藏状态变化:', payload) | ||
| 203 | + // TODO: 更新本地状态或刷新列表 | ||
| 204 | +} | ||
| 205 | + | ||
| 206 | +/** | ||
| 167 | * 页面加载时接收参数并初始化 | 207 | * 页面加载时接收参数并初始化 |
| 168 | */ | 208 | */ |
| 169 | useLoad((options) => { | 209 | useLoad((options) => { | ... | ... |
-
Please register or login to post a comment