diff --git a/src/components/UploadFile/src/useUpload.ts b/src/components/UploadFile/src/useUpload.ts index 2981e12..356da3a 100644 --- a/src/components/UploadFile/src/useUpload.ts +++ b/src/components/UploadFile/src/useUpload.ts @@ -1,4 +1,5 @@ import * as FileApi from '@/api/infra/file' +import { QualityCollectionApi } from '@/api/airqualitycollection' import CryptoJS from 'crypto-js' import { UploadRawFile, UploadRequestOptions } from 'element-plus/es/components/upload/src/upload' import axios from 'axios' @@ -10,6 +11,10 @@ export const getUploadUrl = (): string => { return import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload' } +export const getImportExcel = (): string => { + return import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/quality-collection/upload-excel' +} + export const useUpload = () => { // 后端上传地址 const uploadUrl = getUploadUrl() @@ -61,6 +66,59 @@ export const useUpload = () => { } } + +export const useExcelUpload = (type: any) => { + //excel 上传地址 + const uploadUrl = getImportExcel() + // 是否使用前端直连上传 + const isClientUpload = UPLOAD_TYPE.CLIENT === import.meta.env.VITE_UPLOAD_TYPE + // 重写ElUpload上传方法 + const httpRequest = async (options: UploadRequestOptions) => { + // 模式一:前端上传 + if (isClientUpload) { + // 1.1 生成文件名称 + const fileName = await generateFileName(options.file) + // 1.2 获取文件预签名地址 + const presignedInfo = await FileApi.getFilePresignedUrl(fileName) + // 1.3 上传文件(不能使用 ElUpload 的 ajaxUpload 方法的原因:其使用的是 FormData 上传,Minio 不支持) + return axios + .put(presignedInfo.uploadUrl, options.file, { + headers: { + 'Content-Type': options.file.type + } + }) + .then(() => { + // 1.4. 记录文件信息到后端(异步) + createFile(presignedInfo, fileName, options.file) + // 通知成功,数据格式保持与后端上传的返回结果一致 + return { data: presignedInfo.url } + }) + } else { + // 模式二:后端上传 + // 重写 el-upload httpRequest 文件上传成功会走成功的钩子,失败走失败的钩子 + + return new Promise((resolve, reject) => { + QualityCollectionApi.exportQualityCollection({ file: options.file, type: type }) + .then((res) => { + if (res.code === 0) { + resolve(res) + } else { + reject(res) + } + }) + .catch((res) => { + reject(res) + }) + }) + } + } + + return { + uploadUrl, + httpRequest + } +} + /** * 创建文件信息 * @param vo 文件预签名信息 diff --git a/vite.config.ts b/vite.config.ts index 574d56f..9f936a6 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -27,7 +27,7 @@ export default ({command, mode}: ConfigEnv): UserConfig => { // 服务端渲染 server: { port: env.VITE_PORT, // 端口号 - host: "0.0.0.0", + // host: "0.0.0.0", open: env.VITE_OPEN === 'true', // 本地跨域代理. 目前注释的原因:暂时没有用途,server 端已经支持跨域 // proxy: {