Merge branch 'developer' of ssh://code.cyweb.top:30033/scj/zhxy/v3/cloud-ui into developer
This commit is contained in:
@@ -161,6 +161,7 @@ interface FileItem {
|
||||
name?: string;
|
||||
url?: string;
|
||||
uid?: number;
|
||||
id?: string; // 文件ID
|
||||
}
|
||||
|
||||
interface UploadFileItem {
|
||||
@@ -170,6 +171,7 @@ interface UploadFileItem {
|
||||
fileSize: number;
|
||||
fileName: string;
|
||||
fileType: string;
|
||||
id?: string; // 文件ID
|
||||
}
|
||||
|
||||
const props = defineProps({
|
||||
@@ -270,14 +272,74 @@ const handleBeforeUpload = (file: File) => {
|
||||
// 上传成功回调
|
||||
function handleUploadSuccess(res: any, file: any) {
|
||||
if (res.code === 0) {
|
||||
// 调试:打印完整的响应数据
|
||||
console.log('上传成功响应数据:', res);
|
||||
console.log('res.data:', res.data);
|
||||
|
||||
// 获取文件ID - 尝试多种可能的字段名
|
||||
let fileId = res.data?.id || res.data?.fileId || res.data?.file_id || null;
|
||||
let fileUrl = res.data?.url || res.data?.fileUrl || '';
|
||||
|
||||
// 如果fileId不存在,尝试从fileName中提取(fileName可能是ID)
|
||||
if (!fileId && res.data?.fileName) {
|
||||
const fileName = res.data.fileName;
|
||||
// 检查fileName是否是32位十六进制字符串(文件ID格式)
|
||||
if (/^[a-f0-9]{32}$/i.test(fileName)) {
|
||||
fileId = fileName;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果fileId仍然不存在,尝试从URL中提取
|
||||
if (!fileId && fileUrl) {
|
||||
try {
|
||||
const urlObj = new URL(fileUrl, window.location.origin);
|
||||
fileId = urlObj.searchParams.get('id');
|
||||
// 如果URL参数中没有id,检查路径中是否有32位十六进制字符串
|
||||
if (!fileId) {
|
||||
const pathParts = urlObj.pathname.split('/').filter(p => p);
|
||||
const lastPart = pathParts[pathParts.length - 1];
|
||||
if (lastPart && /^[a-f0-9]{32}$/i.test(lastPart)) {
|
||||
fileId = lastPart;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// URL解析失败,忽略
|
||||
}
|
||||
}
|
||||
|
||||
// 如果仍然没有fileId,尝试从整个res.data中查找可能的ID字段
|
||||
if (!fileId && res.data) {
|
||||
// 遍历res.data的所有属性,查找32位十六进制字符串
|
||||
for (const key in res.data) {
|
||||
const value = res.data[key];
|
||||
if (typeof value === 'string' && /^[a-f0-9]{32}$/i.test(value)) {
|
||||
fileId = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('提取的文件ID:', fileId);
|
||||
|
||||
// 构建URL,如果存在id则添加到URL参数中(用于兼容性)
|
||||
if (fileId && fileUrl) {
|
||||
// 如果URL中已经有参数,使用&,否则使用?
|
||||
fileUrl = fileUrl.includes('?') ? `${fileUrl}&id=${fileId}` : `${fileUrl}?id=${fileId}`;
|
||||
}
|
||||
fileUrl = `${fileUrl}&originalFileName=${file.name}`;
|
||||
|
||||
uploadList.value.push({
|
||||
name: file.name,
|
||||
url: `${res.data?.url}&originalFileName=${file.name}`,
|
||||
url: fileUrl,
|
||||
fileUrl: res.data?.fileName,
|
||||
fileSize: file.size,
|
||||
fileName: file.name,
|
||||
fileType: file.raw.type,
|
||||
id: fileId, // 保存文件ID(优先使用)
|
||||
});
|
||||
|
||||
console.log('保存的文件对象:', uploadList.value[uploadList.value.length - 1]);
|
||||
|
||||
uploadedSuccessfully();
|
||||
} else {
|
||||
number.value--;
|
||||
@@ -298,10 +360,12 @@ const uploadedSuccessfully = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemove = (file: { name: string }) => {
|
||||
fileList.value = fileList.value.filter((f) => f.name !== file.name);
|
||||
emit('update:modelValue', listToString(fileList.value));
|
||||
emit('change', listToString(fileList.value), fileList.value);
|
||||
const handleRemove = (file: { name?: string }) => {
|
||||
if (file.name) {
|
||||
fileList.value = fileList.value.filter((f) => f.name !== file.name);
|
||||
emit('update:modelValue', listToString(fileList.value));
|
||||
emit('change', listToString(fileList.value), fileList.value);
|
||||
}
|
||||
};
|
||||
|
||||
const handlePreview = (file: any) => {
|
||||
@@ -319,12 +383,21 @@ const handleExceed = () => {
|
||||
* @param separator 分隔符,默认为逗号。
|
||||
* @returns {string} 返回转换后的字符串。
|
||||
*/
|
||||
/**
|
||||
* 将对象数组转为字符串,以逗号分隔。
|
||||
* 如果文件有id,优先使用id,否则使用url。
|
||||
* @param list 待转换的对象数组。
|
||||
* @param separator 分隔符,默认为逗号。
|
||||
* @returns {string} 返回转换后的字符串(ID或URL,用逗号分隔)。
|
||||
*/
|
||||
const listToString = (list: FileItem[], separator = ','): string => {
|
||||
let strs = '';
|
||||
separator = separator || ',';
|
||||
for (let i in list) {
|
||||
if (list[i].url) {
|
||||
strs += list[i].url + separator;
|
||||
// 优先使用id,如果没有id则使用url
|
||||
const value = list[i].id || list[i].url;
|
||||
if (value) {
|
||||
strs += value + separator;
|
||||
}
|
||||
}
|
||||
return strs !== '' ? strs.substr(0, strs.length - 1) : '';
|
||||
@@ -347,7 +420,28 @@ watch(
|
||||
// 然后将数组转为对象数组
|
||||
fileList.value = list.map((item: any) => {
|
||||
if (typeof item === 'string') {
|
||||
item = { name: other.getQueryString(item, 'originalFileName') || other.getQueryString(item, 'fileName'), url: item };
|
||||
// 检查是否是32位十六进制字符串(文件ID格式)
|
||||
const isId = /^[a-f0-9]{32}$/i.test(item.trim());
|
||||
if (isId) {
|
||||
// 如果是ID格式,直接使用
|
||||
item = { id: item.trim(), name: '', url: '' };
|
||||
} else {
|
||||
// 如果是URL,尝试从URL中提取id
|
||||
try {
|
||||
const urlObj = new URL(item, window.location.origin);
|
||||
const id = urlObj.searchParams.get('id');
|
||||
item = {
|
||||
name: other.getQueryString(item, 'originalFileName') || other.getQueryString(item, 'fileName'),
|
||||
url: item,
|
||||
id: id || undefined
|
||||
};
|
||||
} catch {
|
||||
item = {
|
||||
name: other.getQueryString(item, 'originalFileName') || other.getQueryString(item, 'fileName'),
|
||||
url: item
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
item.uid = item.uid || new Date().getTime() + temp++;
|
||||
return item as FileItem;
|
||||
|
||||
Reference in New Issue
Block a user