|
1 | 1 | import type { UploadProps, UploadRawFile } from 'element-plus'
|
2 | 2 | import { getAccessToken } from '@/utils/auth'
|
3 |
| -const message = useMessage() // 消息 |
| 3 | +import { MaterialType, useBeforeUpload } from '@/views/mp/hooks/useUpload' |
| 4 | + |
4 | 5 | const HEADERS = { Authorization: 'Bearer ' + getAccessToken() } // 请求头
|
5 | 6 | const UPLOAD_URL = import.meta.env.VITE_BASE_URL + '/admin-api/mp/material/upload-permanent' // 上传地址
|
6 | 7 |
|
7 |
| -enum MaterialType { |
8 |
| - Image = 'image', |
9 |
| - Voice = 'voice', |
10 |
| - Video = 'video' |
11 |
| -} |
12 |
| - |
13 | 8 | interface UploadData {
|
14 | 9 | type: MaterialType
|
15 | 10 | title: string
|
16 | 11 | introduction: string
|
17 | 12 | }
|
18 | 13 |
|
19 |
| -const beforeUpload = (rawFile: UploadRawFile, materialType: MaterialType): boolean => { |
20 |
| - let allowTypes: string[] = [] |
21 |
| - let maxSizeMB = 0 |
22 |
| - let name = '' |
23 |
| - switch (materialType) { |
24 |
| - case MaterialType.Image: |
25 |
| - allowTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/jpg'] |
26 |
| - maxSizeMB = 2 |
27 |
| - name = '图片' |
28 |
| - break |
29 |
| - case MaterialType.Voice: |
30 |
| - allowTypes = ['audio/mp3', 'audio/mpeg', 'audio/wma', 'audio/wav', 'audio/amr'] |
31 |
| - maxSizeMB = 2 |
32 |
| - name = '图片' |
33 |
| - break |
34 |
| - case MaterialType.Video: |
35 |
| - allowTypes = ['video/mp4'] |
36 |
| - maxSizeMB = 10 |
37 |
| - name = '视频' |
38 |
| - break |
39 |
| - } |
40 |
| - // 格式不正确 |
41 |
| - if (!allowTypes.includes(rawFile.type)) { |
42 |
| - message.error(`上传${name}格式不对!`) |
43 |
| - return false |
44 |
| - } |
45 |
| - // 大小不正确 |
46 |
| - if (rawFile.size / 1024 / 1024 > maxSizeMB) { |
47 |
| - message.error(`上传${name}大小不能超过${maxSizeMB}M!`) |
48 |
| - return false |
49 |
| - } |
50 |
| - return true |
51 |
| -} |
52 |
| - |
53 | 14 | const beforeImageUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) =>
|
54 |
| - beforeUpload(rawFile, MaterialType.Image) |
| 15 | + useBeforeUpload(MaterialType.Image, 2)(rawFile) |
55 | 16 |
|
56 | 17 | const beforeVoiceUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) =>
|
57 |
| - beforeUpload(rawFile, MaterialType.Voice) |
| 18 | + useBeforeUpload(MaterialType.Voice, 2)(rawFile) |
58 | 19 |
|
59 | 20 | const beforeVideoUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) =>
|
60 |
| - beforeUpload(rawFile, MaterialType.Video) |
| 21 | + useBeforeUpload(MaterialType.Video, 10)(rawFile) |
61 | 22 |
|
62 | 23 | export {
|
63 | 24 | HEADERS,
|
|
0 commit comments