fix: 修复本地上传资源路径解析
This commit is contained in:
@@ -12,8 +12,8 @@
|
||||
@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" loading="lazy" />
|
||||
<video v-if="isVideo(m)" class="thumb" :src="mediaSrc(m)" muted preload="metadata" />
|
||||
<img v-else class="thumb" :src="mediaSrc(m)" :alt="m.name" loading="lazy" />
|
||||
<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.stop="remove(i)">×</button>
|
||||
@@ -52,6 +52,7 @@ import { AddOutline, PlayCircleOutline } from '@vicons/ionicons5';
|
||||
import { post } from '@/api/request';
|
||||
import { message } from '@/utils/feedback';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
interface MediaItem {
|
||||
key: string;
|
||||
@@ -77,8 +78,12 @@ function updateCaption(i: number, v: string) {
|
||||
emit('update:modelValue', arr);
|
||||
}
|
||||
|
||||
function mediaSrc(m: MediaItem) {
|
||||
return resolveMediaUrl(m.url || m.key);
|
||||
}
|
||||
|
||||
function isVideo(m: MediaItem) {
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(m.url || m.key || '');
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(mediaSrc(m));
|
||||
}
|
||||
|
||||
async function onPick(e: Event) {
|
||||
@@ -104,7 +109,7 @@ async function uploadOne(file: File) {
|
||||
const res: any = await post(`/upload?type=${type}`, fd);
|
||||
emit('update:modelValue', [
|
||||
...list.value,
|
||||
{ key: res.key, url: res.url, name: props.captionable ? '' : res.name, type },
|
||||
{ key: res.key, url: resolveMediaUrl(res.url), name: props.captionable ? '' : res.name, type },
|
||||
]);
|
||||
} catch {
|
||||
message.error(t('common.msg.uploadFailed'));
|
||||
@@ -118,7 +123,7 @@ function remove(i: number) {
|
||||
}
|
||||
|
||||
function toViewerItem(m: MediaItem) {
|
||||
return { url: m.url || m.key, name: m.name || m.key, type: m.type };
|
||||
return { url: mediaSrc(m), name: m.name || m.key, type: m.type };
|
||||
}
|
||||
|
||||
function preview(index: number) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<div class="media-upload" :class="{ text: listType === 'text' }">
|
||||
<div v-if="listType === 'image-card'" class="image-grid">
|
||||
<div v-for="(m, i) in items" :key="m.key" class="image-item previewable" @click="preview(i)">
|
||||
<img v-if="isImage(m)" class="image-thumb" :src="m.url || m.key" :alt="m.name || m.key" loading="lazy" />
|
||||
<img v-if="isImage(m)" class="image-thumb" :src="mediaSrc(m)" :alt="m.name || m.key" loading="lazy" />
|
||||
<div v-else class="file-thumb">{{ fileExt(m) }}</div>
|
||||
<button class="remove" type="button" :title="t('common.action.delete')" @click.stop="remove(m.key)">×</button>
|
||||
</div>
|
||||
@@ -46,6 +46,7 @@ import { AddOutline, CloudUploadOutline } from '@vicons/ionicons5';
|
||||
import { post } from '@/api/request';
|
||||
import { message } from '@/utils/feedback';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
interface MediaItem {
|
||||
key: string;
|
||||
@@ -72,8 +73,12 @@ const accept = computed(() =>
|
||||
const allowMultiple = computed(() => props.max > 1);
|
||||
const canUpload = computed(() => items.value.length + uploading.value.length < props.max);
|
||||
|
||||
function mediaSrc(m: MediaItem) {
|
||||
return resolveMediaUrl(m.url || m.key);
|
||||
}
|
||||
|
||||
function isImage(m: MediaItem) {
|
||||
return props.type === 'image' || /^image\//.test(m.type || '') || /\.(png|jpe?g|gif|webp|bmp|svg)$/i.test(m.url || m.key);
|
||||
return props.type === 'image' || /^image\//.test(m.type || '') || /\.(png|jpe?g|gif|webp|bmp|svg)$/i.test(mediaSrc(m));
|
||||
}
|
||||
|
||||
function fileExt(m: MediaItem) {
|
||||
@@ -104,7 +109,7 @@ async function uploadOne(file: File) {
|
||||
const res: any = await post(`/upload?type=${props.type}`, fd);
|
||||
emit('update:modelValue', [
|
||||
...items.value,
|
||||
{ key: res.key, url: res.url, name: res.name || file.name, type: props.type },
|
||||
{ key: res.key, url: resolveMediaUrl(res.url), name: res.name || file.name, type: props.type },
|
||||
]);
|
||||
} catch {
|
||||
message.error(t('common.msg.uploadFailed'));
|
||||
@@ -119,7 +124,7 @@ function remove(key: string) {
|
||||
|
||||
function preview(index: number) {
|
||||
void openMediaViewer(
|
||||
items.value.map((m) => ({ url: m.url || m.key, name: m.name || m.key, type: m.type })),
|
||||
items.value.map((m) => ({ url: mediaSrc(m), name: m.name || m.key, type: m.type })),
|
||||
index,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import PhotoSwipe from 'photoswipe';
|
||||
import PhotoSwipeLightbox from 'photoswipe/lightbox';
|
||||
import 'photoswipe/style.css';
|
||||
import { i18n } from '@/locales';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
export interface MediaViewerItem {
|
||||
url?: string;
|
||||
@@ -308,7 +309,9 @@ function registerToolbar(lightbox: PhotoSwipeLightbox): void {
|
||||
}
|
||||
|
||||
export async function openMediaViewer(items: MediaViewerItem[], index = 0): Promise<void> {
|
||||
const list = (items || []).filter((item) => item.url);
|
||||
const list = (items || [])
|
||||
.map((item) => ({ ...item, url: resolveMediaUrl(item.url) }))
|
||||
.filter((item) => item.url);
|
||||
if (!list.length) return;
|
||||
const start = Math.min(Math.max(index, 0), list.length - 1);
|
||||
const dataSource = await Promise.all(
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
export function resolveMediaUrl(url?: string | null): string {
|
||||
const value = String(url || '').trim();
|
||||
if (!value) return '';
|
||||
if (/^\d{4}\/\d{2}\/\d{2}\/[^?#]+$/i.test(value)) {
|
||||
return `/uploads/${value}`;
|
||||
}
|
||||
if (/^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::\d+)?\/uploads\//i.test(value)) {
|
||||
return value.replace(/^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::\d+)?/i, '');
|
||||
}
|
||||
if (typeof window !== 'undefined' && /^https?:\/\//i.test(value)) {
|
||||
try {
|
||||
const parsed = new URL(value);
|
||||
if (parsed.origin === window.location.origin && parsed.pathname.startsWith('/uploads/')) {
|
||||
return `${parsed.pathname}${parsed.search}${parsed.hash}`;
|
||||
}
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -130,14 +130,14 @@
|
||||
:model-value="toMedia(form.dynamicFields[f.fieldKey])"
|
||||
type="image"
|
||||
:max="1"
|
||||
@update:model-value="(v) => (form.dynamicFields[f.fieldKey] = v[0]?.url || '')"
|
||||
@update:model-value="(v) => (form.dynamicFields[f.fieldKey] = mediaKey(v[0]) || '')"
|
||||
/>
|
||||
<media-upload
|
||||
v-else-if="f.type === 'FILE'"
|
||||
:model-value="toMedia(form.dynamicFields[f.fieldKey])"
|
||||
type="file"
|
||||
:max="1"
|
||||
@update:model-value="(v) => (form.dynamicFields[f.fieldKey] = v[0]?.url || '')"
|
||||
@update:model-value="(v) => (form.dynamicFields[f.fieldKey] = mediaKey(v[0]) || '')"
|
||||
/>
|
||||
<n-input v-else v-model:value="form.dynamicFields[f.fieldKey]" />
|
||||
</n-form-item-gi>
|
||||
@@ -176,7 +176,7 @@
|
||||
<template v-for="m in n.media" :key="m.url || m.key">
|
||||
<video
|
||||
v-if="isVideoMedia(m)"
|
||||
:src="m.url"
|
||||
:src="mediaUrl(m)"
|
||||
controls
|
||||
preload="metadata"
|
||||
style="max-height: 90px; border-radius: 6px; background: #000"
|
||||
@@ -184,7 +184,7 @@
|
||||
<img
|
||||
v-else
|
||||
class="node-media-image"
|
||||
:src="m.url"
|
||||
:src="mediaUrl(m)"
|
||||
:alt="m.name || n.title"
|
||||
loading="lazy"
|
||||
@click="previewNodeImage(n.media, m)"
|
||||
@@ -213,7 +213,7 @@
|
||||
<img
|
||||
v-if="c.watermarkUrl || c.originUrl"
|
||||
class="cert-preview-image"
|
||||
:src="c.watermarkUrl || c.originUrl"
|
||||
:src="certUrl(c)"
|
||||
:alt="c.name"
|
||||
loading="lazy"
|
||||
@click="previewCert(c)"
|
||||
@@ -341,6 +341,7 @@ import MediaSorter from '@/components/MediaSorter.vue';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { message, dialog } from '@/utils/feedback';
|
||||
import { fmtDate, fmtSecond } from '@/utils/format';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
const { t } = useI18n();
|
||||
const route = useRoute();
|
||||
@@ -415,7 +416,7 @@ function onTemplateChange() {
|
||||
|
||||
// 动态字段 IMAGE/FILE 适配:dynamicFields 存 url 字符串,MediaUpload 需要 MediaItem[]
|
||||
function toMedia(url: unknown) {
|
||||
return url ? [{ key: String(url), url: String(url), name: String(url) }] : [];
|
||||
return url ? [{ key: String(url), url: resolveMediaUrl(String(url)), name: String(url) }] : [];
|
||||
}
|
||||
function auditTagType(s?: string) {
|
||||
return s === 'APPROVED' ? 'success' : s === 'REJECTED' ? 'error' : 'warning';
|
||||
@@ -514,7 +515,7 @@ function normalizeNode(n: any) {
|
||||
...n,
|
||||
media: media.map((m: any) => ({
|
||||
key: m.key || m.url,
|
||||
url: m.url,
|
||||
url: resolveMediaUrl(m.url || m.key),
|
||||
name: m.name,
|
||||
type: m.type || (isVideoMedia(m) ? 'video' : 'image'),
|
||||
})),
|
||||
@@ -527,8 +528,8 @@ function normalizeCert(c: any) {
|
||||
originKey: c.originKey ?? c.originFileUrl,
|
||||
watermarkKey: c.watermarkKey ?? c.watermarkFileUrl,
|
||||
issuedAt: c.issuedAt,
|
||||
originUrl: c.originUrl,
|
||||
watermarkUrl: c.watermarkUrl,
|
||||
originUrl: resolveMediaUrl(c.originUrl || c.originKey || c.originFileUrl),
|
||||
watermarkUrl: resolveMediaUrl(c.watermarkUrl || c.watermarkKey || c.watermarkFileUrl),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -672,10 +673,10 @@ const editingNode = ref<number | null>(null);
|
||||
const editingNodeLocalId = ref<string | null>(null);
|
||||
const nodeForm = reactive<any>({ nodeType: NodeType.CREATE, title: '', occurredAt: null, location: '', description: '', media: [] });
|
||||
function isVideoMedia(m: any) {
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v)$/i.test(m.url || m.key || '');
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v)$/i.test(mediaUrl(m));
|
||||
}
|
||||
function toViewerItem(m: any) {
|
||||
return { url: m.url || m.key, name: m.name || m.key, type: m.type };
|
||||
return { url: mediaUrl(m), name: m.name || m.key, type: m.type };
|
||||
}
|
||||
function previewNodeImage(media: any[] = [], current: any) {
|
||||
const images = (media || []).filter((item) => !isVideoMedia(item) && (item.url || item.key));
|
||||
@@ -683,10 +684,19 @@ function previewNodeImage(media: any[] = [], current: any) {
|
||||
void openMediaViewer(images.map(toViewerItem), Math.max(index, 0));
|
||||
}
|
||||
function previewCert(cert: any) {
|
||||
const url = cert.watermarkUrl || cert.originUrl;
|
||||
const url = certUrl(cert);
|
||||
if (!url) return;
|
||||
void openMediaViewer([{ url, name: cert.name || cert.originKey || cert.watermarkKey }]);
|
||||
}
|
||||
function mediaUrl(item?: any) {
|
||||
return resolveMediaUrl(item?.url || item?.key);
|
||||
}
|
||||
function mediaKey(item?: any) {
|
||||
return item?.key || item?.url || '';
|
||||
}
|
||||
function certUrl(cert?: any) {
|
||||
return resolveMediaUrl(cert?.watermarkUrl || cert?.originUrl || cert?.watermarkKey || cert?.originKey);
|
||||
}
|
||||
function openNode(n?: any) {
|
||||
editingNode.value = n?.id ?? null;
|
||||
editingNodeLocalId.value = n?.localId ?? null;
|
||||
@@ -698,7 +708,7 @@ function openNode(n?: any) {
|
||||
description: n?.description ?? '',
|
||||
media: (n?.media ?? []).map((m: any) => ({
|
||||
key: m.key || m.url,
|
||||
url: m.url,
|
||||
url: resolveMediaUrl(m.url || m.key),
|
||||
name: m.name,
|
||||
type: m.type || (isVideoMedia(m) ? 'video' : 'image'),
|
||||
})),
|
||||
@@ -780,7 +790,7 @@ async function saveCert() {
|
||||
normalizeCert({
|
||||
...payload,
|
||||
localId: nextLocalId('cert'),
|
||||
originUrl: certForm.files[0].url,
|
||||
originUrl: resolveMediaUrl(certForm.files[0].url || certForm.files[0].key),
|
||||
}),
|
||||
);
|
||||
message.success(t('common.msg.created'));
|
||||
|
||||
@@ -52,6 +52,7 @@ import { dialog, message } from '@/utils/feedback';
|
||||
import { renderTableFilterTitle } from '@/utils/tableFilter';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { useRemoteSort } from '@/utils/tableSort';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
const { t } = useI18n();
|
||||
const router = useRouter();
|
||||
@@ -78,7 +79,7 @@ const columns: DataTableColumns<any> = [
|
||||
render: (r) =>
|
||||
r.images?.[0]?.url
|
||||
? h(NAvatar, {
|
||||
src: r.images[0].url,
|
||||
src: mediaUrl(r.images[0]),
|
||||
size: 44,
|
||||
style: 'border-radius:8px;cursor:zoom-in',
|
||||
onClick: () => previewArtworkImages(r.images),
|
||||
@@ -228,9 +229,12 @@ function auditDisplay(r: any) {
|
||||
function goCreate() {
|
||||
router.push('/artworks/new');
|
||||
}
|
||||
function mediaUrl(item: any) {
|
||||
return resolveMediaUrl(item?.url || item?.key);
|
||||
}
|
||||
function previewArtworkImages(images: any[]) {
|
||||
void openMediaViewer(
|
||||
(images || []).map((item) => ({ url: item.url || item.key, name: item.name || item.key, type: item.type || 'image' })),
|
||||
(images || []).map((item) => ({ url: mediaUrl(item), name: item.name || item.key, type: item.type || 'image' })),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ import { useTablePage } from '@/composables/useTablePage';
|
||||
import { renderTableFilterTitle } from '@/utils/tableFilter';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { useRemoteSort } from '@/utils/tableSort';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
const { t } = useI18n();
|
||||
const router = useRouter();
|
||||
@@ -92,7 +93,7 @@ const columns: DataTableColumns<any> = [
|
||||
render: (r) =>
|
||||
r.images?.[0]?.url
|
||||
? h(NAvatar, {
|
||||
src: r.images[0].url,
|
||||
src: mediaUrl(r.images[0]),
|
||||
size: 40,
|
||||
style: 'border-radius:8px;cursor:zoom-in',
|
||||
onClick: () => previewArtworkImages(r.images),
|
||||
@@ -173,9 +174,12 @@ function onSearch() {
|
||||
resetPage();
|
||||
load();
|
||||
}
|
||||
function mediaUrl(item: any) {
|
||||
return resolveMediaUrl(item?.url || item?.key);
|
||||
}
|
||||
function previewArtworkImages(images: any[]) {
|
||||
void openMediaViewer(
|
||||
(images || []).map((item) => ({ url: item.url || item.key, name: item.name || item.key, type: item.type || 'image' })),
|
||||
(images || []).map((item) => ({ url: mediaUrl(item), name: item.name || item.key, type: item.type || 'image' })),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ import { fmt, statusType } from '@/utils/format';
|
||||
import { useTablePage } from '@/composables/useTablePage';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { useRemoteSort } from '@/utils/tableSort';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
const { t } = useI18n();
|
||||
const auth = useAuthStore();
|
||||
@@ -150,7 +151,7 @@ function renderImages(row: any) {
|
||||
width: 38,
|
||||
height: 38,
|
||||
objectFit: 'cover',
|
||||
src: item.url || item.key,
|
||||
src: mediaUrl(item),
|
||||
previewDisabled: true,
|
||||
onClick: () => previewImages(images, index),
|
||||
}),
|
||||
@@ -167,11 +168,15 @@ function renderImages(row: any) {
|
||||
|
||||
function previewImages(images: any[], index: number) {
|
||||
void openMediaViewer(
|
||||
images.map((item) => ({ url: item.url || item.key, name: item.name || item.key, type: item.type || 'image' })),
|
||||
images.map((item) => ({ url: mediaUrl(item), name: item.name || item.key, type: item.type || 'image' })),
|
||||
index,
|
||||
);
|
||||
}
|
||||
|
||||
function mediaUrl(item: any) {
|
||||
return resolveMediaUrl(item?.url || item?.key);
|
||||
}
|
||||
|
||||
async function downloadTemplate() {
|
||||
await downloadFile('/outbound/import/template', 'outbound-import-template.zip');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user