feat: 新增媒体排序组件,支持图片与视频的拖拽排序与上传,优化媒体查看器功能

This commit is contained in:
Nixevol
2026-06-28 23:49:49 +08:00
parent 658d9571da
commit d18b7c592d
15 changed files with 1363 additions and 95 deletions
+220
View File
@@ -0,0 +1,220 @@
<template>
<div class="media-sorter">
<div class="grid">
<div
v-for="(m, i) in list"
:key="m.key"
class="cell"
:class="{ dragging: dragIndex === i, over: overIndex === i }"
draggable="true"
@dragstart="onDragStart(i)"
@dragover.prevent="onDragOver(i)"
@drop="onDrop(i)"
@dragend="onDragEnd"
>
<video v-if="isVideo(m)" class="thumb" :src="m.url" muted preload="metadata" />
<img v-else class="thumb" :src="m.url" :alt="m.name" />
<div v-if="isVideo(m)" class="play"><n-icon :component="PlayCircleOutline" :size="26" /></div>
<div class="order">{{ i + 1 }}</div>
<button class="del" type="button" :title="t('common.action.delete')" @click="remove(i)">×</button>
</div>
<!-- 上传中占位 -->
<div v-for="u in uploading" :key="u.id" class="cell uploading">
<n-spin size="small" />
</div>
<!-- 上传按钮 -->
<label v-if="list.length + uploading.length < max" class="cell add">
<input ref="inputRef" type="file" accept="image/*,video/*" multiple hidden @change="onPick" />
<n-icon :component="AddOutline" :size="22" />
<span>{{ t('common.action.upload') }}</span>
</label>
</div>
<div class="hint">{{ t('pages.artwork.mediaHint') }}</div>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { NIcon, NSpin } from 'naive-ui';
import { AddOutline, PlayCircleOutline } from '@vicons/ionicons5';
import { post } from '@/api/request';
import { message } from '@/utils/feedback';
interface MediaItem {
key: string;
url?: string;
name?: string;
type?: string;
}
const props = withDefaults(defineProps<{ modelValue: MediaItem[]; max?: number }>(), { max: 12 });
const emit = defineEmits<{ (e: 'update:modelValue', v: MediaItem[]): void }>();
const { t } = useI18n();
const inputRef = ref<HTMLInputElement | null>(null);
const list = computed<MediaItem[]>(() => props.modelValue || []);
const uploading = ref<{ id: number }[]>([]);
let seq = 0;
function isVideo(m: MediaItem) {
return m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(m.url || m.key || '');
}
async function onPick(e: Event) {
const input = e.target as HTMLInputElement;
const files = Array.from(input.files || []);
input.value = '';
for (const file of files) {
if (list.value.length + uploading.value.length >= props.max) {
message.warning(t('common.msg.maxReached', { n: props.max }));
break;
}
await uploadOne(file);
}
}
async function uploadOne(file: File) {
const type = file.type.startsWith('video/') ? 'video' : 'image';
const holder = { id: ++seq };
uploading.value.push(holder);
try {
const fd = new FormData();
fd.append('file', file);
const res: any = await post(`/upload?type=${type}`, fd);
emit('update:modelValue', [
...list.value,
{ key: res.key, url: res.url, name: res.name, type },
]);
} catch (err) {
message.error((err as Error)?.message || 'upload failed');
} finally {
uploading.value = uploading.value.filter((u) => u.id !== holder.id);
}
}
function remove(i: number) {
emit('update:modelValue', list.value.filter((_, idx) => idx !== i));
}
/* 拖拽排序 */
const dragIndex = ref<number | null>(null);
const overIndex = ref<number | null>(null);
function onDragStart(i: number) {
dragIndex.value = i;
}
function onDragOver(i: number) {
overIndex.value = i;
}
function onDrop(i: number) {
const from = dragIndex.value;
if (from === null || from === i) return;
const arr = [...list.value];
const [moved] = arr.splice(from, 1);
arr.splice(i, 0, moved);
emit('update:modelValue', arr);
onDragEnd();
}
function onDragEnd() {
dragIndex.value = null;
overIndex.value = null;
}
</script>
<style scoped>
.grid {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.cell {
position: relative;
width: 96px;
height: 96px;
border-radius: 8px;
overflow: hidden;
border: 1px solid var(--n-border-color, #e0e0e6);
background: #f4f7fc;
display: flex;
align-items: center;
justify-content: center;
}
.cell .thumb {
width: 100%;
height: 100%;
object-fit: cover;
background: #000;
}
.cell.dragging {
opacity: 0.4;
}
.cell.over {
outline: 2px dashed var(--brand, #138a72);
outline-offset: -2px;
}
.cell[draggable='true'] {
cursor: grab;
}
.play {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
background: rgba(0, 0, 0, 0.28);
pointer-events: none;
}
.order {
position: absolute;
left: 4px;
bottom: 4px;
min-width: 18px;
height: 18px;
padding: 0 5px;
border-radius: 9px;
background: rgba(0, 0, 0, 0.6);
color: #fff;
font-size: 11px;
line-height: 18px;
text-align: center;
}
.del {
position: absolute;
top: 2px;
right: 2px;
width: 20px;
height: 20px;
border: none;
border-radius: 50%;
background: rgba(0, 0, 0, 0.55);
color: #fff;
font-size: 14px;
line-height: 20px;
cursor: pointer;
padding: 0;
}
.del:hover {
background: #d03050;
}
.cell.add,
.cell.uploading {
flex-direction: column;
gap: 4px;
font-size: 12px;
color: #6b7280;
cursor: pointer;
border-style: dashed;
background: #fafcff;
}
.cell.uploading {
cursor: default;
}
.hint {
margin-top: 8px;
font-size: 12px;
color: #9ca3af;
}
</style>
+4 -2
View File
@@ -106,7 +106,8 @@
"operationSuccess": "Success",
"loginExpired": "Session expired, please log in again",
"networkError": "Network error, please try again",
"noData": "No data"
"noData": "No data",
"maxReached": "Up to {n} files"
},
"lang": { "zh": "中文", "en": "English" }
},
@@ -163,7 +164,8 @@
"title": "Title",
"artist": "Artist",
"template": "Template",
"images": "Images",
"images": "Images / Videos",
"mediaHint": "Images and videos supported. Drag to reorder (order = display order on the consumer page).",
"basicInfo": "Basic Info",
"dynamicFields": "Fields",
"nodes": "Timeline",
+4 -2
View File
@@ -106,7 +106,8 @@
"operationSuccess": "操作成功",
"loginExpired": "登录已过期,请重新登录",
"networkError": "网络异常,请稍后再试",
"noData": "暂无数据"
"noData": "暂无数据",
"maxReached": "最多上传 {n} 个"
},
"lang": { "zh": "中文", "en": "English" }
},
@@ -163,7 +164,8 @@
"title": "作品名称",
"artist": "艺术家",
"template": "所属模板",
"images": "作品图片",
"images": "作品图片 / 视频",
"mediaHint": "支持图片与视频,拖拽可调整顺序(顺序即 C 端展示顺序)",
"basicInfo": "基础信息",
"dynamicFields": "动态字段",
"nodes": "溯源时间轴",
+11 -3
View File
@@ -54,7 +54,7 @@
</n-grid>
<n-divider style="margin: 4px 0 16px">{{ t('pages.artwork.images') }}</n-divider>
<media-upload v-model="form.images" type="image" :max="8" />
<media-sorter v-model="form.images" :max="12" />
</n-form>
<div style="text-align: right; margin-top: 16px">
<n-button type="primary" :loading="saving" :disabled="!auth.canWrite" @click="saveBasic">
@@ -251,6 +251,7 @@ import { ArtworkStatus, CertType, NodeType } from '@artveris/shared';
import { artworkApi, templateApi, traceCodeApi } from '@/api';
import { useAuthStore } from '@/stores/auth';
import MediaUpload from '@/components/MediaUpload.vue';
import MediaSorter from '@/components/MediaSorter.vue';
import { message, dialog } from '@/utils/feedback';
import { fmt, fmtDate, statusType } from '@/utils/format';
@@ -310,7 +311,10 @@ async function loadArtwork() {
form.artist = a.artist;
form.status = a.status;
form.dynamicFields = a.dynamicFields || {};
form.images = a.images || [];
form.images = (a.images || []).map((m: any) => ({
...m,
type: m.type || (isVideoMedia(m) ? 'video' : 'image'),
}));
nodes.value = a.traceNodes || [];
certs.value = a.certificates || [];
}
@@ -329,7 +333,11 @@ async function saveBasic() {
artist: form.artist,
status: form.status,
dynamicFields: form.dynamicFields,
images: form.images.map((m: any) => ({ key: m.key, name: m.name })),
images: form.images.map((m: any) => ({
key: m.key,
name: m.name,
type: m.type || (isVideoMedia(m) ? 'video' : 'image'),
})),
};
if (isNew.value) {
const created: any = await artworkApi.create(payload);
+302
View File
@@ -0,0 +1,302 @@
<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 class="stage">
<video
v-if="curIsVideo"
:key="cur?.url"
class="media-video"
:src="cur?.url"
controls
autoplay
playsinline
@click.stop
/>
<img
v-else
class="media-img"
:src="cur?.url"
:alt="cur?.name"
:style="imgStyle"
@touchstart="onTouchStart"
@touchmove.prevent="onTouchMove"
@touchend="onTouchEnd"
@wheel.prevent="onWheel"
@dblclick="onDblClick"
@click.stop
/>
</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 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 ? 'none' : 'transform 0.18s ease',
cursor: scale.value > 1 ? '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(5, Math.max(1, s));
}
function clampOffset() {
const maxX = ((scale.value - 1) * window.innerWidth) / 2 + 40;
const maxY = ((scale.value - 1) * window.innerHeight) / 2 + 40;
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;
}
}
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;
}
.viewer-fade-enter-active,
.viewer-fade-leave-active {
transition: opacity 0.2s ease;
}
.viewer-fade-enter-from,
.viewer-fade-leave-to {
opacity: 0;
}
</style>
+8
View File
@@ -33,6 +33,14 @@
"abnormalTitle": "Abnormal Status",
"abnormalDesc": "This code status is abnormal. Please verify carefully."
},
"viewer": {
"zoomIn": "Zoom in",
"zoomOut": "Zoom out",
"reset": "Reset",
"close": "Close",
"prev": "Previous",
"next": "Next"
},
"artist": "Artist",
"lang": { "zh": "中文", "en": "EN" }
}
+8
View File
@@ -33,6 +33,14 @@
"abnormalTitle": "防伪码状态异常",
"abnormalDesc": "该防伪码状态异常,请谨慎核验。"
},
"viewer": {
"zoomIn": "放大",
"zoomOut": "缩小",
"reset": "还原",
"close": "关闭",
"prev": "上一个",
"next": "下一个"
},
"artist": "艺术家",
"lang": { "zh": "中文", "en": "EN" }
}
+56 -33
View File
@@ -36,15 +36,30 @@
</div>
</div>
<!-- 图片轮播 -->
<!-- 图片 / 视频轮播 -->
<div v-if="data.artwork.images?.length" class="carousel">
<img
v-for="(img, i) in data.artwork.images"
<div
v-for="(m, i) in data.artwork.images"
:key="i"
:src="img.url"
class="carousel-img"
@click="openLightbox(img.url)"
/>
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>
</div>
</div>
<!-- 基础信息 -->
@@ -79,7 +94,7 @@
playsinline
preload="metadata"
/>
<img v-else :src="m.url" @click="openLightbox(m.url)" />
<img v-else :src="m.url" loading="lazy" @click="openViewer([m], 0)" />
</template>
</div>
</div>
@@ -92,7 +107,7 @@
<h2 class="section-title">{{ t('section.cert') }}</h2>
<div class="cert-grid">
<div v-for="(c, i) in data.certificates" :key="i" class="cert">
<img v-if="c.url" :src="c.url" @click="openLightbox(c.url)" />
<img v-if="c.url" :src="c.url" loading="lazy" @click="openViewer([{ url: c.url, name: c.name, type: 'image' }], 0)" />
<div class="cert-name text-ellipsis">{{ c.name }}</div>
<div v-if="c.issuer" class="muted cert-issuer text-ellipsis">{{ c.issuer }}</div>
</div>
@@ -123,10 +138,8 @@
<footer class="footer muted">{{ appName }}</footer>
</template>
<!-- 大图预览 -->
<div v-if="lightbox" class="lightbox" @click="lightbox = ''">
<img :src="lightbox" />
</div>
<!-- 媒体查看器:图片可双指缩放/拖动,视频弹窗播放 -->
<media-viewer v-model:show="viewerShow" :items="viewerItems" :start="viewerStart" />
<!-- 轻提示 -->
<transition name="toast">
@@ -142,6 +155,7 @@ import { useI18n } from 'vue-i18n';
import dayjs from 'dayjs';
import { fetchTrace, fetchAppInfo } from '@/api';
import { setLocale } from '@/locales';
import MediaViewer from '@/components/MediaViewer.vue';
const route = useRoute();
const { t, locale } = useI18n();
@@ -149,9 +163,12 @@ const { t, locale } = useI18n();
const loading = ref(true);
const data = ref<any>(null);
const appName = ref('');
const lightbox = ref('');
const toast = ref('');
const viewerShow = ref(false);
const viewerItems = ref<any[]>([]);
const viewerStart = ref(0);
const otherLangLabel = computed(() => (locale.value === 'zh-CN' ? 'EN' : '中文'));
const stateKey = computed(() => {
const s = data.value?.status;
@@ -171,10 +188,12 @@ function ellipsis(s?: string) {
return s.length > 16 ? `${s.slice(0, 8)}…${s.slice(-6)}` : s;
}
function isVideo(m: { type?: string; url?: string }) {
return m.type === 'video' || /\.(mp4|webm|mov|m4v)$/i.test(m.url || '');
return m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(m.url || '');
}
function openLightbox(url: string) {
lightbox.value = url;
function openViewer(items: any[], index: number) {
viewerItems.value = items || [];
viewerStart.value = index;
viewerShow.value = true;
}
function toggleLang() {
setLocale(locale.value === 'zh-CN' ? 'en-US' : 'zh-CN');
@@ -198,7 +217,7 @@ async function copy(text?: string) {
async function load(code: string) {
loading.value = true;
data.value = null;
lightbox.value = '';
viewerShow.value = false;
try {
data.value = await fetchTrace(code);
} catch {
@@ -329,14 +348,31 @@ watch(
padding: 0 12px;
-webkit-overflow-scrolling: touch;
}
.carousel-img {
.carousel-item {
position: relative;
width: 86%;
height: 240px;
object-fit: cover;
border-radius: 14px;
scroll-snap-align: center;
flex-shrink: 0;
background: #e7ecf3;
overflow: hidden;
cursor: pointer;
}
.carousel-media {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
background: #000;
}
.play-overlay {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
pointer-events: none;
}
.art-title {
font-size: 20px;
@@ -484,19 +520,6 @@ watch(
font-size: 12px;
padding: 24px 0 32px;
}
.lightbox {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.92);
display: flex;
align-items: center;
justify-content: center;
z-index: 50;
}
.lightbox img {
max-width: 100%;
max-height: 100%;
}
.toast {
position: fixed;
bottom: 60px;