YinLiShiDetail.vue
5.88 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
<template>
<div class="yinlishi-detail-container">
<section class="single-list">
<div class="item-card">
<img :src="articleItem.src" :alt="articleItem.name || '引礼师详情'" class="item-image" />
<div class="item-content">
<div class="item-role">{{ articleItem.role }}</div>
<div class="item-name" v-html="formatNameWithSuperscript(articleItem.name)"></div>
<div class="item-desc" v-html="articleItem.desc"></div>
</div>
</div>
</section>
<div class="waterfall-content" v-if="columns[0].length || columns[1].length">
<div class="waterfall-container">
<div class="waterfall-column" v-for="(column, cidx) in columns" :key="cidx">
<div
class="waterfall-item"
v-for="item in column"
:key="item.id"
@click="onImageClick(item)"
>
<div class="image-wrapper">
<img
:src="item.src"
:alt="item.title"
:style="{ height: item.height + 'px' }"
@load="onImageLoad"
@error="onImageError"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, reactive } from 'vue'
import { useTitle } from '@vueuse/core'
import { useRoute } from 'vue-router'
import { showImagePreview } from 'vant'
import { formatNameWithSuperscript } from '@utils/text'
import { getArticleDetailAPI } from '@/api/index.js'
useTitle('引礼师详情')
const route = useRoute()
const articleItem = ref({})
const columns = reactive([[], []])
const allImages = ref([])
/**
* 分配图片到两列
* @param {Array} images 新增图片列表
* @returns {void}
*/
const distributeImages = (images) => {
images.forEach((image) => {
const leftHeight = columns[0].reduce((sum, item) => sum + item.height + 20, 0)
const rightHeight = columns[1].reduce((sum, item) => sum + item.height + 20, 0)
if (leftHeight <= rightHeight) {
columns[0].push(image)
return
}
columns[1].push(image)
})
}
/**
* 初始化瀑布流图片数据
* @param {Array} imgs 原始图片列表
* @returns {void}
*/
const initWaterfallImages = (imgs) => {
columns[0].splice(0, columns[0].length)
columns[1].splice(0, columns[1].length)
const list = Array.isArray(imgs) ? imgs : []
const mapped = list.map((item, idx) => {
const src = typeof item === 'string' ? item : (item?.value || item?.src || '')
return {
id: typeof item === 'object' && item?.id ? item.id : idx + 1,
src: src + ((item?.size || 0) > 20 * 1024 * 1024 ? '' : '?imageMogr2/thumbnail/400x/strip/quality/70'),
title: typeof item === 'object' && item?.name ? item.name : (`图片${idx + 1}`),
height: Math.floor(Math.random() * 200) + 200
}
}).filter((item) => String(item.src || '').trim().length > 0)
allImages.value = mapped
distributeImages(mapped)
}
/**
* 加载文章详情
* @returns {Promise<void>}
*/
const loadArticleDetail = async () => {
try {
const articleId = route.params.id
const { code, data } = await getArticleDetailAPI({ i: articleId })
if (!code) {
return
}
articleItem.value = {
id: data.id,
role: data.post_excerpt,
name: data.post_title,
src: `${data?.file_list?.photo?.value || ''}?imageMogr2/thumbnail/400x/strip/quality/70`,
desc: data.post_content,
imgs: data?.file_list?.img || []
}
initWaterfallImages(articleItem.value.imgs)
} catch (error) {
console.error('加载引礼师详情失败:', error)
}
}
/**
* 图片加载完成回调
* @returns {void}
*/
const onImageLoad = () => {
// 预留:如需按图片实际宽高调整瀑布流高度,可在这里扩展
}
/**
* 图片加载失败回调
* @param {Event} evt 图片加载事件
* @returns {void}
*/
const onImageError = (evt) => {
console.warn(`图片加载失败:${evt.target.src}`)
}
/**
* 点击预览大图
* @param {object} item 当前图片项
* @returns {void}
*/
const onImageClick = (item) => {
const currentIndex = allImages.value.findIndex((img) => img.id === item.id)
const images = allImages.value.map((img) => img.src.replace('?imageMogr2/thumbnail/400x/strip/quality/70', ''))
showImagePreview({
images,
startPosition: currentIndex >= 0 ? currentIndex : 0,
showIndex: true,
closeable: true
})
}
onMounted(() => {
loadArticleDetail()
})
</script>
<style lang="less" scoped>
.yinlishi-detail-container {
min-height: 100vh;
width: 100%;
padding: 1.5rem;
box-sizing: border-box;
background: #F2EBDB;
.waterfall-content {
margin-top: 1rem;
}
.waterfall-container {
display: flex;
gap: 0.5rem;
align-items: flex-start;
}
.waterfall-column {
flex: 1;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.waterfall-item {
.image-wrapper {
width: 100%;
overflow: hidden;
border-radius: 0.5rem;
background: #FFFFFF;
box-shadow: inset 0 0 0 0.0625rem rgba(0, 0, 0, 0.08);
img {
width: 100%;
display: block;
object-fit: cover;
}
}
}
}
.single-list {
display: flex;
flex-direction: column;
gap: 1rem;
margin-bottom: 1rem;
}
.item-card {
overflow: hidden;
background: #FFFFFF;
border-radius: 0.75rem;
box-shadow: 0 0.25rem 1rem rgba(107, 65, 2, 0.08);
}
.item-image {
width: 100%;
display: block;
}
.item-content {
padding: 1rem;
}
.item-role {
color: #6B4102;
font-size: 0.875rem;
}
.item-name {
margin-top: 0.5rem;
color: #3D2A0B;
font-size: 1.75rem;
font-weight: 600;
}
.item-desc {
margin-top: 0.75rem;
color: #4F4F4F;
line-height: 1.7;
font-size: 0.9375rem;
}
@media (max-width: 30rem) {
.yinlishi-detail-container {
padding: 0.75rem;
}
.item-content {
padding: 0.875rem;
}
.item-name {
font-size: 1.5rem;
}
}
</style>