From a4172c364336dcef3f39f755c08378fcc8232be9 Mon Sep 17 00:00:00 2001 From: dhb52 Date: Tue, 18 Apr 2023 11:19:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8useUpload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit cddf1ee87894051a0fc20cf93049145cdfb2ba39) --- src/views/mp/material/components/upload.ts | 49 ++----------------- .../mp/menu/components/MenuPreviewer.vue | 2 +- 2 files changed, 6 insertions(+), 45 deletions(-) diff --git a/src/views/mp/material/components/upload.ts b/src/views/mp/material/components/upload.ts index ac6f3a1f..dc52a9eb 100644 --- a/src/views/mp/material/components/upload.ts +++ b/src/views/mp/material/components/upload.ts @@ -1,63 +1,24 @@ import type { UploadProps, UploadRawFile } from 'element-plus' import { getAccessToken } from '@/utils/auth' -const message = useMessage() // 消息 +import { MaterialType, useBeforeUpload } from '@/views/mp/hooks/useUpload' + const HEADERS = { Authorization: 'Bearer ' + getAccessToken() } // 请求头 const UPLOAD_URL = import.meta.env.VITE_BASE_URL + '/admin-api/mp/material/upload-permanent' // 上传地址 -enum MaterialType { - Image = 'image', - Voice = 'voice', - Video = 'video' -} - interface UploadData { type: MaterialType title: string introduction: string } -const beforeUpload = (rawFile: UploadRawFile, materialType: MaterialType): boolean => { - let allowTypes: string[] = [] - let maxSizeMB = 0 - let name = '' - switch (materialType) { - case MaterialType.Image: - allowTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/jpg'] - maxSizeMB = 2 - name = '图片' - break - case MaterialType.Voice: - allowTypes = ['audio/mp3', 'audio/mpeg', 'audio/wma', 'audio/wav', 'audio/amr'] - maxSizeMB = 2 - name = '图片' - break - case MaterialType.Video: - allowTypes = ['video/mp4'] - maxSizeMB = 10 - name = '视频' - break - } - // 格式不正确 - if (!allowTypes.includes(rawFile.type)) { - message.error(`上传${name}格式不对!`) - return false - } - // 大小不正确 - if (rawFile.size / 1024 / 1024 > maxSizeMB) { - message.error(`上传${name}大小不能超过${maxSizeMB}M!`) - return false - } - return true -} - const beforeImageUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) => - beforeUpload(rawFile, MaterialType.Image) + useBeforeUpload(MaterialType.Image, 2)(rawFile) const beforeVoiceUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) => - beforeUpload(rawFile, MaterialType.Voice) + useBeforeUpload(MaterialType.Voice, 2)(rawFile) const beforeVideoUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) => - beforeUpload(rawFile, MaterialType.Video) + useBeforeUpload(MaterialType.Video, 10)(rawFile) export { HEADERS, diff --git a/src/views/mp/menu/components/MenuPreviewer.vue b/src/views/mp/menu/components/MenuPreviewer.vue index 9fb45008..aca1c2ae 100644 --- a/src/views/mp/menu/components/MenuPreviewer.vue +++ b/src/views/mp/menu/components/MenuPreviewer.vue @@ -14,7 +14,7 @@