feat: 更新上传功能,返回文件ID和公共URL;优化实名认证表单数据处理
This commit is contained in:
@@ -9,21 +9,22 @@ interface UploadOptions {
|
||||
|
||||
export type UploadFetchOptions = TreatyBody<typeof client.api.file_storage.upload_url.post>;
|
||||
|
||||
export async function uploadToS3(file: File, options: UploadOptions): Promise<string> {
|
||||
export async function uploadToS3(file: File, options: UploadOptions): Promise<{ fileId: string; publicUrl: string }> {
|
||||
const { onProgress, signal, fetchOptions } = options;
|
||||
|
||||
// 1. 获取预签名 URL
|
||||
const { data, error } = await safeClient(client.api.file_storage.upload_url.post({
|
||||
...fetchOptions,
|
||||
accessControl: "public",
|
||||
}));
|
||||
|
||||
if (error.value || !data.value) {
|
||||
throw new Error("获取上传 URL 失败");
|
||||
}
|
||||
const { fileId, uploadUrl, method, headers } = toRefs(data.value);
|
||||
const { fileId, uploadUrl, method, headers, publicUrl } = toRefs(data.value);
|
||||
|
||||
// 2. 上传文件到 S3
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise<{ fileId: string; publicUrl: string }>((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
// 监听上传进度
|
||||
@@ -37,7 +38,10 @@ export async function uploadToS3(file: File, options: UploadOptions): Promise<st
|
||||
// 上传成功
|
||||
xhr.addEventListener("load", () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve(fileId.value);
|
||||
resolve({
|
||||
fileId: fileId.value,
|
||||
publicUrl: publicUrl!.value!,
|
||||
});
|
||||
}
|
||||
else {
|
||||
reject(new Error(`上传失败: ${xhr.status} ${xhr.statusText}`));
|
||||
|
||||
Reference in New Issue
Block a user