ImageGallery.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!--
  2. 通用图片展示(票 03):缩略图平铺 / 点击放大 / 加载失败占位 / 无图空态。
  3. 实体详情附件、设备详情设备图片、工单时间线的维修图共用这一份,各自不再写 <img>。
  4. 地址一律交 `resolveDeviceImageUrl`(唯一解析器):组件不拼前缀,也不拿原始串
  5. 给解析失败的项兜底 —— 解析器返回空就是「这一张不许展示」(见 domain/imageGallery.js)。
  6. 只有两档尺寸:`thumb` 列表里的小方块、`large` 详情里的整宽主图。
  7. -->
  8. <template>
  9. <div class="image-gallery">
  10. <p v-if="!entries.length" class="image-gallery__empty" data-image-gallery-empty>{{ emptyText }}</p>
  11. <template v-else>
  12. <ul class="image-gallery__list" :class="`is-${size}`" data-image-gallery>
  13. <li v-for="item in entries" :key="item.key" class="image-gallery__item" :data-image-state="stateOf(item)">
  14. <button v-if="isShown(item)" type="button" class="image-gallery__button" :aria-label="`查看${item.alt || item.name || `第 ${item.index + 1} 张图片`}`" @click="open(item)">
  15. <img :key="attempts[item.key] || 0" :src="item.src" :alt="item.alt || `图片 ${item.index + 1}`" loading="lazy" @load="settle(item.key, false)" @error="settle(item.key, true)" />
  16. <span v-if="hint" class="image-gallery__hint">{{ stateOf(item) === 'ready' ? hint : '图片加载中…' }}</span>
  17. </button>
  18. <!-- 解析不出或加载出错:如实说取不到,不画破图;只有加载出错才给重试(解析拒绝重试也没用)。 -->
  19. <span v-else-if="stateOf(item) === 'failed'" class="image-gallery__failed" role="status">
  20. 图片加载失败
  21. <button v-if="canRetry(item)" type="button" class="image-gallery__retry" @click="retry(item)">重试</button>
  22. </span>
  23. <span v-if="item.name" class="image-gallery__name">{{ item.name }}</span>
  24. </li>
  25. </ul>
  26. <ImagePreview :open="preview.open" :src="preview.src" :alt="preview.alt" :caption="preview.caption" @close="preview.open = false" />
  27. </template>
  28. </div>
  29. </template>
  30. <script setup>
  31. import { computed, ref } from 'vue'
  32. import { galleryItems, resolveGalleryItems } from '../domain/imageGallery.js'
  33. import { resolveDeviceImageUrl } from '../services/deviceImage.js'
  34. import ImagePreview from './ImagePreview.vue'
  35. const props = defineProps({
  36. /** 图片列表:地址字符串,或 `{ url, name, caption, alt }`。 */
  37. items: { type: Array, default: () => [] },
  38. /** 没有图时说的话(措辞由各分区给,组件不编业务文案)。 */
  39. emptyText: { type: String, default: '暂无图片' },
  40. /** `thumb` 小方块 / `large` 整宽主图。 */
  41. size: { type: String, default: 'thumb' },
  42. /** 主图上的提示文字(设备图片用「点击放大」;列表附图不传)。 */
  43. hint: { type: String, default: '' },
  44. })
  45. const entries = computed(() => resolveGalleryItems(galleryItems(props.items), resolveDeviceImageUrl))
  46. /**
  47. * 每一项的显示状态:
  48. * missing — 只有名字没有地址(改造前的附件就是这样:没图只写名字)
  49. * failed — 有地址,但解析器拒绝或图片加载出错
  50. * loading — 还在加载
  51. * ready — 可以点开看大图
  52. */
  53. const states = ref({})
  54. const attempts = ref({})
  55. function stateOf(item) {
  56. if (!item.url) return 'missing'
  57. if (!item.available) return 'failed'
  58. return states.value[item.key] || 'loading'
  59. }
  60. function isShown(item) {
  61. const state = stateOf(item)
  62. return state === 'loading' || state === 'ready'
  63. }
  64. function settle(key, failed) {
  65. const next = failed ? 'failed' : 'ready'
  66. if (states.value[key] === next) return
  67. states.value = { ...states.value, [key]: next }
  68. }
  69. /** 地址解析成功但图片加载出错:可以重试(换 key 让浏览器重新请求)。 */
  70. function canRetry(item) {
  71. return item.available && states.value[item.key] === 'failed'
  72. }
  73. function retry(item) {
  74. states.value = { ...states.value, [item.key]: 'loading' }
  75. attempts.value = { ...attempts.value, [item.key]: (attempts.value[item.key] || 0) + 1 }
  76. }
  77. const preview = ref({ open: false, src: '', alt: '', caption: '' })
  78. function open(item) {
  79. if (!item.src) return
  80. preview.value = { open: true, src: item.src, alt: item.alt || item.name || '图片', caption: item.caption }
  81. }
  82. </script>
  83. <style scoped>
  84. .image-gallery { min-width: 0; }
  85. .image-gallery__empty {
  86. margin: 0;
  87. color: var(--muted, #5680b0);
  88. font-size: calc(10.5 * var(--u, 1px));
  89. }
  90. .image-gallery__list {
  91. display: flex;
  92. flex-wrap: wrap;
  93. gap: calc(4 * var(--u, 1px));
  94. margin: 0;
  95. padding: 0;
  96. list-style: none;
  97. }
  98. /* 整宽主图:一列铺开,多张时按序往下排。 */
  99. .image-gallery__list.is-large { display: grid; grid-template-columns: 1fr; gap: 10px; }
  100. .image-gallery__item { display: grid; gap: 3px; min-width: 0; }
  101. .image-gallery__list.is-thumb .image-gallery__item { width: calc(44 * var(--u, 1px)); }
  102. .image-gallery__button {
  103. position: relative;
  104. display: block;
  105. width: 100%;
  106. padding: 0;
  107. overflow: hidden;
  108. border: 1px solid rgba(1, 171, 234, .3);
  109. border-radius: calc(3 * var(--u, 1px));
  110. background: rgba(1, 12, 32, .5);
  111. cursor: zoom-in;
  112. }
  113. .image-gallery__list.is-large .image-gallery__button {
  114. border-color: rgba(1, 171, 234, .25);
  115. border-radius: 6px;
  116. background: rgba(0, 20, 72, .4);
  117. transition: border-color .15s ease;
  118. }
  119. .image-gallery__list.is-large .image-gallery__button:hover,
  120. .image-gallery__list.is-large .image-gallery__button:focus-visible { border-color: rgba(53, 216, 255, .7); }
  121. .image-gallery__button img { display: block; width: 100%; height: 100%; object-fit: cover; }
  122. .image-gallery__list.is-thumb .image-gallery__button img { height: calc(44 * var(--u, 1px)); }
  123. .image-gallery__list.is-large .image-gallery__button img { height: auto; max-height: var(--image-gallery-max-height, 320px); }
  124. .image-gallery__hint {
  125. position: absolute;
  126. right: calc(4 * var(--u, 1px));
  127. bottom: calc(4 * var(--u, 1px));
  128. padding: 1px calc(5 * var(--u, 1px));
  129. border-radius: calc(3 * var(--u, 1px));
  130. background: rgba(0, 12, 32, .72);
  131. color: var(--text-2, #88b7e8);
  132. font-size: calc(10 * var(--u, 1px));
  133. pointer-events: none;
  134. }
  135. /* 失败占位:与缩略图同尺寸,虚线红框 + 一句话,不留空洞。 */
  136. .image-gallery__failed {
  137. display: grid;
  138. place-items: center;
  139. width: 100%;
  140. height: calc(44 * var(--u, 1px));
  141. padding: calc(2 * var(--u, 1px));
  142. border: 1px dashed rgba(255, 59, 82, .5);
  143. border-radius: calc(3 * var(--u, 1px));
  144. background: rgba(60, 12, 20, .32);
  145. color: var(--lv1, #ff3b52);
  146. font-size: calc(9.5 * var(--u, 1px));
  147. line-height: 1.3;
  148. text-align: center;
  149. }
  150. .image-gallery__list.is-large .image-gallery__failed { height: calc(120 * var(--u, 1px)); font-size: calc(11 * var(--u, 1px)); }
  151. .image-gallery__retry {
  152. justify-self: center;
  153. margin-top: calc(3 * var(--u, 1px));
  154. padding: calc(1 * var(--u, 1px)) calc(7 * var(--u, 1px));
  155. border: 1px solid rgba(255, 59, 82, .55);
  156. border-radius: calc(3 * var(--u, 1px));
  157. background: transparent;
  158. color: inherit;
  159. font: inherit;
  160. cursor: pointer;
  161. }
  162. .image-gallery__name {
  163. color: var(--cyan-hi, #00b8ff);
  164. font-size: calc(10.5 * var(--u, 1px));
  165. overflow-wrap: anywhere;
  166. }
  167. </style>