mirror of
https://gitee.com/hhyykk/ipms-sjy-ui.git
synced 2025-07-14 10:55:06 +08:00
refactor: MP/wx-reply组件,抽离useUpload钩子
This commit is contained in:
50
src/views/mp/hooks/useUpload.ts
Normal file
50
src/views/mp/hooks/useUpload.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import type { UploadRawFile } from 'element-plus'
|
||||
|
||||
const message = useMessage() // 消息
|
||||
|
||||
enum MaterialType {
|
||||
Image = 'image',
|
||||
Voice = 'voice',
|
||||
Video = 'video'
|
||||
}
|
||||
|
||||
const useBeforeUpload = (type: MaterialType, maxSizeMB: number) => {
|
||||
const fn = (rawFile: UploadRawFile): boolean => {
|
||||
let allowTypes: string[] = []
|
||||
let name = ''
|
||||
|
||||
switch (type) {
|
||||
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
|
||||
}
|
||||
|
||||
return fn
|
||||
}
|
||||
|
||||
export { MaterialType, useBeforeUpload }
|
Reference in New Issue
Block a user