优化图片上传逻辑,统一使用fileURL和fileAddressPrefix字段
This commit is contained in:
@@ -553,13 +553,13 @@ const imageFileList = ref<UploadUserFile[]>([]);
|
||||
const dialogVisible = ref(false);
|
||||
const dialogImageUrl = ref('');
|
||||
// 图片拼接
|
||||
const imgAddressPrefix = ref('');
|
||||
const fileAddressPrefix = ref('');
|
||||
|
||||
const formatImageUrl = (url?: string) => {
|
||||
if (!url) return '';
|
||||
if (/^https?:\/\//i.test(url)) return url;
|
||||
if (/^blob:/i.test(url)) return url; // 支持本地预览地址
|
||||
return `${imgAddressPrefix.value || ''}${url}`;
|
||||
return `${fileAddressPrefix.value || ''}${url}`;
|
||||
};
|
||||
|
||||
const createDefaultTimeSlots = (): TimeSlot[] => {
|
||||
@@ -827,7 +827,7 @@ const resetForm = () => {
|
||||
mainImagePreview.value = '';
|
||||
imageFileList.value = [];
|
||||
categoryAttrs.value = [];
|
||||
imgAddressPrefix.value = '';
|
||||
fileAddressPrefix.value = '';
|
||||
};
|
||||
|
||||
// 获取分类数据
|
||||
@@ -879,7 +879,8 @@ const openDialog = (row?: any, edit?: boolean) => {
|
||||
getAsset(row.id)
|
||||
.then((res: any) => {
|
||||
const data = res.data;
|
||||
imgAddressPrefix.value = data.imgAddressPrefix || '';
|
||||
// 支持 fileAddressPrefix 和 imgAddressPrefix
|
||||
fileAddressPrefix.value = data.fileAddressPrefix || data.imgAddressPrefix || '';
|
||||
ruleForm.id = data.id || '';
|
||||
ruleForm.name = data.name || '';
|
||||
ruleForm.type = data.type || 'physical';
|
||||
@@ -888,10 +889,11 @@ const openDialog = (row?: any, edit?: boolean) => {
|
||||
ruleForm.onlineTime = data.onlineTime || '';
|
||||
ruleForm.offlineTime = data.offlineTime || '';
|
||||
|
||||
// 主图预览
|
||||
if (data.imageUrl) {
|
||||
mainImagePreview.value = formatImageUrl(data.imageUrl);
|
||||
ruleForm.mainImage = data.imageUrl;
|
||||
// 主图预览 (支持 imageUrl 和 fileURL)
|
||||
const mainImg = data.imageUrl || data.fileURL;
|
||||
if (mainImg) {
|
||||
mainImagePreview.value = formatImageUrl(mainImg);
|
||||
ruleForm.mainImage = mainImg;
|
||||
}
|
||||
|
||||
// 图片列表
|
||||
@@ -899,6 +901,7 @@ const openDialog = (row?: any, edit?: boolean) => {
|
||||
imageFileList.value = data.images.map((url: string, index: number) => ({
|
||||
name: `image-${index}`,
|
||||
url: formatImageUrl(url),
|
||||
response: url, // 存储原始相对路径,供提交时使用
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -1039,7 +1042,29 @@ const onCancel = () => {
|
||||
// 上传图片并返回URL
|
||||
const uploadImage = async (file: File): Promise<string> => {
|
||||
const res: any = await uploadAssetImage(file);
|
||||
return res.data?.url || res.data || '';
|
||||
|
||||
// 1. 尝试获取并设置 fileAddressPrefix
|
||||
// 优先检查顶层,再检查 data 内部
|
||||
if (res.fileAddressPrefix) {
|
||||
fileAddressPrefix.value = res.fileAddressPrefix;
|
||||
} else if (res.data && typeof res.data === 'object' && res.data.fileAddressPrefix) {
|
||||
fileAddressPrefix.value = res.data.fileAddressPrefix;
|
||||
}
|
||||
|
||||
// 2. 尝试获取 fileURL / url
|
||||
// 优先检查顶层 fileURL
|
||||
if (res.fileURL) return res.fileURL;
|
||||
|
||||
// 检查 data 对象中的 fileURL 或 url
|
||||
if (res.data && typeof res.data === 'object') {
|
||||
if (res.data.fileURL) return res.data.fileURL;
|
||||
if (res.data.url) return res.data.url;
|
||||
}
|
||||
|
||||
// 3. 兼容旧逻辑 (data 直接是 url 字符串)
|
||||
if (typeof res.data === 'string') return res.data;
|
||||
|
||||
return '';
|
||||
};
|
||||
|
||||
// 构建请求体
|
||||
@@ -1066,7 +1091,7 @@ const buildRequestBody = async (): Promise<any> => {
|
||||
|
||||
// 主图 (已在上传时直接赋值给 ruleForm.mainImage)
|
||||
if (ruleForm.mainImage) {
|
||||
body.imageUrl = ruleForm.mainImage;
|
||||
body.fileURL = ruleForm.mainImage;
|
||||
}
|
||||
|
||||
// 图片列表
|
||||
|
||||
Reference in New Issue
Block a user