2025-04-17 23:30:34 +08:00
|
|
|
|
import type { Ref } from 'vue';
|
2025-04-22 21:18:10 +08:00
|
|
|
|
|
2025-04-18 18:30:50 +08:00
|
|
|
|
import type { AxiosProgressEvent, InfraFileApi } from '#/api/infra/file';
|
2025-04-17 23:30:34 +08:00
|
|
|
|
|
|
|
|
|
|
import { computed, unref } from 'vue';
|
2025-04-22 21:18:10 +08:00
|
|
|
|
|
|
|
|
|
|
import { useAppConfig } from '@vben/hooks';
|
2025-04-17 23:30:34 +08:00
|
|
|
|
import { $t } from '@vben/locales';
|
2025-04-22 21:18:10 +08:00
|
|
|
|
|
|
|
|
|
|
import { createFile, getFilePresignedUrl, uploadFile } from '#/api/infra/file';
|
2025-04-22 18:49:31 +08:00
|
|
|
|
import { baseRequestClient } from '#/api/request';
|
2025-04-22 21:18:10 +08:00
|
|
|
|
|
|
|
|
|
|
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 上传类型
|
|
|
|
|
|
*/
|
|
|
|
|
|
enum UPLOAD_TYPE {
|
|
|
|
|
|
// 客户端直接上传(只支持S3服务)
|
|
|
|
|
|
CLIENT = 'client',
|
|
|
|
|
|
// 客户端发送到后端上传
|
|
|
|
|
|
SERVER = 'server',
|
|
|
|
|
|
}
|
2025-04-17 23:30:34 +08:00
|
|
|
|
|
2025-12-27 18:50:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 上传类型钩子函数
|
|
|
|
|
|
* @param acceptRef 接受的文件类型
|
|
|
|
|
|
* @param helpTextRef 帮助文本
|
|
|
|
|
|
* @param maxNumberRef 最大文件数量
|
|
|
|
|
|
* @param maxSizeRef 最大文件大小
|
|
|
|
|
|
* @returns 文件类型限制和帮助文本的计算属性
|
|
|
|
|
|
*/
|
2025-04-17 23:30:34 +08:00
|
|
|
|
export function useUploadType({
|
|
|
|
|
|
acceptRef,
|
|
|
|
|
|
helpTextRef,
|
|
|
|
|
|
maxNumberRef,
|
|
|
|
|
|
maxSizeRef,
|
|
|
|
|
|
}: {
|
|
|
|
|
|
acceptRef: Ref<string[]>;
|
|
|
|
|
|
helpTextRef: Ref<string>;
|
|
|
|
|
|
maxNumberRef: Ref<number>;
|
|
|
|
|
|
maxSizeRef: Ref<number>;
|
|
|
|
|
|
}) {
|
|
|
|
|
|
// 文件类型限制
|
|
|
|
|
|
const getAccept = computed(() => {
|
|
|
|
|
|
const accept = unref(acceptRef);
|
|
|
|
|
|
if (accept && accept.length > 0) {
|
|
|
|
|
|
return accept;
|
|
|
|
|
|
}
|
|
|
|
|
|
return [];
|
|
|
|
|
|
});
|
|
|
|
|
|
const getStringAccept = computed(() => {
|
|
|
|
|
|
return unref(getAccept)
|
|
|
|
|
|
.map((item) => {
|
|
|
|
|
|
return item.indexOf('/') > 0 || item.startsWith('.')
|
|
|
|
|
|
? item
|
|
|
|
|
|
: `.${item}`;
|
|
|
|
|
|
})
|
|
|
|
|
|
.join(',');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 支持jpg、jpeg、png格式,不超过2M,最多可选择10张图片,。
|
|
|
|
|
|
const getHelpText = computed(() => {
|
|
|
|
|
|
const helpText = unref(helpTextRef);
|
|
|
|
|
|
if (helpText) {
|
|
|
|
|
|
return helpText;
|
|
|
|
|
|
}
|
|
|
|
|
|
const helpTexts: string[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
const accept = unref(acceptRef);
|
|
|
|
|
|
if (accept.length > 0) {
|
|
|
|
|
|
helpTexts.push($t('ui.upload.accept', [accept.join(',')]));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const maxSize = unref(maxSizeRef);
|
|
|
|
|
|
if (maxSize) {
|
|
|
|
|
|
helpTexts.push($t('ui.upload.maxSize', [maxSize]));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const maxNumber = unref(maxNumberRef);
|
|
|
|
|
|
if (maxNumber && maxNumber !== Infinity) {
|
|
|
|
|
|
helpTexts.push($t('ui.upload.maxNumber', [maxNumber]));
|
|
|
|
|
|
}
|
|
|
|
|
|
return helpTexts.join(',');
|
|
|
|
|
|
});
|
|
|
|
|
|
return { getAccept, getStringAccept, getHelpText };
|
|
|
|
|
|
}
|
2025-04-18 18:30:50 +08:00
|
|
|
|
|
2025-12-27 18:50:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 上传钩子函数
|
|
|
|
|
|
* @param directory 上传目录
|
|
|
|
|
|
* @returns 上传 URL 和自定义上传方法
|
|
|
|
|
|
*/
|
2025-05-26 18:46:06 +08:00
|
|
|
|
export function useUpload(directory?: string) {
|
2025-04-18 18:30:50 +08:00
|
|
|
|
// 后端上传地址
|
2025-04-22 21:18:10 +08:00
|
|
|
|
const uploadUrl = getUploadUrl();
|
2025-04-18 18:30:50 +08:00
|
|
|
|
// 是否使用前端直连上传
|
2025-04-22 21:18:10 +08:00
|
|
|
|
const isClientUpload =
|
|
|
|
|
|
UPLOAD_TYPE.CLIENT === import.meta.env.VITE_UPLOAD_TYPE;
|
2025-04-18 18:30:50 +08:00
|
|
|
|
// 重写ElUpload上传方法
|
2025-05-26 18:46:06 +08:00
|
|
|
|
async function httpRequest(
|
2025-04-22 21:18:10 +08:00
|
|
|
|
file: File,
|
|
|
|
|
|
onUploadProgress?: AxiosProgressEvent,
|
2025-05-26 18:46:06 +08:00
|
|
|
|
) {
|
2025-04-18 18:30:50 +08:00
|
|
|
|
// 模式一:前端上传
|
|
|
|
|
|
if (isClientUpload) {
|
|
|
|
|
|
// 1.1 生成文件名称
|
2025-04-22 21:18:10 +08:00
|
|
|
|
const fileName = await generateFileName(file);
|
2025-04-18 18:30:50 +08:00
|
|
|
|
// 1.2 获取文件预签名地址
|
2025-05-02 19:59:05 +08:00
|
|
|
|
const presignedInfo = await getFilePresignedUrl(fileName, directory);
|
2025-04-18 18:30:50 +08:00
|
|
|
|
// 1.3 上传文件
|
2025-04-22 18:49:31 +08:00
|
|
|
|
return baseRequestClient
|
2025-04-18 18:30:50 +08:00
|
|
|
|
.put(presignedInfo.uploadUrl, file, {
|
|
|
|
|
|
headers: {
|
2025-04-22 21:18:10 +08:00
|
|
|
|
'Content-Type': file.type,
|
|
|
|
|
|
},
|
2025-04-18 18:30:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
.then(() => {
|
|
|
|
|
|
// 1.4. 记录文件信息到后端(异步)
|
2025-05-02 19:59:05 +08:00
|
|
|
|
createFile0(presignedInfo, file);
|
2025-04-18 18:30:50 +08:00
|
|
|
|
// 通知成功,数据格式保持与后端上传的返回结果一致
|
2025-05-02 20:42:54 +08:00
|
|
|
|
return { url: presignedInfo.url };
|
2025-04-22 21:18:10 +08:00
|
|
|
|
});
|
2025-04-18 18:30:50 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 模式二:后端上传
|
2025-05-02 19:59:05 +08:00
|
|
|
|
return uploadFile({ file, directory }, onUploadProgress);
|
2025-04-18 18:30:50 +08:00
|
|
|
|
}
|
2025-05-26 18:46:06 +08:00
|
|
|
|
}
|
2025-04-18 18:30:50 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
uploadUrl,
|
2025-04-22 21:18:10 +08:00
|
|
|
|
httpRequest,
|
|
|
|
|
|
};
|
2025-05-26 18:46:06 +08:00
|
|
|
|
}
|
2025-04-18 18:30:50 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获得上传 URL
|
|
|
|
|
|
*/
|
2025-05-26 18:46:06 +08:00
|
|
|
|
export function getUploadUrl(): string {
|
2025-04-22 21:18:10 +08:00
|
|
|
|
return `${apiURL}/infra/file/upload`;
|
2025-05-26 18:46:06 +08:00
|
|
|
|
}
|
2025-04-18 18:30:50 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建文件信息
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param vo 文件预签名信息
|
|
|
|
|
|
* @param file 文件
|
|
|
|
|
|
*/
|
2025-05-26 18:46:06 +08:00
|
|
|
|
function createFile0(
|
2025-09-22 09:59:22 +08:00
|
|
|
|
vo: InfraFileApi.FilePresignedUrlRespVO,
|
2025-05-26 18:46:06 +08:00
|
|
|
|
file: File,
|
|
|
|
|
|
): InfraFileApi.File {
|
2025-04-18 18:30:50 +08:00
|
|
|
|
const fileVO = {
|
|
|
|
|
|
configId: vo.configId,
|
|
|
|
|
|
url: vo.url,
|
2025-05-02 19:59:05 +08:00
|
|
|
|
path: vo.path,
|
2025-04-18 18:30:50 +08:00
|
|
|
|
name: file.name,
|
|
|
|
|
|
type: file.type,
|
2025-04-22 21:18:10 +08:00
|
|
|
|
size: file.size,
|
|
|
|
|
|
};
|
|
|
|
|
|
createFile(fileVO);
|
|
|
|
|
|
return fileVO;
|
2025-04-18 18:30:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 生成文件名称(使用算法SHA256)
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param file 要上传的文件
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function generateFileName(file: File) {
|
2025-05-02 19:59:05 +08:00
|
|
|
|
// // 读取文件内容
|
|
|
|
|
|
// const data = await file.arrayBuffer();
|
|
|
|
|
|
// const wordArray = CryptoJS.lib.WordArray.create(data);
|
|
|
|
|
|
// // 计算SHA256
|
|
|
|
|
|
// const sha256 = CryptoJS.SHA256(wordArray).toString();
|
|
|
|
|
|
// // 拼接后缀
|
|
|
|
|
|
// const ext = file.name.slice(Math.max(0, file.name.lastIndexOf('.')));
|
|
|
|
|
|
// return `${sha256}${ext}`;
|
|
|
|
|
|
return file.name;
|
2025-04-22 22:10:33 +08:00
|
|
|
|
}
|