index.vue
9.81 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!--
* @Date: 2026-02-27
* @Description: 文章详情页
-->
<template>
<view class="article-detail-page">
<!-- 导航栏 -->
<NavHeader title="文章详情" />
<!-- 滚动容器 -->
<view class="flex-1 pb-safe">
<!-- 加载状态 - 自定义 Tailwind CSS 加载动画 -->
<view v-if="loading" class="flex items-center justify-center h-screen">
<view class="flex flex-col items-center">
<view class="w-[64rpx] h-[64rpx] border-4 border-gray-200 border-t-blue-600 rounded-full animate-spin mb-[24rpx]"></view>
<text class="text-gray-600 text-[28rpx]">加载中...</text>
</view>
</view>
<!-- 文章内容 -->
<view v-else-if="article" class="article-content">
<!-- 封面图 -->
<view v-if="article.coverUrl" class="cover-image-wrapper">
<image :src="article.coverUrl" mode="widthFix" class="cover-image" />
</view>
<!-- 文章信息 -->
<view class="article-info px-[32rpx]">
<!-- 标题 -->
<view class="article-title">{{ article.title }}</view>
<!-- 作者和日期 -->
<view class="article-meta">
<text v-if="article.authorName" class="meta-item">{{ article.authorName }}</text>
<text v-if="article.authorName && article.date" class="meta-separator">·</text>
<text v-if="article.date" class="meta-item">{{ formattedDate }}</text>
</view>
</view>
<!-- 分割线 -->
<view class="divider"></view>
<!-- 富文本内容 -->
<view class="article-body px-[32rpx]">
<rich-text :nodes="processedContent" class="rich-text-content" />
</view>
</view>
<!-- 加载失败 - 自定义空状态 -->
<view v-else-if="error" class="error-state">
<view class="flex flex-col items-center justify-center h-screen">
<view class="text-[#9CA3AF] text-[120rpx] mb-[24rpx]">⚠️</view>
<text class="text-gray-600 text-[28rpx] mb-[32rpx]">加载失败</text>
<view
class="px-[48rpx] py-[20rpx] bg-blue-600 text-white rounded-full"
@tap="fetchArticleDetail"
>
<text class="text-[28rpx]">重试</text>
</view>
</view>
</view>
<!-- 底部安全区域 -->
<view class="safe-area-bottom"></view>
</view>
<!-- 底部收藏按钮 -->
<view class="footer-bar">
<view class="footer-content">
<view
v-if="article"
:class="['collect-button', article.is_favorite ? 'collected' : '']"
@tap="toggleCollect"
>
<text class="collect-icon">{{ article.is_favorite ? '★' : '☆' }}</text>
<text class="collect-text">{{ article.is_favorite ? '已收藏' : '收藏' }}</text>
</view>
</view>
</view>
</view>
</template>
<script setup>
import { ref, computed } from 'vue'
import { useLoad } from '@tarojs/taro'
import NavHeader from '@/components/navigation/NavHeader.vue'
import { articleDetailAPI } from '@/api/article'
import { addAPI, delAPI } from '@/api/favorite'
import { mockArticleDetailAPI } from '@/utils/mockData'
import eventBus, { Events } from '@/utils/eventBus'
import dayjs from 'dayjs'
import Taro from '@tarojs/taro'
import { USE_MOCK_DATA } from '@/config/app'
/**
* 文章数据
*/
const article = ref(null)
/**
* 加载状态
*/
const loading = ref(true)
/**
* 错误状态
*/
const error = ref(false)
/**
* 文章 ID
*/
const articleId = ref(null)
/**
* 格式化日期显示
*/
const formattedDate = computed(() => {
if (!article.value?.post_date) return ''
try {
return dayjs(article.value.post_date).format('YYYY年MM月DD日')
} catch {
return article.value.post_date
}
})
/**
* 处理富文本内容,适配图片宽度
*/
const processedContent = computed(() => {
if (!article.value?.content) return ''
// 给 img 标签添加内联样式,确保宽度不超过容器
return article.value.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:12px auto;"')
})
/**
* 获取文章详情
*/
const fetchArticleDetail = async () => {
if (!articleId.value) return
loading.value = true
error.value = false
try {
console.log('[Article Detail] 获取文章详情:', articleId.value)
console.log('[Article Detail] 使用 Mock 数据:', USE_MOCK_DATA)
const res = USE_MOCK_DATA
? await mockArticleDetailAPI({ i: articleId.value })
: await articleDetailAPI({ i: articleId.value })
if (res.code === 1 && res.data) {
console.log('[Article Detail] 数据:', res.data)
article.value = {
id: res.data.id,
title: res.data.post_title || '未命名文章',
content: res.data.post_content || '',
excerpt: res.data.post_excerpt || '',
coverUrl: res.data.cover_url || res.data.post_thumbnail || '',
date: res.data.post_date || '',
authorName: res.data.author_name || '',
is_favorite: res.data.is_favorite === 1 || res.data.is_favorite === '1'
}
} else {
error.value = true
Taro.showToast({
title: res.msg || '获取文章详情失败',
icon: 'none'
})
}
} catch (err) {
console.error('[Article Detail] 获取文章详情失败:', err)
error.value = true
Taro.showToast({
title: '加载失败',
icon: 'error'
})
} finally {
loading.value = false
}
}
/**
* 切换收藏状态
*/
const toggleCollect = async () => {
if (!article.value) return
try {
const newCollectStatus = !article.value.is_favorite
// 调用收藏 API(使用与文件相同的收藏 API)
const res = newCollectStatus
? await addAPI({ meta_id: article.value.id })
: await delAPI({ meta_id: article.value.id })
if (res.code === 1) {
// 更新本地状态
article.value.is_favorite = newCollectStatus
Taro.showToast({
title: newCollectStatus ? '已收藏' : '已取消收藏',
icon: 'success',
duration: 1000
})
// 发送收藏更新事件
eventBus.emit(Events.FAVORITES_UPDATE, {
metaId: article.value.id,
collected: newCollectStatus,
timestamp: Date.now()
})
} else {
Taro.showToast({
title: res.msg || '操作失败',
icon: 'none',
duration: 2000
})
}
} catch (err) {
console.error('[Article Detail] 收藏操作失败:', err)
Taro.showToast({
title: '网络错误,请重试',
icon: 'none',
duration: 2000
})
}
}
/**
* 滚动到底部事件
*/
const onScrollToLower = () => {
// 可以在这里加载相关文章推荐等
console.log('[Article Detail] 滚动到底部')
}
/**
* 页面加载时获取文章详情
*/
useLoad((options) => {
console.log('[Article Detail] 页面参数:', options)
if (options.id) {
articleId.value = options.id
fetchArticleDetail()
} else {
error.value = true
loading.value = false
Taro.showToast({
title: '文章ID不存在',
icon: 'none'
})
}
})
</script>
<style lang="less">
.article-detail-page {
display: flex;
flex-direction: column;
min-height: 100vh;
background-color: #F9FAFB;
}
.cover-image-wrapper {
width: 100%;
background-color: #F3F4F6;
}
.cover-image {
width: 100%;
display: block;
}
.article-info {
padding-top: 32rpx;
padding-bottom: 24rpx;
}
.article-title {
font-size: 40rpx;
font-weight: bold;
color: #1F2937;
line-height: 1.4;
margin-bottom: 16rpx;
}
.article-meta {
display: flex;
align-items: center;
font-size: 24rpx;
color: #9CA3AF;
}
.meta-item {
margin-right: 8rpx;
}
.meta-separator {
margin-right: 8rpx;
}
.divider {
height: 1rpx;
background-color: #E5E7EB;
margin: 0 0 32rpx 0;
}
.article-body {
padding-bottom: 32rpx;
}
.rich-text-content {
font-size: 30rpx;
color: #374151;
line-height: 1.8;
word-wrap: break-word;
overflow: hidden;
/* 富文本图片样式:确保宽度适配移动端 */
:deep(img) {
max-width: 100% !important;
width: auto !important;
height: auto !important;
display: block !important;
margin: 24rpx auto !important;
border-radius: 12rpx;
}
/* 标题样式优化 */
:deep(h1),
:deep(h2),
:deep(h3),
:deep(h4),
:deep(h5),
:deep(h6) {
margin: 24rpx 0 16rpx;
font-weight: bold;
color: #1F2937;
}
:deep(h1) {
font-size: 40rpx;
}
:deep(h2) {
font-size: 36rpx;
}
:deep(h3) {
font-size: 32rpx;
}
/* 段落样式 */
:deep(p) {
margin: 16rpx 0;
}
/* 列表样式 */
:deep(ul),
:deep(ol) {
padding-left: 32rpx;
margin: 16rpx 0;
}
:deep(li) {
margin: 8rpx 0;
}
/* 引用样式 */
:deep(blockquote) {
padding: 16rpx 24rpx;
margin: 16rpx 0;
background-color: #F3F4F6;
border-left: 4rpx solid #D1D5DB;
color: #6B7280;
}
/* 链接样式 */
:deep(a) {
color: #2563EB;
text-decoration: underline;
}
}
.safe-area-bottom {
height: 120rpx;
}
.footer-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background-color: #fff;
border-top: 1rpx solid #E5E7EB;
padding-bottom: env(safe-area-inset-bottom);
}
.footer-content {
display: flex;
justify-content: center;
align-items: center;
padding: 20rpx 32rpx;
}
.collect-button {
display: flex;
align-items: center;
justify-content: center;
padding: 16rpx 48rpx;
border-radius: 999rpx;
background-color: #F3F4F6;
transition: all 0.2s ease;
&.collected {
background-color: #FEF3C7;
.collect-icon {
color: #F59E0B;
}
.collect-text {
color: #F59E0B;
}
}
}
.collect-icon {
font-size: 32rpx;
color: #9CA3AF;
margin-right: 8rpx;
}
.collect-text {
font-size: 28rpx;
color: #6B7280;
}
.error-state {
display: flex;
justify-content: center;
align-items: center;
min-height: 400rpx;
}
</style>