hookehuyr

refactor(PosterCheckin): 重构海报背景图管理和生成逻辑

优化海报背景图管理,改为按海报索引存储背景图
添加海报生成状态和配置哈希检查,避免重复生成
重构生成逻辑,仅在配置变化或未生成时重新生成海报
...@@ -85,7 +85,7 @@ ...@@ -85,7 +85,7 @@
85 class="flex-1 bg-gradient-to-r from-orange-400 to-orange-500 text-white py-3 px-6 rounded-lg font-medium shadow-lg active:scale-95 transition-transform duration-150 flex items-center justify-center gap-2" 85 class="flex-1 bg-gradient-to-r from-orange-400 to-orange-500 text-white py-3 px-6 rounded-lg font-medium shadow-lg active:scale-95 transition-transform duration-150 flex items-center justify-center gap-2"
86 @click="chooseBackgroundImage" 86 @click="chooseBackgroundImage"
87 > 87 >
88 - <text>{{ backgroundImage ? '更改照片' : '上传照片' }}</text> 88 + <text>{{ currentPosterHasCustomBackground ? '更改照片' : '上传照片' }}</text>
89 </view> 89 </view>
90 <view 90 <view
91 class="flex-1 text-white py-3 px-6 rounded-lg font-medium shadow-lg active:scale-95 transition-transform duration-150 flex items-center justify-center gap-2" 91 class="flex-1 text-white py-3 px-6 rounded-lg font-medium shadow-lg active:scale-95 transition-transform duration-150 flex items-center justify-center gap-2"
...@@ -151,10 +151,12 @@ const defaultBackground = 'https://cdn.ipadbiz.cn/lls_prog/images/%E6%B5%B7%E6%8 ...@@ -151,10 +151,12 @@ const defaultBackground = 'https://cdn.ipadbiz.cn/lls_prog/images/%E6%B5%B7%E6%8
151 151
152 // 页面状态 152 // 页面状态
153 const posterPath = ref('') // 生成的海报路径 153 const posterPath = ref('') // 生成的海报路径
154 -const backgroundImage = ref('') // 用户上传的背景图 154 +const backgroundImages = ref({}) // 每个海报的用户上传背景图 {posterIndex: imageUrl}
155 const shouldGeneratePoster = ref(false) // 是否应该生成海报 155 const shouldGeneratePoster = ref(false) // 是否应该生成海报
156 const currentPosterIndex = ref(0) // 当前显示的海报索引 156 const currentPosterIndex = ref(0) // 当前显示的海报索引
157 const posterGenerateFailed = ref(false) // 海报生成是否失败 157 const posterGenerateFailed = ref(false) // 海报生成是否失败
158 +const posterGeneratedFlags = ref({}) // 每个海报的生成状态 {posterIndex: boolean}
159 +const posterConfigHashes = ref({}) // 每个海报的配置哈希值,用于检测配置变化
158 160
159 // 页面参数 161 // 页面参数
160 const pageParams = ref({ 162 const pageParams = ref({
...@@ -282,10 +284,15 @@ const currentMockData = computed(() => { ...@@ -282,10 +284,15 @@ const currentMockData = computed(() => {
282 } 284 }
283 }) 285 })
284 286
287 +// 当前海报是否有用户上传的背景图
288 +const currentPosterHasCustomBackground = computed(() => {
289 + return !!backgroundImages.value[currentPosterIndex.value]
290 +})
291 +
285 // 海报配置 292 // 海报配置
286 const posterConfig = computed(() => { 293 const posterConfig = computed(() => {
287 const currentPosterData = posterList.value[currentPosterIndex.value] 294 const currentPosterData = posterList.value[currentPosterIndex.value]
288 - const bgImage = backgroundImage.value || currentPosterData?.backgroundImage || defaultBackground 295 + const bgImage = backgroundImages.value[currentPosterIndex.value] || currentPosterData?.backgroundImage || defaultBackground
289 296
290 return { 297 return {
291 width: 750, 298 width: 750,
...@@ -437,27 +444,76 @@ onMounted(() => { ...@@ -437,27 +444,76 @@ onMounted(() => {
437 444
438 console.log('海报打卡页面接收到的参数:', pageParams.value) 445 console.log('海报打卡页面接收到的参数:', pageParams.value)
439 446
440 - // 页面加载时生成当前海报 447 + // 页面加载时检查是否需要生成当前海报
441 - generateCurrentPoster() 448 + generateCurrentPosterIfNeeded()
442 }) 449 })
443 450
444 /** 451 /**
445 * 监听背景图变化,重新生成海报 452 * 监听背景图变化,重新生成海报
446 */ 453 */
447 -watch(backgroundImage, () => { 454 +watch(backgroundImages, (newVal, oldVal) => {
448 - if (backgroundImage.value) { 455 + // 只有当前海报的背景图发生变化时才重新生成
456 + const currentIndex = currentPosterIndex.value
457 + if (newVal[currentIndex] !== oldVal?.[currentIndex]) {
458 + // 标记当前海报需要重新生成
459 + posterGeneratedFlags.value[currentIndex] = false
449 generateCurrentPoster() 460 generateCurrentPoster()
450 } 461 }
451 -}) 462 +}, { deep: true })
452 463
453 /** 464 /**
454 * 监听当前海报索引变化,切换海报 465 * 监听当前海报索引变化,切换海报
455 */ 466 */
456 -watch(currentPosterIndex, () => { 467 +watch(currentPosterIndex, (newIndex, oldIndex) => {
457 - generateCurrentPoster() 468 + // 切换海报时,检查新海报是否已生成
469 + if (newIndex !== oldIndex) {
470 + generateCurrentPosterIfNeeded()
471 + }
458 }) 472 })
459 473
460 /** 474 /**
475 + * 生成当前海报配置的哈希值
476 + */
477 +const generateConfigHash = (config) => {
478 + const configStr = JSON.stringify({
479 + backgroundImage: backgroundImages.value[currentPosterIndex.value],
480 + posterIndex: currentPosterIndex.value,
481 + mockData: currentMockData.value
482 + })
483 + // 简单哈希函数
484 + let hash = 0
485 + for (let i = 0; i < configStr.length; i++) {
486 + const char = configStr.charCodeAt(i)
487 + hash = ((hash << 5) - hash) + char
488 + hash = hash & hash // 转换为32位整数
489 + }
490 + return hash.toString()
491 +}
492 +
493 +/**
494 + * 检查是否需要生成当前海报
495 + */
496 +const generateCurrentPosterIfNeeded = () => {
497 + const currentIndex = currentPosterIndex.value
498 + const currentHash = generateConfigHash()
499 + const isGenerated = posterGeneratedFlags.value[currentIndex]
500 + const lastHash = posterConfigHashes.value[currentIndex]
501 +
502 + // 如果海报未生成过,或者配置发生了变化,则需要重新生成
503 + if (!isGenerated || lastHash !== currentHash) {
504 + posterConfigHashes.value[currentIndex] = currentHash
505 + generateCurrentPoster()
506 + } else {
507 + // 海报已存在且配置未变化,直接使用缓存的海报
508 + const cachedPoster = posterList.value[currentIndex]
509 + if (cachedPoster && cachedPoster.path) {
510 + posterPath.value = cachedPoster.path
511 + posterGenerateFailed.value = false
512 + }
513 + }
514 +}
515 +
516 +/**
461 * 生成当前海报 517 * 生成当前海报
462 */ 518 */
463 const generateCurrentPoster = () => { 519 const generateCurrentPoster = () => {
...@@ -541,7 +597,8 @@ const uploadBackgroundImage = (filePath) => { ...@@ -541,7 +597,8 @@ const uploadBackgroundImage = (filePath) => {
541 Taro.hideLoading() 597 Taro.hideLoading()
542 const data = JSON.parse(uploadRes.data) 598 const data = JSON.parse(uploadRes.data)
543 if (data.code === 0 && data.data) { 599 if (data.code === 0 && data.data) {
544 - backgroundImage.value = data.data.src 600 + // 为当前海报设置背景图
601 + backgroundImages.value[currentPosterIndex.value] = data.data.src
545 Taro.showToast({ title: '上传成功', icon: 'success' }) 602 Taro.showToast({ title: '上传成功', icon: 'success' })
546 } else { 603 } else {
547 // 检查是否为审核不通过 604 // 检查是否为审核不通过
...@@ -571,12 +628,16 @@ const uploadBackgroundImage = (filePath) => { ...@@ -571,12 +628,16 @@ const uploadBackgroundImage = (filePath) => {
571 * 海报生成成功 628 * 海报生成成功
572 */ 629 */
573 const onPosterSuccess = (result) => { 630 const onPosterSuccess = (result) => {
631 + const currentIndex = currentPosterIndex.value
574 posterPath.value = result.tempFilePath 632 posterPath.value = result.tempFilePath
575 posterGenerateFailed.value = false 633 posterGenerateFailed.value = false
576 - // 更新当前海报的路径 634 +
577 - if (posterList.value[currentPosterIndex.value]) { 635 + // 更新当前海报的路径和生成状态
578 - posterList.value[currentPosterIndex.value].path = result.tempFilePath 636 + if (posterList.value[currentIndex]) {
637 + posterList.value[currentIndex].path = result.tempFilePath
579 } 638 }
639 + posterGeneratedFlags.value[currentIndex] = true
640 +
580 shouldGeneratePoster.value = false 641 shouldGeneratePoster.value = false
581 Taro.showToast({ title: '海报生成成功', icon: 'success' }) 642 Taro.showToast({ title: '海报生成成功', icon: 'success' })
582 } 643 }
...@@ -585,9 +646,14 @@ const onPosterSuccess = (result) => { ...@@ -585,9 +646,14 @@ const onPosterSuccess = (result) => {
585 * 海报生成失败 646 * 海报生成失败
586 */ 647 */
587 const onPosterFail = (error) => { 648 const onPosterFail = (error) => {
649 + const currentIndex = currentPosterIndex.value
588 shouldGeneratePoster.value = false 650 shouldGeneratePoster.value = false
589 posterGenerateFailed.value = true 651 posterGenerateFailed.value = true
590 posterPath.value = '' 652 posterPath.value = ''
653 +
654 + // 标记当前海报生成失败,下次仍需重新生成
655 + posterGeneratedFlags.value[currentIndex] = false
656 +
591 Taro.showToast({ title: '海报生成失败', icon: 'none' }) 657 Taro.showToast({ title: '海报生成失败', icon: 'none' })
592 console.error('海报生成失败:', error) 658 console.error('海报生成失败:', error)
593 } 659 }
......