From 32d58924225605ed8e2533f728b70bf8186e1130 Mon Sep 17 00:00:00 2001 From: puhui999 Date: Tue, 27 Feb 2024 23:34:32 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=20el-upload=20httpRequest=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=88=90=E5=8A=9F=E4=BC=9A?= =?UTF-8?q?=E8=B5=B0=E6=88=90=E5=8A=9F=E7=9A=84=E9=92=A9=E5=AD=90=EF=BC=8C?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E8=B5=B0=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=92=A9?= =?UTF-8?q?=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/infra/file/index.ts | 5 +++++ src/components/UploadFile/src/useUpload.ts | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/api/infra/file/index.ts b/src/api/infra/file/index.ts index 87d966e1..0e1b2e7c 100644 --- a/src/api/infra/file/index.ts +++ b/src/api/infra/file/index.ts @@ -38,3 +38,8 @@ export const getFilePresignedUrl = (path: string) => { export const createFile = (data: any) => { return request.post({ url: '/infra/file/create', data }) } + +// 上传文件 +export const updateFile = (data: any) => { + return request.upload({ url: '/infra/file/upload', data }) +} diff --git a/src/components/UploadFile/src/useUpload.ts b/src/components/UploadFile/src/useUpload.ts index 0c6d49db..cadad9e9 100644 --- a/src/components/UploadFile/src/useUpload.ts +++ b/src/components/UploadFile/src/useUpload.ts @@ -1,8 +1,6 @@ -import { getAccessToken, getTenantId } from '@/utils/auth' import * as FileApi from '@/api/infra/file' import CryptoJS from 'crypto-js' import { UploadRawFile, UploadRequestOptions } from 'element-plus/es/components/upload/src/upload' -import { ajaxUpload } from 'element-plus/es/components/upload/src/ajax' import axios from 'axios' export const useUpload = () => { @@ -26,11 +24,21 @@ export const useUpload = () => { return { data: presignedInfo.url } }) } else { - // 模式二:后端上传(需要增加后端身份认证请求头) - options.headers['Authorization'] = 'Bearer ' + getAccessToken() - options.headers['tenant-id'] = getTenantId() - // 使用 ElUpload 的上传方法 - return ajaxUpload(options) + // 模式二:后端上传 + // 重写 el-upload httpRequest 文件上传成功会走成功的钩子,失败走失败的钩子 + return new Promise((resolve, reject) => { + FileApi.updateFile({ file: options.file }) + .then((res) => { + if (res.code === 0) { + resolve(res) + } else { + reject(res) + } + }) + .catch((res) => { + reject(res) + }) + }) } }