模块移植:场地预约、理论学习()
This commit is contained in:
@@ -322,29 +322,51 @@ module.exports = {
|
||||
return true
|
||||
}
|
||||
|
||||
// 去除前后空格并转换为数组
|
||||
const acceptTypes = this.fileAccept
|
||||
.trim()
|
||||
.split(",")
|
||||
.map((type) => type.trim().toUpperCase())
|
||||
|
||||
let fileType
|
||||
const dotIndex = file.name.lastIndexOf(".")
|
||||
if (dotIndex !== -1) {
|
||||
fileType = file.name.substring(dotIndex).toUpperCase()
|
||||
} else {
|
||||
this.$message.error("文件必须包含有效的扩展名!")
|
||||
return false
|
||||
}
|
||||
|
||||
const valid = acceptTypes.includes(fileType)
|
||||
const valid = this.isAcceptFile(file)
|
||||
if (valid) {
|
||||
return true
|
||||
}
|
||||
this.$message.error(`文件只能是 ${this.fileAccept} 格式!`)
|
||||
this.$message.error("文件只能是 " + this.fileAccept + " 格式!")
|
||||
return false
|
||||
},
|
||||
|
||||
// 兼容 accept="video/*" 这类 MIME 通配写法,同时保留原有扩展名白名单校验。
|
||||
isAcceptFile(file) {
|
||||
if (!this.fileAccept || typeof this.fileAccept !== "string") {
|
||||
return true
|
||||
}
|
||||
const acceptTypes = this.getAcceptTypes()
|
||||
const fileName = file.name || ""
|
||||
const dotIndex = fileName.lastIndexOf(".")
|
||||
const fileType = dotIndex !== -1 ? fileName.substring(dotIndex).toUpperCase() : ""
|
||||
const mimeType = (file.type || "").toUpperCase()
|
||||
|
||||
if (!fileType) {
|
||||
this.$message.error("文件必须包含有效的扩展名!")
|
||||
return false
|
||||
}
|
||||
|
||||
return acceptTypes.some((acceptType) => {
|
||||
if (acceptType.endsWith("/*")) {
|
||||
return mimeType.startsWith(acceptType.substring(0, acceptType.length - 1)) || this.matchVideoWildcard(acceptType, fileType)
|
||||
}
|
||||
return acceptType === fileType
|
||||
})
|
||||
},
|
||||
|
||||
// 部分浏览器或系统无法识别视频 MIME,此处用视频后缀兜底保证 video/* 可覆盖常见视频格式。
|
||||
matchVideoWildcard(acceptType, fileType) {
|
||||
const videoExtTypes = [".FLV", ".SWF", ".MKV", ".AVI", ".RM", ".RMVB", ".WMV", ".MP4", ".MOV", ".MPG", ".MPEG", ".WEBM", ".M4V", ".3GP", ".3G2", ".TS", ".MTS", ".M2TS", ".OGV"]
|
||||
return acceptType === "VIDEO/*" && videoExtTypes.includes(fileType)
|
||||
},
|
||||
|
||||
getAcceptTypes() {
|
||||
return this.fileAccept
|
||||
.trim()
|
||||
.split(",")
|
||||
.map((type) => type.trim().toUpperCase())
|
||||
},
|
||||
|
||||
// 预览图片
|
||||
handlePreview(file) {
|
||||
const suffix = file.name.substring(file.name.lastIndexOf(".") + 1).toLowerCase()
|
||||
@@ -571,24 +593,13 @@ module.exports = {
|
||||
}
|
||||
|
||||
if (this.fileAccept) {
|
||||
// 去除前后空格并转换为数组
|
||||
const acceptTypes = this.fileAccept
|
||||
.trim()
|
||||
.split(",")
|
||||
.map((type) => type.trim().toUpperCase())
|
||||
|
||||
const validFiles = data.files.filter((file) => {
|
||||
let fileType
|
||||
const dotIndex = file.name.lastIndexOf(".")
|
||||
if (dotIndex !== -1) {
|
||||
fileType = file.name.substring(dotIndex).toUpperCase()
|
||||
}
|
||||
return acceptTypes.includes(fileType)
|
||||
return this.isAcceptFile(file)
|
||||
})
|
||||
|
||||
if (validFiles.length < data.files.length) {
|
||||
data.files = validFiles
|
||||
this.$message.error(`文件只能是 ${this.fileAccept} 格式,已自动过滤掉不符合要求文件!`)
|
||||
this.$message.error("文件只能是 " + this.fileAccept + " 格式,已自动过滤掉不符合要求文件!")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user