feat: C端展示区改为单张居中无缝轮播并接入PhotoSwipe图片视频查看器,轮播间隔支持后台配置
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -307,6 +307,8 @@
|
||||
"appName": "Platform Name",
|
||||
"logRetentionDays": "Log Retention (days)",
|
||||
"watermarkText": "Certificate Watermark",
|
||||
"carouselInterval": "H5 Carousel Interval (s)",
|
||||
"carouselIntervalHint": "Seconds each media stays; decimals allowed. 0 disables auto-advance (manual loop still works).",
|
||||
"saved": "Settings saved"
|
||||
},
|
||||
"profile": {
|
||||
|
||||
@@ -307,6 +307,8 @@
|
||||
"appName": "平台名称",
|
||||
"logRetentionDays": "日志保留天数",
|
||||
"watermarkText": "证书水印文案",
|
||||
"carouselInterval": "C 端轮播间隔(秒)",
|
||||
"carouselIntervalHint": "每张媒体停留秒数,支持小数;填 0 则不自动切换(仍可手动循环切换)",
|
||||
"saved": "设置已保存"
|
||||
},
|
||||
"profile": {
|
||||
|
||||
@@ -11,6 +11,19 @@
|
||||
<n-form-item :label="t('pages.settings.watermarkText')">
|
||||
<n-input v-model:value="form.watermarkText" />
|
||||
</n-form-item>
|
||||
<n-form-item :label="t('pages.settings.carouselInterval')">
|
||||
<div style="display: flex; flex-direction: column; gap: 4px">
|
||||
<n-input-number
|
||||
v-model:value="form.carouselInterval"
|
||||
:min="0"
|
||||
:max="30"
|
||||
:step="0.5"
|
||||
:precision="1"
|
||||
style="width: 220px"
|
||||
/>
|
||||
<span style="font-size: 12px; color: #999">{{ t('pages.settings.carouselIntervalHint') }}</span>
|
||||
</div>
|
||||
</n-form-item>
|
||||
<n-button type="primary" :loading="saving" @click="save">{{ t('common.action.save') }}</n-button>
|
||||
</n-form>
|
||||
</n-card>
|
||||
@@ -25,10 +38,16 @@ import { message } from '@/utils/feedback';
|
||||
|
||||
const { t } = useI18n();
|
||||
const saving = ref(false);
|
||||
const form = reactive<{ appName: string; logRetentionDays: number; watermarkText: string }>({
|
||||
const form = reactive<{
|
||||
appName: string;
|
||||
logRetentionDays: number;
|
||||
watermarkText: string;
|
||||
carouselInterval: number;
|
||||
}>({
|
||||
appName: '',
|
||||
logRetentionDays: 365,
|
||||
watermarkText: '',
|
||||
carouselInterval: 3,
|
||||
});
|
||||
|
||||
async function load() {
|
||||
@@ -36,6 +55,7 @@ async function load() {
|
||||
form.appName = s.appName || '';
|
||||
form.logRetentionDays = Number(s.logRetentionDays || 365);
|
||||
form.watermarkText = s.watermarkText || '';
|
||||
form.carouselInterval = Number(s.carouselInterval ?? 3);
|
||||
}
|
||||
async function save() {
|
||||
saving.value = true;
|
||||
@@ -44,6 +64,7 @@ async function save() {
|
||||
appName: form.appName,
|
||||
logRetentionDays: String(form.logRetentionDays),
|
||||
watermarkText: form.watermarkText,
|
||||
carouselInterval: String(form.carouselInterval),
|
||||
});
|
||||
message.success(t('pages.settings.saved'));
|
||||
} finally {
|
||||
|
||||
@@ -22,7 +22,7 @@ export class SettingsController {
|
||||
@ApiOperation({ summary: '获取公开设置(应用名等)' })
|
||||
async getPublic() {
|
||||
const all = await this.settingsService.getAll();
|
||||
return { appName: all.appName };
|
||||
return { appName: all.appName, carouselInterval: all.carouselInterval };
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
const EDITABLE_KEYS = ['appName', 'logRetentionDays', 'watermarkText'];
|
||||
const EDITABLE_KEYS = ['appName', 'logRetentionDays', 'watermarkText', 'carouselInterval'];
|
||||
|
||||
@Injectable()
|
||||
export class SettingsService {
|
||||
@@ -17,6 +17,8 @@ export class SettingsService {
|
||||
appName: this.config.get<string>('appName') || '',
|
||||
logRetentionDays: String(this.config.get<number>('logRetentionDays') || 365),
|
||||
watermarkText: this.config.get<string>('watermarkText') || '仅供溯源核验使用',
|
||||
// C 端顶部媒体轮播:单张停留秒数(支持小数),0 表示不自动切换
|
||||
carouselInterval: '3',
|
||||
};
|
||||
for (const r of rows) map[r.key] = r.value;
|
||||
return map;
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
"dependencies": {
|
||||
"axios": "^1.7.9",
|
||||
"dayjs": "^1.11.13",
|
||||
"photoswipe": "^5.4.4",
|
||||
"vue": "^3.5.13",
|
||||
"vue-i18n": "^9.14.2",
|
||||
"vue-router": "^4.5.0"
|
||||
|
||||
@@ -1,355 +0,0 @@
|
||||
<template>
|
||||
<transition name="viewer-fade">
|
||||
<div v-if="show" class="viewer" @click.self="onBackdrop">
|
||||
<!-- 顶部工具栏 -->
|
||||
<div class="bar">
|
||||
<span class="counter" v-if="items.length > 1">{{ current + 1 }} / {{ items.length }}</span>
|
||||
<span class="spacer" />
|
||||
<template v-if="!curIsVideo">
|
||||
<button class="tool" :aria-label="t('viewer.zoomOut')" @click="zoomOut">−</button>
|
||||
<button class="tool" :aria-label="t('viewer.reset')" @click="resetTransform">⟲</button>
|
||||
<button class="tool" :aria-label="t('viewer.zoomIn')" @click="zoomIn">+</button>
|
||||
</template>
|
||||
<button class="tool close" :aria-label="t('viewer.close')" @click="close">×</button>
|
||||
</div>
|
||||
|
||||
<!-- 左右切换 -->
|
||||
<button v-if="items.length > 1" class="nav prev" :aria-label="t('viewer.prev')" @click.stop="prev">‹</button>
|
||||
<button v-if="items.length > 1" class="nav next" :aria-label="t('viewer.next')" @click.stop="next">›</button>
|
||||
|
||||
<!-- 媒体区 -->
|
||||
<div ref="stageEl" class="stage">
|
||||
<video
|
||||
v-if="curIsVideo"
|
||||
:key="cur?.url"
|
||||
class="media-video"
|
||||
:src="cur?.url"
|
||||
controls
|
||||
autoplay
|
||||
playsinline
|
||||
@click.stop
|
||||
/>
|
||||
<img
|
||||
v-else
|
||||
ref="imgEl"
|
||||
class="media-img"
|
||||
:src="cur?.url"
|
||||
:alt="cur?.name"
|
||||
:style="imgStyle"
|
||||
draggable="false"
|
||||
@touchstart="onTouchStart"
|
||||
@touchmove.prevent="onTouchMove"
|
||||
@touchend="onTouchEnd"
|
||||
@wheel.prevent="onWheel"
|
||||
@mousedown.prevent="onMouseDown"
|
||||
@dblclick="onDblClick"
|
||||
@click.stop
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- 图片标题 -->
|
||||
<div v-if="!curIsVideo && cur?.name" class="caption">{{ cur.name }}</div>
|
||||
</div>
|
||||
</transition>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
||||
interface MediaItem {
|
||||
url?: string;
|
||||
name?: string;
|
||||
type?: string;
|
||||
}
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{ show: boolean; items: MediaItem[]; start?: number }>(),
|
||||
{ start: 0 },
|
||||
);
|
||||
const emit = defineEmits<{ (e: 'update:show', v: boolean): void }>();
|
||||
const { t } = useI18n();
|
||||
|
||||
const current = ref(0);
|
||||
const scale = ref(1);
|
||||
const tx = ref(0);
|
||||
const ty = ref(0);
|
||||
const imgEl = ref<HTMLImageElement | null>(null);
|
||||
const stageEl = ref<HTMLElement | null>(null);
|
||||
|
||||
const cur = computed(() => props.items[current.value]);
|
||||
const curIsVideo = computed(() => isVideo(cur.value));
|
||||
const imgStyle = computed(() => ({
|
||||
transform: `translate(${tx.value}px, ${ty.value}px) scale(${scale.value})`,
|
||||
transition: gesturing || dragging ? 'none' : 'transform 0.18s ease',
|
||||
cursor: scale.value > 1 ? (dragging ? 'grabbing' : 'grab') : 'zoom-in',
|
||||
}));
|
||||
|
||||
function isVideo(m?: MediaItem) {
|
||||
return !!m && (m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(m.url || ''));
|
||||
}
|
||||
|
||||
function resetTransform() {
|
||||
scale.value = 1;
|
||||
tx.value = 0;
|
||||
ty.value = 0;
|
||||
}
|
||||
function close() {
|
||||
emit('update:show', false);
|
||||
}
|
||||
function onBackdrop() {
|
||||
if (scale.value === 1) close();
|
||||
else resetTransform();
|
||||
}
|
||||
function prev() {
|
||||
current.value = (current.value - 1 + props.items.length) % props.items.length;
|
||||
resetTransform();
|
||||
}
|
||||
function next() {
|
||||
current.value = (current.value + 1) % props.items.length;
|
||||
resetTransform();
|
||||
}
|
||||
|
||||
function clampScale(s: number) {
|
||||
return Math.min(6, Math.max(1, s));
|
||||
}
|
||||
// 按"放大后图片实际溢出可视区"的尺寸约束平移,使拖动恰好能到达图片边缘
|
||||
function clampOffset() {
|
||||
const stage = stageEl.value;
|
||||
const img = imgEl.value;
|
||||
const vw = stage?.clientWidth ?? window.innerWidth;
|
||||
const vh = stage?.clientHeight ?? window.innerHeight;
|
||||
const baseW = img?.clientWidth ?? vw;
|
||||
const baseH = img?.clientHeight ?? vh;
|
||||
const maxX = Math.max(0, (baseW * scale.value - vw) / 2);
|
||||
const maxY = Math.max(0, (baseH * scale.value - vh) / 2);
|
||||
tx.value = Math.max(-maxX, Math.min(maxX, tx.value));
|
||||
ty.value = Math.max(-maxY, Math.min(maxY, ty.value));
|
||||
}
|
||||
function zoomIn() {
|
||||
scale.value = clampScale(scale.value * 1.4);
|
||||
clampOffset();
|
||||
}
|
||||
function zoomOut() {
|
||||
scale.value = clampScale(scale.value / 1.4);
|
||||
if (scale.value === 1) {
|
||||
tx.value = 0;
|
||||
ty.value = 0;
|
||||
} else {
|
||||
clampOffset();
|
||||
}
|
||||
}
|
||||
function onWheel(e: WheelEvent) {
|
||||
scale.value = clampScale(scale.value * (e.deltaY < 0 ? 1.15 : 0.87));
|
||||
if (scale.value === 1) {
|
||||
tx.value = 0;
|
||||
ty.value = 0;
|
||||
} else {
|
||||
clampOffset();
|
||||
}
|
||||
}
|
||||
function onDblClick() {
|
||||
if (scale.value > 1) resetTransform();
|
||||
else scale.value = 2.5;
|
||||
}
|
||||
|
||||
/* 触摸:单指平移、双指缩放 */
|
||||
let gesturing = false;
|
||||
let mode: 'none' | 'pan' | 'pinch' = 'none';
|
||||
let startDist = 0;
|
||||
let startScale = 1;
|
||||
let panX = 0;
|
||||
let panY = 0;
|
||||
|
||||
function dist(a: Touch, b: Touch) {
|
||||
return Math.hypot(a.clientX - b.clientX, a.clientY - b.clientY);
|
||||
}
|
||||
function onTouchStart(e: TouchEvent) {
|
||||
gesturing = true;
|
||||
if (e.touches.length >= 2) {
|
||||
mode = 'pinch';
|
||||
startDist = dist(e.touches[0], e.touches[1]);
|
||||
startScale = scale.value;
|
||||
} else if (e.touches.length === 1) {
|
||||
mode = 'pan';
|
||||
panX = e.touches[0].clientX - tx.value;
|
||||
panY = e.touches[0].clientY - ty.value;
|
||||
}
|
||||
}
|
||||
function onTouchMove(e: TouchEvent) {
|
||||
if (mode === 'pinch' && e.touches.length >= 2) {
|
||||
const d = dist(e.touches[0], e.touches[1]);
|
||||
scale.value = clampScale(startScale * (d / (startDist || 1)));
|
||||
if (scale.value === 1) {
|
||||
tx.value = 0;
|
||||
ty.value = 0;
|
||||
} else {
|
||||
clampOffset();
|
||||
}
|
||||
} else if (mode === 'pan' && e.touches.length === 1 && scale.value > 1) {
|
||||
tx.value = e.touches[0].clientX - panX;
|
||||
ty.value = e.touches[0].clientY - panY;
|
||||
clampOffset();
|
||||
}
|
||||
}
|
||||
function onTouchEnd(e: TouchEvent) {
|
||||
if (e.touches.length === 0) {
|
||||
mode = 'none';
|
||||
gesturing = false;
|
||||
}
|
||||
}
|
||||
|
||||
/* 鼠标拖动(桌面端):放大后可拖动查看四周细节 */
|
||||
let dragging = false;
|
||||
let dragStartX = 0;
|
||||
let dragStartY = 0;
|
||||
function onMouseDown(e: MouseEvent) {
|
||||
if (scale.value <= 1) return; // 未放大不拖动
|
||||
dragging = true;
|
||||
dragStartX = e.clientX - tx.value;
|
||||
dragStartY = e.clientY - ty.value;
|
||||
window.addEventListener('mousemove', onMouseMove);
|
||||
window.addEventListener('mouseup', onMouseUp);
|
||||
}
|
||||
function onMouseMove(e: MouseEvent) {
|
||||
if (!dragging) return;
|
||||
tx.value = e.clientX - dragStartX;
|
||||
ty.value = e.clientY - dragStartY;
|
||||
clampOffset();
|
||||
}
|
||||
function onMouseUp() {
|
||||
dragging = false;
|
||||
window.removeEventListener('mousemove', onMouseMove);
|
||||
window.removeEventListener('mouseup', onMouseUp);
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.show,
|
||||
(v) => {
|
||||
if (v) {
|
||||
current.value = Math.min(props.start, Math.max(0, props.items.length - 1));
|
||||
resetTransform();
|
||||
document.body.style.overflow = 'hidden';
|
||||
} else {
|
||||
document.body.style.overflow = '';
|
||||
}
|
||||
},
|
||||
);
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.viewer {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 100;
|
||||
background: rgba(0, 0, 0, 0.94);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
overflow: hidden;
|
||||
touch-action: none;
|
||||
}
|
||||
.bar {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 52px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 0 12px;
|
||||
z-index: 2;
|
||||
}
|
||||
.spacer {
|
||||
flex: 1;
|
||||
}
|
||||
.counter {
|
||||
color: #fff;
|
||||
font-size: 13px;
|
||||
opacity: 0.9;
|
||||
}
|
||||
.tool {
|
||||
width: 38px;
|
||||
height: 38px;
|
||||
border-radius: 50%;
|
||||
border: none;
|
||||
background: rgba(255, 255, 255, 0.16);
|
||||
color: #fff;
|
||||
font-size: 20px;
|
||||
line-height: 38px;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
}
|
||||
.tool:active {
|
||||
background: rgba(255, 255, 255, 0.32);
|
||||
}
|
||||
.tool.close {
|
||||
font-size: 24px;
|
||||
}
|
||||
.nav {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
width: 42px;
|
||||
height: 64px;
|
||||
border: none;
|
||||
background: rgba(255, 255, 255, 0.12);
|
||||
color: #fff;
|
||||
font-size: 30px;
|
||||
cursor: pointer;
|
||||
z-index: 2;
|
||||
}
|
||||
.nav.prev {
|
||||
left: 0;
|
||||
border-radius: 0 8px 8px 0;
|
||||
}
|
||||
.nav.next {
|
||||
right: 0;
|
||||
border-radius: 8px 0 0 8px;
|
||||
}
|
||||
.stage {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
overflow: hidden;
|
||||
}
|
||||
.media-img {
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
object-fit: contain;
|
||||
will-change: transform;
|
||||
user-select: none;
|
||||
-webkit-user-drag: none;
|
||||
}
|
||||
.media-video {
|
||||
max-width: 100%;
|
||||
max-height: 86%;
|
||||
background: #000;
|
||||
}
|
||||
.caption {
|
||||
position: absolute;
|
||||
bottom: 18px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
max-width: 86%;
|
||||
padding: 6px 14px;
|
||||
border-radius: 16px;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
color: #fff;
|
||||
font-size: 13px;
|
||||
text-align: center;
|
||||
z-index: 2;
|
||||
}
|
||||
.viewer-fade-enter-active,
|
||||
.viewer-fade-leave-active {
|
||||
transition: opacity 0.2s ease;
|
||||
}
|
||||
.viewer-fade-enter-from,
|
||||
.viewer-fade-leave-to {
|
||||
opacity: 0;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,99 @@
|
||||
import PhotoSwipeLightbox from 'photoswipe/lightbox';
|
||||
import PhotoSwipe from 'photoswipe';
|
||||
import 'photoswipe/style.css';
|
||||
|
||||
export interface MediaItem {
|
||||
url?: string;
|
||||
name?: string;
|
||||
type?: string;
|
||||
}
|
||||
|
||||
function isVideo(m: MediaItem) {
|
||||
return !!m && (m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(m.url || ''));
|
||||
}
|
||||
|
||||
// 缓存图片真实尺寸,避免重复加载;PhotoSwipe 需要宽高才能正确缩放/居中
|
||||
const sizeCache = new Map<string, { w: number; h: number }>();
|
||||
function getImageSize(url: string): Promise<{ w: number; h: number }> {
|
||||
const cached = sizeCache.get(url);
|
||||
if (cached) return Promise.resolve(cached);
|
||||
return new Promise((resolve) => {
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
const dim = { w: img.naturalWidth || 1600, h: img.naturalHeight || 1200 };
|
||||
sizeCache.set(url, dim);
|
||||
resolve(dim);
|
||||
};
|
||||
img.onerror = () => resolve({ w: 1600, h: 1200 });
|
||||
img.src = url;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开图片 / 视频详情查看器(基于 PhotoSwipe v5)。
|
||||
* - 图片:支持双指/滚轮/双击缩放、拖动、左右滑动切换、下滑关闭。
|
||||
* - 视频:渲染为可控制的 <video> 播放,切到其它张或关闭时自动暂停。
|
||||
* - 循环:到最后一张的下一张回到第一张。
|
||||
*/
|
||||
export async function openMediaViewer(items: MediaItem[], index = 0) {
|
||||
const list = (items || []).filter((m) => m && m.url);
|
||||
if (!list.length) return;
|
||||
const start = Math.min(Math.max(0, index), list.length - 1);
|
||||
|
||||
const dataSource = await Promise.all(
|
||||
list.map(async (m) => {
|
||||
if (isVideo(m)) {
|
||||
return { type: 'video', src: m.url as string, title: m.name };
|
||||
}
|
||||
const { w, h } = await getImageSize(m.url as string);
|
||||
return { src: m.url as string, width: w, height: h, alt: m.name, title: m.name };
|
||||
}),
|
||||
);
|
||||
|
||||
const lightbox = new PhotoSwipeLightbox({
|
||||
dataSource,
|
||||
index: start,
|
||||
pswpModule: PhotoSwipe as any,
|
||||
loop: true,
|
||||
bgOpacity: 0.96,
|
||||
wheelToZoom: true,
|
||||
// 图片支持双击/双指/滚轮缩放;视频项无宽高,PhotoSwipe 会自动隐藏其缩放按钮
|
||||
});
|
||||
|
||||
// 视频项:用自定义 DOM 替换默认内容
|
||||
lightbox.on('contentLoad', (e: any) => {
|
||||
const { content } = e;
|
||||
if (content?.data?.type !== 'video') return;
|
||||
e.preventDefault();
|
||||
const wrap = document.createElement('div');
|
||||
wrap.className = 'pswp-video';
|
||||
wrap.style.cssText =
|
||||
'width:100%;height:100%;display:flex;align-items:center;justify-content:center;';
|
||||
const video = document.createElement('video');
|
||||
video.src = content.data.src;
|
||||
video.controls = true;
|
||||
video.playsInline = true;
|
||||
video.preload = 'metadata';
|
||||
video.style.cssText = 'max-width:100%;max-height:100%;background:#000;';
|
||||
wrap.appendChild(video);
|
||||
content.element = wrap;
|
||||
});
|
||||
|
||||
// 离开当前张时暂停其视频
|
||||
const pauseVideo = (e: any) => {
|
||||
const v = e?.content?.element?.querySelector?.('video') as HTMLVideoElement | null;
|
||||
if (v) v.pause();
|
||||
};
|
||||
lightbox.on('contentDeactivate', pauseVideo);
|
||||
lightbox.on('close', () => {
|
||||
const v = document.querySelector('.pswp .pswp-video video') as HTMLVideoElement | null;
|
||||
if (v) v.pause();
|
||||
});
|
||||
// 关闭后销毁实例,释放键盘/尺寸监听
|
||||
lightbox.on('destroy', () => {
|
||||
/* PhotoSwipe 自行清理 */
|
||||
});
|
||||
|
||||
lightbox.init();
|
||||
lightbox.loadAndOpen(start);
|
||||
}
|
||||
+399
-46
@@ -38,30 +38,83 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 图片 / 视频轮播 -->
|
||||
<div v-if="data.artwork.images?.length" class="carousel">
|
||||
<div
|
||||
v-for="(m, i) in data.artwork.images"
|
||||
:key="i"
|
||||
class="carousel-item"
|
||||
@click="openViewer(data.artwork.images, i)"
|
||||
>
|
||||
<video
|
||||
v-if="isVideo(m)"
|
||||
:src="m.url"
|
||||
class="carousel-media"
|
||||
muted
|
||||
playsinline
|
||||
preload="metadata"
|
||||
/>
|
||||
<img v-else :src="m.url" :alt="m.name" class="carousel-media" loading="lazy" />
|
||||
<div v-if="isVideo(m)" class="play-overlay">
|
||||
<svg viewBox="0 0 64 64" width="56" height="56" aria-hidden="true">
|
||||
<circle cx="32" cy="32" r="30" fill="rgba(0,0,0,0.42)" />
|
||||
<polygon points="26,21 46,32 26,43" fill="#fff" />
|
||||
</svg>
|
||||
<!-- 图片 / 视频轮播:单张居中、两侧露出相邻媒体边界、定时切换、克隆实现无缝循环 -->
|
||||
<div
|
||||
v-if="data.artwork.images?.length"
|
||||
ref="carouselEl"
|
||||
class="carousel"
|
||||
@pointerdown="onDragStart"
|
||||
@pointermove="onDragMove"
|
||||
@pointerup="onDragEnd"
|
||||
@pointercancel="onDragEnd"
|
||||
@mouseenter="pauseAuto"
|
||||
@mouseleave="resumeAuto"
|
||||
>
|
||||
<div class="carousel-track" :style="trackStyle" @transitionend="onTransitionEnd">
|
||||
<div
|
||||
v-for="(s, i) in loopSlides"
|
||||
:key="i"
|
||||
class="carousel-slide"
|
||||
:class="{ current: i === pos }"
|
||||
@click="onSlideClick(i, s)"
|
||||
>
|
||||
<video
|
||||
v-if="isVideo(s.media)"
|
||||
:src="s.media.url"
|
||||
class="carousel-media"
|
||||
muted
|
||||
playsinline
|
||||
preload="metadata"
|
||||
/>
|
||||
<img v-else :src="s.media.url" :alt="s.media.name" class="carousel-media" loading="lazy" />
|
||||
<div v-if="isVideo(s.media)" class="play-overlay">
|
||||
<svg viewBox="0 0 64 64" width="56" height="56" aria-hidden="true">
|
||||
<circle cx="32" cy="32" r="30" fill="rgba(0,0,0,0.42)" />
|
||||
<polygon points="26,21 46,32 26,43" fill="#fff" />
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 左右切换箭头(PC 悬停显示,移动端常驻) -->
|
||||
<button
|
||||
v-if="data.artwork.images.length > 1"
|
||||
type="button"
|
||||
class="carousel-nav prev"
|
||||
:aria-label="t('viewer.prev')"
|
||||
@pointerdown.stop
|
||||
@click.stop="go(pos - 1)"
|
||||
>
|
||||
‹
|
||||
</button>
|
||||
<button
|
||||
v-if="data.artwork.images.length > 1"
|
||||
type="button"
|
||||
class="carousel-nav next"
|
||||
:aria-label="t('viewer.next')"
|
||||
@pointerdown.stop
|
||||
@click.stop="go(pos + 1)"
|
||||
>
|
||||
›
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 媒体计数 + 当前位置指示 -->
|
||||
<div v-if="data.artwork.images?.length > 1" class="carousel-ind">
|
||||
<div class="carousel-dots">
|
||||
<button
|
||||
v-for="(m, i) in data.artwork.images"
|
||||
:key="i"
|
||||
type="button"
|
||||
class="dot"
|
||||
:class="{ active: i === carouselIndex }"
|
||||
:aria-label="String(i + 1)"
|
||||
@click="goToReal(i)"
|
||||
/>
|
||||
</div>
|
||||
<div class="carousel-count">
|
||||
<span v-if="isVideo(data.artwork.images[carouselIndex])" class="carousel-count-ico">▶</span>
|
||||
{{ carouselIndex + 1 }} / {{ data.artwork.images.length }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 基础信息 -->
|
||||
@@ -178,9 +231,6 @@
|
||||
<footer class="footer muted">{{ appName }}</footer>
|
||||
</template>
|
||||
|
||||
<!-- 媒体查看器:图片可双指缩放/拖动,视频弹窗播放 -->
|
||||
<media-viewer v-model:show="viewerShow" :items="viewerItems" :start="viewerStart" />
|
||||
|
||||
<!-- 轻提示 -->
|
||||
<transition name="toast">
|
||||
<div v-if="toast" class="toast">{{ toast }}</div>
|
||||
@@ -189,13 +239,13 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref, watch } from 'vue';
|
||||
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
|
||||
import { useRoute } from 'vue-router';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import dayjs from 'dayjs';
|
||||
import { fetchTrace, fetchAppInfo } from '@/api';
|
||||
import { setLocale } from '@/locales';
|
||||
import MediaViewer from '@/components/MediaViewer.vue';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
|
||||
const route = useRoute();
|
||||
const { t, locale } = useI18n();
|
||||
@@ -205,10 +255,6 @@ const data = ref<any>(null);
|
||||
const appName = ref('');
|
||||
const toast = ref('');
|
||||
|
||||
const viewerShow = ref(false);
|
||||
const viewerItems = ref<any[]>([]);
|
||||
const viewerStart = ref(0);
|
||||
|
||||
// 节点图片默认收缩,避免页面过长;点击展开
|
||||
const expanded = ref<Set<number>>(new Set());
|
||||
function toggleNode(i: number) {
|
||||
@@ -244,9 +290,208 @@ function safeUrl(v?: string): string {
|
||||
return /^(https?:\/\/|\/)/i.test(v) ? v : '';
|
||||
}
|
||||
function openViewer(items: any[], index: number) {
|
||||
viewerItems.value = items || [];
|
||||
viewerStart.value = index;
|
||||
viewerShow.value = true;
|
||||
openMediaViewer(items, index);
|
||||
}
|
||||
|
||||
// 顶部媒体轮播:单张居中、两侧露出相邻媒体边界、定时切换并带切换动画。
|
||||
// 通过在首尾各复制一张「克隆」实现无缝循环:滑到克隆张后,动画结束瞬间无动画跳回真实张,
|
||||
// 因此无论手动还是自动,永远不会出现“最后一张直接跳回第一张”的违和回跳。
|
||||
// 后台设置 carouselInterval = 单张停留秒数(支持小数),0 表示不自动切换(仍可手动循环)。
|
||||
const carouselEl = ref<HTMLElement | null>(null);
|
||||
const carouselInterval = ref(3); // 秒/张,来自 /settings/public
|
||||
const pos = ref(0); // 当前在 loopSlides 中的下标
|
||||
const viewW = ref(0); // 容器宽度
|
||||
const slideW = ref(0); // 单张布局宽度
|
||||
const stride = ref(0); // 相邻两张中心间距(含间隙)
|
||||
const dragDelta = ref(0); // 拖动中的临时位移
|
||||
const dragging = ref(false);
|
||||
const noAnim = ref(true); // true 时禁用过渡(用于克隆瞬移、初始化、resize、拖动)
|
||||
|
||||
let autoTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
let ro: ResizeObserver | undefined;
|
||||
let startX = 0;
|
||||
let startY = 0;
|
||||
let axisLocked = false;
|
||||
let moved = false;
|
||||
|
||||
const carouselCount = computed(() => data.value?.artwork?.images?.length || 0);
|
||||
// 首尾各加一张克隆:[克隆(末), 真0..真N-1, 克隆(首)]
|
||||
const loopSlides = computed<{ media: any; real: number }[]>(() => {
|
||||
const imgs = data.value?.artwork?.images || [];
|
||||
if (imgs.length <= 1) return imgs.map((m: any, idx: number) => ({ media: m, real: idx }));
|
||||
const arr: { media: any; real: number }[] = [];
|
||||
arr.push({ media: imgs[imgs.length - 1], real: imgs.length - 1 });
|
||||
imgs.forEach((m: any, idx: number) => arr.push({ media: m, real: idx }));
|
||||
arr.push({ media: imgs[0], real: 0 });
|
||||
return arr;
|
||||
});
|
||||
// 当前真实下标(用于计数 / 圆点高亮)
|
||||
const carouselIndex = computed(() => {
|
||||
const n = carouselCount.value;
|
||||
if (n <= 1) return 0;
|
||||
return ((pos.value - 1) % n + n) % n;
|
||||
});
|
||||
const intervalMs = computed(() => Math.round(carouselInterval.value * 1000));
|
||||
const autoEnabled = computed(() => intervalMs.value > 0 && carouselCount.value > 1);
|
||||
|
||||
// 轨道位移:把第 pos 张居中;拖动时叠加 dragDelta
|
||||
const baseTrackX = computed(() => (viewW.value - slideW.value) / 2 - pos.value * stride.value);
|
||||
const trackStyle = computed(() => ({
|
||||
transform: `translateX(${baseTrackX.value + dragDelta.value}px)`,
|
||||
transitionDuration: noAnim.value || dragging.value ? '0ms' : '450ms',
|
||||
}));
|
||||
|
||||
function measureCarousel() {
|
||||
const el = carouselEl.value;
|
||||
if (!el) return;
|
||||
viewW.value = el.clientWidth;
|
||||
const track = el.querySelector('.carousel-track') as HTMLElement | null;
|
||||
const slides = track?.children;
|
||||
if (slides && slides.length) {
|
||||
const a = slides[0] as HTMLElement;
|
||||
slideW.value = a.offsetWidth; // offset* 取布局尺寸,不受 CSS transform 缩放影响
|
||||
stride.value =
|
||||
slides.length > 1 ? (slides[1] as HTMLElement).offsetLeft - a.offsetLeft : a.offsetWidth;
|
||||
}
|
||||
}
|
||||
|
||||
function clearAuto() {
|
||||
if (autoTimer) {
|
||||
clearTimeout(autoTimer);
|
||||
autoTimer = undefined;
|
||||
}
|
||||
}
|
||||
function scheduleAuto() {
|
||||
clearAuto();
|
||||
if (!autoEnabled.value) return;
|
||||
autoTimer = setTimeout(() => next(), intervalMs.value);
|
||||
}
|
||||
function pauseAuto() {
|
||||
clearAuto();
|
||||
}
|
||||
function resumeAuto() {
|
||||
scheduleAuto();
|
||||
}
|
||||
|
||||
// 切换到 loopSlides 的第 p 张(带动画),并重置自动计时
|
||||
function go(p: number) {
|
||||
pos.value = p;
|
||||
scheduleAuto();
|
||||
}
|
||||
function next() {
|
||||
go(pos.value + 1);
|
||||
}
|
||||
// 跳到真实第 i 张
|
||||
function goToReal(i: number) {
|
||||
go(i + 1);
|
||||
}
|
||||
|
||||
// 动画结束后若停在克隆张,则无动画瞬移到对应真实张,实现无缝循环
|
||||
async function onTransitionEnd(e: TransitionEvent) {
|
||||
// 仅响应轨道自身的 transform 过渡;忽略子卡片缩放过渡的冒泡事件
|
||||
if (e.target !== e.currentTarget || e.propertyName !== 'transform') return;
|
||||
const n = carouselCount.value;
|
||||
if (n <= 1) return;
|
||||
let target = pos.value;
|
||||
if (pos.value > n) target = pos.value - n; // 越过末尾克隆 → 回到真实首部
|
||||
else if (pos.value < 1) target = pos.value + n; // 越过开头克隆 → 回到真实尾部
|
||||
if (target === pos.value) return;
|
||||
noAnim.value = true;
|
||||
pos.value = target;
|
||||
await nextTick();
|
||||
requestAnimationFrame(() =>
|
||||
requestAnimationFrame(() => {
|
||||
noAnim.value = false;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function onSlideClick(i: number, slide: { media: any; real: number }) {
|
||||
if (moved) {
|
||||
moved = false;
|
||||
return; // 刚才是拖动,不触发点击
|
||||
}
|
||||
if (i === pos.value) {
|
||||
openViewer(data.value?.artwork?.images || [], slide.real); // 点当前张 → 打开查看器
|
||||
} else {
|
||||
go(i); // 点两侧露出的相邻张 → 切到它
|
||||
}
|
||||
}
|
||||
|
||||
function onDragStart(e: PointerEvent) {
|
||||
if (carouselCount.value <= 1) return;
|
||||
dragging.value = true;
|
||||
moved = false;
|
||||
axisLocked = false;
|
||||
startX = e.clientX;
|
||||
startY = e.clientY;
|
||||
dragDelta.value = 0;
|
||||
pauseAuto();
|
||||
// 注意:不在此处 setPointerCapture,否则会把后续 click 改派到容器,
|
||||
// 导致 PC 上卡片的 @click 不触发、无法点击查看。仅在确认拖动后再捕获。
|
||||
}
|
||||
function onDragMove(e: PointerEvent) {
|
||||
if (!dragging.value) return;
|
||||
const dx = e.clientX - startX;
|
||||
const dy = e.clientY - startY;
|
||||
if (!axisLocked) {
|
||||
if (Math.abs(dx) < 6 && Math.abs(dy) < 6) return;
|
||||
axisLocked = true;
|
||||
if (Math.abs(dy) > Math.abs(dx)) {
|
||||
// 纵向手势 → 放弃拖动,交还页面滚动
|
||||
dragging.value = false;
|
||||
dragDelta.value = 0;
|
||||
return;
|
||||
}
|
||||
// 确认为横向拖动后再捕获指针,保证拖出元素也能继续跟手
|
||||
carouselEl.value?.setPointerCapture?.(e.pointerId);
|
||||
}
|
||||
moved = true;
|
||||
dragDelta.value = dx;
|
||||
}
|
||||
function onDragEnd(e?: PointerEvent) {
|
||||
if (!dragging.value) return;
|
||||
dragging.value = false;
|
||||
const dd = dragDelta.value;
|
||||
dragDelta.value = 0;
|
||||
if (e && carouselEl.value?.hasPointerCapture?.(e.pointerId)) {
|
||||
carouselEl.value.releasePointerCapture(e.pointerId);
|
||||
}
|
||||
const threshold = Math.max(40, slideW.value * 0.18);
|
||||
if (dd <= -threshold) go(pos.value + 1);
|
||||
else if (dd >= threshold) go(pos.value - 1);
|
||||
else scheduleAuto(); // 位移不够 → 回弹(pos 不变,过渡自动复位),恢复自动
|
||||
}
|
||||
|
||||
function onCarouselResize() {
|
||||
noAnim.value = true;
|
||||
measureCarousel();
|
||||
requestAnimationFrame(() =>
|
||||
requestAnimationFrame(() => {
|
||||
noAnim.value = false;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// 数据 / 间隔变化时重置轮播
|
||||
async function resetCarousel() {
|
||||
clearAuto();
|
||||
ro?.disconnect();
|
||||
pos.value = carouselCount.value > 1 ? 1 : 0;
|
||||
noAnim.value = true;
|
||||
dragDelta.value = 0;
|
||||
await nextTick();
|
||||
measureCarousel();
|
||||
if (carouselEl.value) {
|
||||
ro = new ResizeObserver(onCarouselResize);
|
||||
ro.observe(carouselEl.value);
|
||||
}
|
||||
requestAnimationFrame(() =>
|
||||
requestAnimationFrame(() => {
|
||||
noAnim.value = false;
|
||||
scheduleAuto();
|
||||
}),
|
||||
);
|
||||
}
|
||||
function toggleLang() {
|
||||
setLocale(locale.value === 'zh-CN' ? 'en-US' : 'zh-CN');
|
||||
@@ -270,7 +515,6 @@ async function copy(text?: string) {
|
||||
async function load(code: string) {
|
||||
loading.value = true;
|
||||
data.value = null;
|
||||
viewerShow.value = false;
|
||||
expanded.value = new Set();
|
||||
try {
|
||||
data.value = await fetchTrace(code);
|
||||
@@ -285,11 +529,23 @@ onMounted(async () => {
|
||||
try {
|
||||
const info = await fetchAppInfo();
|
||||
appName.value = info?.appName || t('app.name');
|
||||
const iv = Number(info?.carouselInterval);
|
||||
if (Number.isFinite(iv) && iv >= 0) carouselInterval.value = iv;
|
||||
} catch {
|
||||
appName.value = t('app.name');
|
||||
}
|
||||
});
|
||||
|
||||
// 数据或间隔变化时重置轮播
|
||||
watch([() => data.value?.artwork?.images, carouselInterval], () => {
|
||||
resetCarousel();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
clearAuto();
|
||||
ro?.disconnect();
|
||||
});
|
||||
|
||||
watch(
|
||||
() => route.params.code,
|
||||
(code) => {
|
||||
@@ -401,23 +657,120 @@ watch(
|
||||
word-break: break-all;
|
||||
}
|
||||
.carousel {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
overflow-x: auto;
|
||||
scroll-snap-type: x mandatory;
|
||||
padding: 0 12px;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
}
|
||||
.carousel-item {
|
||||
position: relative;
|
||||
width: 86%;
|
||||
overflow: hidden;
|
||||
touch-action: pan-y; /* 纵向交还页面滚动,横向手势由 JS 处理 */
|
||||
user-select: none;
|
||||
}
|
||||
.carousel-track {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
transition-property: transform;
|
||||
transition-timing-function: cubic-bezier(0.22, 0.61, 0.36, 1);
|
||||
will-change: transform;
|
||||
}
|
||||
.carousel-slide {
|
||||
position: relative;
|
||||
flex: 0 0 80%;
|
||||
max-width: 420px;
|
||||
height: 240px;
|
||||
border-radius: 14px;
|
||||
scroll-snap-align: center;
|
||||
flex-shrink: 0;
|
||||
background: #e7ecf3;
|
||||
overflow: hidden;
|
||||
cursor: pointer;
|
||||
/* 非当前张略缩小并降低不透明度,突出居中张 */
|
||||
transform: scale(0.9);
|
||||
opacity: 0.55;
|
||||
transition:
|
||||
transform 0.45s cubic-bezier(0.22, 0.61, 0.36, 1),
|
||||
opacity 0.45s ease;
|
||||
}
|
||||
.carousel-slide.current {
|
||||
transform: scale(1);
|
||||
opacity: 1;
|
||||
}
|
||||
/* 左右切换箭头 */
|
||||
.carousel-nav {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
width: 36px;
|
||||
height: 56px;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
background: rgba(15, 123, 99, 0.42);
|
||||
color: #fff;
|
||||
font-size: 26px;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 2;
|
||||
opacity: 0;
|
||||
transition: opacity 0.2s ease;
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
}
|
||||
.carousel:hover .carousel-nav {
|
||||
opacity: 1;
|
||||
}
|
||||
.carousel-nav.prev {
|
||||
left: 14px;
|
||||
}
|
||||
.carousel-nav.next {
|
||||
right: 14px;
|
||||
}
|
||||
.carousel-nav:active {
|
||||
background: rgba(15, 123, 99, 0.7);
|
||||
}
|
||||
/* 触摸设备没有 hover:常驻显示,方便点击 */
|
||||
@media (hover: none) {
|
||||
.carousel-nav {
|
||||
opacity: 0.85;
|
||||
}
|
||||
}
|
||||
/* 媒体计数 + 当前位置指示 */
|
||||
.carousel-ind {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 12px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
.carousel-dots {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
}
|
||||
.carousel-dots .dot {
|
||||
width: 7px;
|
||||
height: 7px;
|
||||
padding: 0;
|
||||
border: none;
|
||||
border-radius: 50%;
|
||||
background: #cfd8e3;
|
||||
cursor: pointer;
|
||||
transition:
|
||||
width 0.25s ease,
|
||||
background 0.25s ease;
|
||||
}
|
||||
.carousel-dots .dot.active {
|
||||
width: 18px;
|
||||
border-radius: 4px;
|
||||
background: #0f7b63;
|
||||
}
|
||||
.carousel-count {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 3px;
|
||||
font-size: 12px;
|
||||
color: #6b7785;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.carousel-count-ico {
|
||||
font-size: 10px;
|
||||
color: #0f7b63;
|
||||
}
|
||||
.carousel-media {
|
||||
width: 100%;
|
||||
|
||||
Reference in New Issue
Block a user