Volunteers.vue 8.06 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
<!--
 * @Date: 2025-01-01 15:20:00
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-11-12 16:00:48
 * @FilePath: /stdj_h5/src/views/Volunteers.vue
 * @Description: 义工页面 - 图片瀑布流展示
-->

<template>
  <div class="volunteers-container">
    <!-- 瀑布流内容 -->
    <div class="waterfall-content">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div class="waterfall-container">
          <div class="waterfall-column" v-for="(column, index) in columns" :key="index">
            <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 class="image-overlay">
                  <span class="image-title">{{ item.title }}</span>
                </div> -->
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useTitle } from '@vueuse/core';
import { useRouter } from 'vue-router'
import { showImagePreview } from 'vant'

// 导入接口
import { getImgStreamAPI } from '@/api/index.js'

useTitle('义工')

const router = useRouter()

const i = ref(router.currentRoute.value.query.i)

// 响应式数据
const loading = ref(false)
const finished = ref(false)
const currentPage = ref(0) // API使用0作为起始页码
const pageSize = 10
const allImages = ref([])
const columns = reactive([[], []])

// 加载数据
const onLoad = async () => {
  loading.value = true

  try {
    // 调用正式接口
    const { code, data } = await getImgStreamAPI({
      i: i.value,
      page: currentPage.value,
      limit: pageSize
    })

    if (code) {
      const newData = data.list.map(item => ({
        id: item.id,
        src: item.value,
        title: item.name,
        description: item.description,
        date: item.post_date,
        height: Math.floor(Math.random() * 200) + 200 // 随机高度用于瀑布流布局
      }))

      if (newData.length === 0) {
        finished.value = true
      } else {
        allImages.value.push(...newData)
        distributeImages(newData)
        currentPage.value++
      }
    } else {
      finished.value = true
    }
  } catch (error) {
    console.error('加载数据失败:', error)
    finished.value = true
  } finally {
    loading.value = false
  }
}

// 分配图片到两列
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)
    } else {
      columns[1].push(image)
    }
  })
}

/**
 * 显示图片预览(仅单张)
 * @param {Object} item - 当前点击的图片对象
 */
// 图片点击事件 - 仅预览当前单张图片
const onImageClick = (item) => {
    console.log('点击图片:', item)

    // 获取当前点击图片在所有图片中的索引
    const currentIndex = allImages.value.findIndex(img => img.id === item.id)

    // 提取所有图片的src用于预览
    const images = allImages.value.map(img => img.src)

    // 仅预览当前单张图片,禁用索引与循环
    showImagePreview({
        images: [images[currentIndex]],
        startPosition: 0,
        showIndex: false,
        loop: false,
        closeable: true
    })
}

// 图片加载成功
const onImageLoad = (event) => {
  // console.log('图片加载成功:', event.target.src)
}

// 图片加载失败
const onImageError = (event) => {
  console.error('图片加载失败:', event.target.src)
  // 可以设置默认图片
  event.target.src = 'https://via.placeholder.com/300x400?text=加载失败'
}

// 组件挂载时初始化
onMounted(() => {
  // 初始加载第一页数据
  onLoad()
})
</script>

<style scoped>
.volunteers-container {
  background-color: #F2EBDB;
  min-height: 100vh;
}

.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: #fff;
}

.waterfall-content {
  padding: 1rem;
}

.waterfall-container {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
}

.waterfall-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.waterfall-item {
  background-color: #fff;
  border-radius: 0.5rem;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  cursor: pointer;
}

.waterfall-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.image-wrapper {
  position: relative;
  overflow: hidden;
}

.image-wrapper img {
  width: 100%;
  display: block;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.waterfall-item:hover .image-wrapper img {
  transform: scale(1.05);
}

.image-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.7));
  padding: 1rem 0.75rem 0.75rem;
  transform: translateY(100%);
  transition: transform 0.3s ease;
}

.waterfall-item:hover .image-overlay {
  transform: translateY(0);
}

.image-title {
  color: #fff;
  font-size: 0.875rem;
  font-weight: 500;
  line-height: 1.4;
}

/* 加载状态样式 */
:deep(.van-list__loading) {
  padding: 1rem;
  text-align: center;
  color: #969799;
}

:deep(.van-list__finished-text) {
  padding: 1rem;
  text-align: center;
  color: #969799;
  font-size: 0.875rem;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .waterfall-content {
    padding: 0.75rem;
  }

  .waterfall-container {
    gap: 0.5rem;
  }

  .waterfall-column {
    gap: 0.5rem;
  }

  .image-title {
    font-size: 0.8125rem;
  }
}

/* 骨架屏效果 */
.waterfall-item.loading {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: loading 1.5s infinite;
}

@keyframes loading {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* 遮罩层样式 */
:deep(.van-overlay) {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.overlay-content {
  background-color: #fff;
  border-radius: 1rem;
  max-width: 90vw;
  max-height: 80vh;
  overflow: hidden;
  position: relative;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.close-btn {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 2.5rem;
  height: 2.5rem;
  background-color: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  transition: background-color 0.2s ease;
}

.close-btn:hover {
  background-color: rgba(0, 0, 0, 0.7);
}

.overlay-image-wrapper {
  width: 100%;
  max-height: 60vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
}

.overlay-image {
  width: 100%;
  height: auto;
  max-height: 60vh;
  object-fit: contain;
  display: block;
}

.overlay-description {
  padding: 1.5rem;
  background-color: #fff;
}

.overlay-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #333;
  margin: 0 0 1rem 0;
  line-height: 1.4;
}

.overlay-text {
  font-size: 1rem;
  color: #666;
  line-height: 1.6;
  margin: 0;
  white-space: pre-wrap;
}

/* 移动端适配 */
@media (max-width: 480px) {
  .overlay-content {
    max-width: 95vw;
    max-height: 85vh;
    border-radius: 0.75rem;
  }

  .close-btn {
    top: 0.75rem;
    right: 0.75rem;
    width: 2rem;
    height: 2rem;
  }

  .overlay-description {
    padding: 1rem;
  }

  .overlay-title {
    font-size: 1.125rem;
  }

  .overlay-text {
    font-size: 0.875rem;
  }
}
</style>