| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <!--
- 通用图片展示(票 03):缩略图平铺 / 点击放大 / 加载失败占位 / 无图空态。
- 实体详情附件、设备详情设备图片、工单时间线的维修图共用这一份,各自不再写 <img>。
- 地址一律交 `resolveDeviceImageUrl`(唯一解析器):组件不拼前缀,也不拿原始串
- 给解析失败的项兜底 —— 解析器返回空就是「这一张不许展示」(见 domain/imageGallery.js)。
- 只有两档尺寸:`thumb` 列表里的小方块、`large` 详情里的整宽主图。
- -->
- <template>
- <div class="image-gallery">
- <p v-if="!entries.length" class="image-gallery__empty" data-image-gallery-empty>{{ emptyText }}</p>
- <template v-else>
- <ul class="image-gallery__list" :class="`is-${size}`" data-image-gallery>
- <li v-for="item in entries" :key="item.key" class="image-gallery__item" :data-image-state="stateOf(item)">
- <button v-if="isShown(item)" type="button" class="image-gallery__button" :aria-label="`查看${item.alt || item.name || `第 ${item.index + 1} 张图片`}`" @click="open(item)">
- <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)" />
- <span v-if="hint" class="image-gallery__hint">{{ stateOf(item) === 'ready' ? hint : '图片加载中…' }}</span>
- </button>
- <!-- 解析不出或加载出错:如实说取不到,不画破图;只有加载出错才给重试(解析拒绝重试也没用)。 -->
- <span v-else-if="stateOf(item) === 'failed'" class="image-gallery__failed" role="status">
- 图片加载失败
- <button v-if="canRetry(item)" type="button" class="image-gallery__retry" @click="retry(item)">重试</button>
- </span>
- <span v-if="item.name" class="image-gallery__name">{{ item.name }}</span>
- </li>
- </ul>
- <ImagePreview :open="preview.open" :src="preview.src" :alt="preview.alt" :caption="preview.caption" @close="preview.open = false" />
- </template>
- </div>
- </template>
- <script setup>
- import { computed, ref } from 'vue'
- import { galleryItems, resolveGalleryItems } from '../domain/imageGallery.js'
- import { resolveDeviceImageUrl } from '../services/deviceImage.js'
- import ImagePreview from './ImagePreview.vue'
- const props = defineProps({
- /** 图片列表:地址字符串,或 `{ url, name, caption, alt }`。 */
- items: { type: Array, default: () => [] },
- /** 没有图时说的话(措辞由各分区给,组件不编业务文案)。 */
- emptyText: { type: String, default: '暂无图片' },
- /** `thumb` 小方块 / `large` 整宽主图。 */
- size: { type: String, default: 'thumb' },
- /** 主图上的提示文字(设备图片用「点击放大」;列表附图不传)。 */
- hint: { type: String, default: '' },
- })
- const entries = computed(() => resolveGalleryItems(galleryItems(props.items), resolveDeviceImageUrl))
- /**
- * 每一项的显示状态:
- * missing — 只有名字没有地址(改造前的附件就是这样:没图只写名字)
- * failed — 有地址,但解析器拒绝或图片加载出错
- * loading — 还在加载
- * ready — 可以点开看大图
- */
- const states = ref({})
- const attempts = ref({})
- function stateOf(item) {
- if (!item.url) return 'missing'
- if (!item.available) return 'failed'
- return states.value[item.key] || 'loading'
- }
- function isShown(item) {
- const state = stateOf(item)
- return state === 'loading' || state === 'ready'
- }
- function settle(key, failed) {
- const next = failed ? 'failed' : 'ready'
- if (states.value[key] === next) return
- states.value = { ...states.value, [key]: next }
- }
- /** 地址解析成功但图片加载出错:可以重试(换 key 让浏览器重新请求)。 */
- function canRetry(item) {
- return item.available && states.value[item.key] === 'failed'
- }
- function retry(item) {
- states.value = { ...states.value, [item.key]: 'loading' }
- attempts.value = { ...attempts.value, [item.key]: (attempts.value[item.key] || 0) + 1 }
- }
- const preview = ref({ open: false, src: '', alt: '', caption: '' })
- function open(item) {
- if (!item.src) return
- preview.value = { open: true, src: item.src, alt: item.alt || item.name || '图片', caption: item.caption }
- }
- </script>
- <style scoped>
- .image-gallery { min-width: 0; }
- .image-gallery__empty {
- margin: 0;
- color: var(--muted, #5680b0);
- font-size: calc(10.5 * var(--u, 1px));
- }
- .image-gallery__list {
- display: flex;
- flex-wrap: wrap;
- gap: calc(4 * var(--u, 1px));
- margin: 0;
- padding: 0;
- list-style: none;
- }
- /* 整宽主图:一列铺开,多张时按序往下排。 */
- .image-gallery__list.is-large { display: grid; grid-template-columns: 1fr; gap: 10px; }
- .image-gallery__item { display: grid; gap: 3px; min-width: 0; }
- .image-gallery__list.is-thumb .image-gallery__item { width: calc(44 * var(--u, 1px)); }
- .image-gallery__button {
- position: relative;
- display: block;
- width: 100%;
- padding: 0;
- overflow: hidden;
- border: 1px solid rgba(1, 171, 234, .3);
- border-radius: calc(3 * var(--u, 1px));
- background: rgba(1, 12, 32, .5);
- cursor: zoom-in;
- }
- .image-gallery__list.is-large .image-gallery__button {
- border-color: rgba(1, 171, 234, .25);
- border-radius: 6px;
- background: rgba(0, 20, 72, .4);
- transition: border-color .15s ease;
- }
- .image-gallery__list.is-large .image-gallery__button:hover,
- .image-gallery__list.is-large .image-gallery__button:focus-visible { border-color: rgba(53, 216, 255, .7); }
- .image-gallery__button img { display: block; width: 100%; height: 100%; object-fit: cover; }
- .image-gallery__list.is-thumb .image-gallery__button img { height: calc(44 * var(--u, 1px)); }
- .image-gallery__list.is-large .image-gallery__button img { height: auto; max-height: var(--image-gallery-max-height, 320px); }
- .image-gallery__hint {
- position: absolute;
- right: calc(4 * var(--u, 1px));
- bottom: calc(4 * var(--u, 1px));
- padding: 1px calc(5 * var(--u, 1px));
- border-radius: calc(3 * var(--u, 1px));
- background: rgba(0, 12, 32, .72);
- color: var(--text-2, #88b7e8);
- font-size: calc(10 * var(--u, 1px));
- pointer-events: none;
- }
- /* 失败占位:与缩略图同尺寸,虚线红框 + 一句话,不留空洞。 */
- .image-gallery__failed {
- display: grid;
- place-items: center;
- width: 100%;
- height: calc(44 * var(--u, 1px));
- padding: calc(2 * var(--u, 1px));
- border: 1px dashed rgba(255, 59, 82, .5);
- border-radius: calc(3 * var(--u, 1px));
- background: rgba(60, 12, 20, .32);
- color: var(--lv1, #ff3b52);
- font-size: calc(9.5 * var(--u, 1px));
- line-height: 1.3;
- text-align: center;
- }
- .image-gallery__list.is-large .image-gallery__failed { height: calc(120 * var(--u, 1px)); font-size: calc(11 * var(--u, 1px)); }
- .image-gallery__retry {
- justify-self: center;
- margin-top: calc(3 * var(--u, 1px));
- padding: calc(1 * var(--u, 1px)) calc(7 * var(--u, 1px));
- border: 1px solid rgba(255, 59, 82, .55);
- border-radius: calc(3 * var(--u, 1px));
- background: transparent;
- color: inherit;
- font: inherit;
- cursor: pointer;
- }
- .image-gallery__name {
- color: var(--cyan-hi, #00b8ff);
- font-size: calc(10.5 * var(--u, 1px));
- overflow-wrap: anywhere;
- }
- </style>
|