sadais-piui 1.2.7-beta149 → 1.2.7-beta150
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -442,6 +442,13 @@ export default {
|
|
|
442
442
|
default() {
|
|
443
443
|
return uploadImg.customTakePhoto
|
|
444
444
|
}
|
|
445
|
+
},
|
|
446
|
+
// 文件大小限制,单位为字节,null表示不限制
|
|
447
|
+
fileLimit: {
|
|
448
|
+
type: [Number, String, null],
|
|
449
|
+
default() {
|
|
450
|
+
return uploadImg.fileLimit
|
|
451
|
+
}
|
|
445
452
|
}
|
|
446
453
|
},
|
|
447
454
|
data() {
|
|
@@ -724,6 +731,135 @@ export default {
|
|
|
724
731
|
}
|
|
725
732
|
})
|
|
726
733
|
},
|
|
734
|
+
// 检查文件大小限制
|
|
735
|
+
checkFileSize(files) {
|
|
736
|
+
if (!this.fileLimit) return { valid: true, files }
|
|
737
|
+
|
|
738
|
+
const limitBytes = this.parseFileSize(this.fileLimit)
|
|
739
|
+
const validFiles = []
|
|
740
|
+
const oversizedFiles = []
|
|
741
|
+
|
|
742
|
+
for (const file of files) {
|
|
743
|
+
// 对于不同的文件对象,获取文件大小的方式可能不同
|
|
744
|
+
let fileSize = 0
|
|
745
|
+
if (file && file.size !== undefined) {
|
|
746
|
+
// 标准的 File 对象或有 size 属性的对象
|
|
747
|
+
fileSize = file.size
|
|
748
|
+
} else if (file && file.tempFile && file.tempFile.size !== undefined) {
|
|
749
|
+
// uni-app 的 tempFile 格式
|
|
750
|
+
fileSize = file.tempFile.size
|
|
751
|
+
} else if (typeof file === 'string') {
|
|
752
|
+
// 如果是纯路径字符串,无法获取文件大小,跳过检查
|
|
753
|
+
validFiles.push(file)
|
|
754
|
+
continue
|
|
755
|
+
} else {
|
|
756
|
+
// 如果无法获取文件大小,跳过检查
|
|
757
|
+
validFiles.push(file)
|
|
758
|
+
continue
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
if (fileSize > limitBytes) {
|
|
762
|
+
oversizedFiles.push({
|
|
763
|
+
file,
|
|
764
|
+
size: fileSize,
|
|
765
|
+
humanSize: this.formatFileSize(fileSize)
|
|
766
|
+
})
|
|
767
|
+
} else {
|
|
768
|
+
validFiles.push(file)
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
if (oversizedFiles.length > 0) {
|
|
773
|
+
const limitHumanSize = this.formatFileSize(limitBytes)
|
|
774
|
+
const message =
|
|
775
|
+
oversizedFiles.length === 1
|
|
776
|
+
? `文件限制大小: ${limitHumanSize}`
|
|
777
|
+
: `有 ${oversizedFiles.length} 个文件限制: ${limitHumanSize}`
|
|
778
|
+
|
|
779
|
+
this.$toast(message)
|
|
780
|
+
return { valid: false, files: validFiles, oversizedFiles }
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
return { valid: true, files: validFiles }
|
|
784
|
+
},
|
|
785
|
+
// 解析文件大小字符串或数字为字节数
|
|
786
|
+
parseFileSize(size) {
|
|
787
|
+
if (typeof size === 'number') return size
|
|
788
|
+
if (typeof size === 'string') {
|
|
789
|
+
const units = {
|
|
790
|
+
B: 1,
|
|
791
|
+
KB: 1024,
|
|
792
|
+
MB: 1024 * 1024,
|
|
793
|
+
GB: 1024 * 1024 * 1024
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
const regex = /^(\d+(?:\.\d+)?)\s*(B|KB|MB|GB)$/i
|
|
797
|
+
const match = size.trim().match(regex)
|
|
798
|
+
|
|
799
|
+
if (match) {
|
|
800
|
+
const value = parseFloat(match[1])
|
|
801
|
+
const unit = match[2].toUpperCase()
|
|
802
|
+
return value * units[unit]
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// 如果没有单位,默认为字节
|
|
806
|
+
const numSize = parseInt(size, 10)
|
|
807
|
+
return isNaN(numSize) ? 0 : numSize
|
|
808
|
+
}
|
|
809
|
+
return 0
|
|
810
|
+
},
|
|
811
|
+
// 格式化文件大小显示
|
|
812
|
+
formatFileSize(bytes) {
|
|
813
|
+
if (bytes === 0) return '0 B'
|
|
814
|
+
|
|
815
|
+
const units = ['B', 'KB', 'MB', 'GB']
|
|
816
|
+
let size = bytes
|
|
817
|
+
let unitIndex = 0
|
|
818
|
+
|
|
819
|
+
while (size >= 1024 && unitIndex < units.length - 1) {
|
|
820
|
+
size /= 1024
|
|
821
|
+
unitIndex++
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
return `${size.toFixed(unitIndex === 0 ? 0 : 1)} ${units[unitIndex]}`
|
|
825
|
+
},
|
|
826
|
+
// 获取文件路径的文件信息(包含大小)
|
|
827
|
+
async getFileInfoForPaths(filePaths) {
|
|
828
|
+
const fileInfos = []
|
|
829
|
+
|
|
830
|
+
for (const filePath of filePaths) {
|
|
831
|
+
try {
|
|
832
|
+
// #ifdef APP-PLUS || H5
|
|
833
|
+
// uni.getFileInfo 在 app 和 h5 中可用
|
|
834
|
+
const [, fileInfo] = await uni.getFileInfo({
|
|
835
|
+
filePath: filePath
|
|
836
|
+
})
|
|
837
|
+
|
|
838
|
+
if (fileInfo && fileInfo.size !== undefined) {
|
|
839
|
+
fileInfos.push({
|
|
840
|
+
path: filePath,
|
|
841
|
+
size: fileInfo.size
|
|
842
|
+
})
|
|
843
|
+
} else {
|
|
844
|
+
// 如果无法获取文件信息,保持原路径(跳过大小检查)
|
|
845
|
+
fileInfos.push(filePath)
|
|
846
|
+
}
|
|
847
|
+
// #endif
|
|
848
|
+
|
|
849
|
+
// #ifndef APP-PLUS || H5
|
|
850
|
+
// 在小程序环境中,由于安全限制,可能无法获取文件大小
|
|
851
|
+
// 这种情况下跳过文件大小检查
|
|
852
|
+
fileInfos.push(filePath)
|
|
853
|
+
// #endif
|
|
854
|
+
} catch (error) {
|
|
855
|
+
console.log(TAG, '获取文件信息失败', error)
|
|
856
|
+
// 如果获取文件信息失败,保持原路径(跳过大小检查)
|
|
857
|
+
fileInfos.push(filePath)
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
return fileInfos
|
|
862
|
+
},
|
|
727
863
|
async handleUpload(paths) {
|
|
728
864
|
const beforeUpload = this.beforeUpload[PI_DEFAULT_FN_FLAG]
|
|
729
865
|
? this.beforeUpload.fn
|
|
@@ -799,11 +935,19 @@ export default {
|
|
|
799
935
|
this.showCrop = false
|
|
800
936
|
this.cropPic = ''
|
|
801
937
|
},
|
|
802
|
-
handleCompelteCrop() {
|
|
938
|
+
async handleCompelteCrop() {
|
|
803
939
|
console.log(TAG, '完成剪裁')
|
|
804
940
|
this.$emit('croped', [this.cropedPic])
|
|
805
941
|
this.showCrop = false
|
|
806
|
-
|
|
942
|
+
|
|
943
|
+
// 剪裁完成后也要检查文件大小
|
|
944
|
+
const tempFilesWithSize = await this.getFileInfoForPaths([this.cropedPic])
|
|
945
|
+
const sizeCheckResult = this.checkFileSize(tempFilesWithSize)
|
|
946
|
+
|
|
947
|
+
if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
|
|
948
|
+
const validPaths = sizeCheckResult.files.map(file => file.path || file)
|
|
949
|
+
this.handleUpload(validPaths)
|
|
950
|
+
}
|
|
807
951
|
},
|
|
808
952
|
async handleCompressImgs(tempFilePaths) {
|
|
809
953
|
const compressUrls = []
|
|
@@ -900,7 +1044,12 @@ export default {
|
|
|
900
1044
|
if (errMsg !== 'chooseFile:ok') {
|
|
901
1045
|
throw new Error(errMsg)
|
|
902
1046
|
}
|
|
903
|
-
|
|
1047
|
+
|
|
1048
|
+
// 检查文件大小限制
|
|
1049
|
+
const sizeCheckResult = this.checkFileSize(tempFiles)
|
|
1050
|
+
if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
|
|
1051
|
+
this.handleUpload(sizeCheckResult.files)
|
|
1052
|
+
}
|
|
904
1053
|
} catch (error) {
|
|
905
1054
|
console.log(TAG, '上传文件失败', error)
|
|
906
1055
|
this.$emit('fail', error)
|
|
@@ -939,7 +1088,12 @@ export default {
|
|
|
939
1088
|
return this.isRightType(file)
|
|
940
1089
|
})
|
|
941
1090
|
console.log(TAG, '过滤后的文件列表:', tempFiles)
|
|
942
|
-
|
|
1091
|
+
|
|
1092
|
+
// 检查文件大小限制
|
|
1093
|
+
const sizeCheckResult = this.checkFileSize(tempFiles)
|
|
1094
|
+
if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
|
|
1095
|
+
this.handleUpload(sizeCheckResult.files)
|
|
1096
|
+
}
|
|
943
1097
|
} catch (error) {
|
|
944
1098
|
console.log(error)
|
|
945
1099
|
}
|
|
@@ -989,12 +1143,25 @@ export default {
|
|
|
989
1143
|
this.crop &&
|
|
990
1144
|
tempFilePaths.length === 1
|
|
991
1145
|
) {
|
|
992
|
-
//
|
|
1146
|
+
// 单张图片设置了剪裁,暂时不进行文件大小检查,在剪裁完成后再检查
|
|
993
1147
|
this.showCrop = true
|
|
994
1148
|
this.cropPic = tempFilePaths[0]
|
|
995
1149
|
return
|
|
996
1150
|
}
|
|
997
|
-
|
|
1151
|
+
|
|
1152
|
+
// 对于选择的图片路径,我们需要构造合适的文件对象进行大小检查
|
|
1153
|
+
// uni-app 的 chooseImage 返回的 tempFilePaths 不包含文件大小信息
|
|
1154
|
+
// 所以我们需要获取文件信息
|
|
1155
|
+
const tempFilesWithSize = await this.getFileInfoForPaths(tempFilePaths)
|
|
1156
|
+
const sizeCheckResult = this.checkFileSize(tempFilesWithSize)
|
|
1157
|
+
|
|
1158
|
+
if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
|
|
1159
|
+
// 恢复为路径数组格式传递给 handleUpload
|
|
1160
|
+
const validPaths = sizeCheckResult.files.map(
|
|
1161
|
+
file => file.path || file
|
|
1162
|
+
)
|
|
1163
|
+
this.handleUpload(validPaths)
|
|
1164
|
+
}
|
|
998
1165
|
} catch (error) {
|
|
999
1166
|
console.log(TAG, '上传文件失败', error)
|
|
1000
1167
|
this.$emit('fail', error)
|
|
@@ -1028,7 +1195,12 @@ export default {
|
|
|
1028
1195
|
}
|
|
1029
1196
|
tempFile.path = tempFilePath
|
|
1030
1197
|
console.log(TAG, '上传视频结果', result)
|
|
1031
|
-
|
|
1198
|
+
|
|
1199
|
+
// 检查文件大小限制
|
|
1200
|
+
const sizeCheckResult = this.checkFileSize([tempFile])
|
|
1201
|
+
if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
|
|
1202
|
+
this.handleUpload(sizeCheckResult.files)
|
|
1203
|
+
}
|
|
1032
1204
|
} catch (error) {
|
|
1033
1205
|
console.log(TAG, '上传视频失败', error)
|
|
1034
1206
|
this.$emit('fail', error)
|
|
@@ -1089,7 +1261,13 @@ export default {
|
|
|
1089
1261
|
}
|
|
1090
1262
|
// #endif
|
|
1091
1263
|
|
|
1092
|
-
|
|
1264
|
+
// 检查文件大小限制
|
|
1265
|
+
const sizeCheckResult = this.checkFileSize(tempFiles)
|
|
1266
|
+
if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
|
|
1267
|
+
this.handleUpload(
|
|
1268
|
+
sizeCheckResult.files.map(file => file.tempFilePath || file)
|
|
1269
|
+
)
|
|
1270
|
+
}
|
|
1093
1271
|
} catch (error) {
|
|
1094
1272
|
console.log(TAG, '上传视频失败', error)
|
|
1095
1273
|
this.$emit('fail', error)
|
package/config/uploadImg.js
CHANGED
|
@@ -80,5 +80,6 @@ export default {
|
|
|
80
80
|
beforeRemove: null, // 删除之前的钩子
|
|
81
81
|
customTakePhoto: false, // 是否自定义拍照(如果为true,则不调用相机,通过事件自己实现拍照上传)
|
|
82
82
|
fileIconMap: {}, // 文件类型和图标映射关系,对象形式({PDF: 'https://xxx'})
|
|
83
|
-
customUploadParams: {} // 自定义上传参数,对象形式
|
|
83
|
+
customUploadParams: {}, // 自定义上传参数,对象形式
|
|
84
|
+
fileLimit: null // 文件大小限制,单位为字节,null表示不限制
|
|
84
85
|
}
|