sadais-piui 1.2.7-beta154 → 1.2.7-beta157
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.
- package/components/pi-upload-img/index.vue +94 -100
- package/package.json +1 -1
- package/utils/file-utils.js +51 -0
|
@@ -145,23 +145,13 @@
|
|
|
145
145
|
/>
|
|
146
146
|
|
|
147
147
|
<!-- 自定义拍照操作面板(小程序专用) -->
|
|
148
|
-
<pi-
|
|
148
|
+
<pi-action-sheet
|
|
149
149
|
v-model="showCustomTakePhoto"
|
|
150
150
|
:items="customTakePhotoItems"
|
|
151
|
-
:single-cancel="false"
|
|
152
|
-
single-confirm
|
|
153
|
-
:item-style="{
|
|
154
|
-
padding: '32rpx'
|
|
155
|
-
}"
|
|
156
151
|
:popup="{
|
|
157
152
|
borderRadius: 0
|
|
158
153
|
}"
|
|
159
|
-
|
|
160
|
-
showTitle: true,
|
|
161
|
-
title: '请选择拍摄方式'
|
|
162
|
-
}"
|
|
163
|
-
item-height="auto"
|
|
164
|
-
@confirm="handleCustomTakePhotoSelect"
|
|
154
|
+
@select="handleCustomTakePhotoSelect"
|
|
165
155
|
/>
|
|
166
156
|
</view>
|
|
167
157
|
</template>
|
|
@@ -180,7 +170,8 @@ import {
|
|
|
180
170
|
formatFileSize,
|
|
181
171
|
getFileInfoForPaths,
|
|
182
172
|
getImageInfo,
|
|
183
|
-
calculateCompressDimensions
|
|
173
|
+
calculateCompressDimensions,
|
|
174
|
+
handleCompressImgs
|
|
184
175
|
} from '../../utils/file-utils'
|
|
185
176
|
|
|
186
177
|
const TAG = 'PiUploadImg'
|
|
@@ -456,8 +447,7 @@ export default {
|
|
|
456
447
|
cropPic: '', // 需要剪裁的图片
|
|
457
448
|
cropedPic: '', // 剪裁后的图片
|
|
458
449
|
showActionSheet: false, // 是否显示上传视频操作面板
|
|
459
|
-
showCustomTakePhoto: false
|
|
460
|
-
currentSourceType: ['camera', 'album'] // 当前选择的拍照方式
|
|
450
|
+
showCustomTakePhoto: false // 是否显示自定义拍照操作面板
|
|
461
451
|
}
|
|
462
452
|
},
|
|
463
453
|
computed: {
|
|
@@ -486,8 +476,16 @@ export default {
|
|
|
486
476
|
}
|
|
487
477
|
]
|
|
488
478
|
},
|
|
479
|
+
currentSourceType() {
|
|
480
|
+
if (this.type === 'video') {
|
|
481
|
+
return this.getChooseVideoOpts.sourceType
|
|
482
|
+
}
|
|
483
|
+
if (this.type === 'image') {
|
|
484
|
+
return this.getChooseImageOpts.sourceType
|
|
485
|
+
}
|
|
486
|
+
return this.getChooseFileOpts.sourceType
|
|
487
|
+
},
|
|
489
488
|
customTakePhotoItems() {
|
|
490
|
-
// currentSourceType 当前选择的拍照方式 ['camera', 'album']
|
|
491
489
|
let sourceType = this.currentSourceType || ['camera', 'album']
|
|
492
490
|
if (sourceType.length === 0) {
|
|
493
491
|
sourceType = ['camera', 'album']
|
|
@@ -496,12 +494,16 @@ export default {
|
|
|
496
494
|
{
|
|
497
495
|
id: 'takePhoto',
|
|
498
496
|
text: '拍照',
|
|
499
|
-
show:
|
|
497
|
+
show:
|
|
498
|
+
sourceType.includes('camera') &&
|
|
499
|
+
['all', 'image'].includes(this.type)
|
|
500
500
|
},
|
|
501
501
|
{
|
|
502
502
|
id: 'takeVideo',
|
|
503
503
|
text: '录像',
|
|
504
|
-
show:
|
|
504
|
+
show:
|
|
505
|
+
sourceType.includes('camera') &&
|
|
506
|
+
['all', 'video'].includes(this.type)
|
|
505
507
|
},
|
|
506
508
|
// {
|
|
507
509
|
// id: 'takePhotoOffline',
|
|
@@ -510,9 +512,16 @@ export default {
|
|
|
510
512
|
// show: sourceType.includes('camera')
|
|
511
513
|
// },
|
|
512
514
|
{
|
|
513
|
-
id: '
|
|
514
|
-
text: '
|
|
515
|
-
show:
|
|
515
|
+
id: 'chooseImageFromAlbum',
|
|
516
|
+
text: '从手机相册选择图片',
|
|
517
|
+
show:
|
|
518
|
+
sourceType.includes('album') && ['all', 'image'].includes(this.type)
|
|
519
|
+
},
|
|
520
|
+
{
|
|
521
|
+
id: 'chooseVideoFromAlbum',
|
|
522
|
+
text: '从手机相册选择视频',
|
|
523
|
+
show:
|
|
524
|
+
sourceType.includes('album') && ['all', 'video'].includes(this.type)
|
|
516
525
|
}
|
|
517
526
|
// {
|
|
518
527
|
// id: 'chooseFromCacheAlbum',
|
|
@@ -628,11 +637,11 @@ export default {
|
|
|
628
637
|
}
|
|
629
638
|
},
|
|
630
639
|
methods: {
|
|
631
|
-
isImageFilename() {
|
|
632
|
-
return isImageFilename
|
|
640
|
+
isImageFilename(filename) {
|
|
641
|
+
return isImageFilename(filename)
|
|
633
642
|
},
|
|
634
|
-
isVideoFilename() {
|
|
635
|
-
return isVideoFilename
|
|
643
|
+
isVideoFilename(filename) {
|
|
644
|
+
return isVideoFilename(filename)
|
|
636
645
|
},
|
|
637
646
|
getFileType(file) {
|
|
638
647
|
if (!file) return ''
|
|
@@ -879,50 +888,7 @@ export default {
|
|
|
879
888
|
this.handleUpload(validPaths)
|
|
880
889
|
}
|
|
881
890
|
},
|
|
882
|
-
async handleCompressImgs(tempFilePaths) {
|
|
883
|
-
const compressUrls = []
|
|
884
|
-
|
|
885
|
-
for (let index = 0; index < tempFilePaths.length; index++) {
|
|
886
|
-
const url = tempFilePaths[index]
|
|
887
|
-
|
|
888
|
-
// 获取图片信息并计算智能压缩参数
|
|
889
|
-
const imageInfo = await getImageInfo(url)
|
|
890
|
-
const compressDimensions = calculateCompressDimensions(
|
|
891
|
-
imageInfo,
|
|
892
|
-
this.compressOption
|
|
893
|
-
)
|
|
894
|
-
|
|
895
|
-
// 构建压缩参数
|
|
896
|
-
const compressParams = {
|
|
897
|
-
src: url,
|
|
898
|
-
...this.compressOption
|
|
899
|
-
}
|
|
900
|
-
|
|
901
|
-
// 如果启用了智能压缩,添加宽高参数
|
|
902
|
-
if (this.compressOption && this.compressOption.smartCompress) {
|
|
903
|
-
compressParams.compressedWidth = compressDimensions.width
|
|
904
|
-
compressParams.compressedHeight = compressDimensions.height
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
console.log('图片压缩参数', compressParams)
|
|
908
891
|
|
|
909
|
-
const [, result] = await uni.compressImage(compressParams)
|
|
910
|
-
const { errMsg, tempFilePath } = result
|
|
911
|
-
|
|
912
|
-
if (
|
|
913
|
-
errMsg == 'compressImage:ok' &&
|
|
914
|
-
tempFilePath &&
|
|
915
|
-
typeof tempFilePath == 'string'
|
|
916
|
-
) {
|
|
917
|
-
compressUrls.push(tempFilePath)
|
|
918
|
-
console.log(TAG, '智能压缩图片成功', tempFilePath)
|
|
919
|
-
} else {
|
|
920
|
-
compressUrls.push(url)
|
|
921
|
-
console.log(TAG, '压缩图片失败,使用原图', tempFilePath, 'src=' + url)
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
return compressUrls
|
|
925
|
-
},
|
|
926
892
|
handleChoose() {
|
|
927
893
|
if (this.disabled || !this.getUploadMaxCount) return
|
|
928
894
|
// 检查上传地址
|
|
@@ -952,12 +918,20 @@ export default {
|
|
|
952
918
|
this.$emit('show-actions')
|
|
953
919
|
this.showActionSheet = true
|
|
954
920
|
} else {
|
|
921
|
+
if (this.customTakePhoto) {
|
|
922
|
+
this.handleOpenCustomTakePhoto()
|
|
923
|
+
return
|
|
924
|
+
}
|
|
955
925
|
this.handleChooseMedia()
|
|
956
926
|
}
|
|
957
927
|
// #endif
|
|
958
928
|
|
|
959
929
|
return
|
|
960
930
|
}
|
|
931
|
+
if (this.customTakePhoto) {
|
|
932
|
+
this.handleOpenCustomTakePhoto()
|
|
933
|
+
return
|
|
934
|
+
}
|
|
961
935
|
if (this.type === 'image') {
|
|
962
936
|
this.handleChooseImage()
|
|
963
937
|
return
|
|
@@ -974,6 +948,13 @@ export default {
|
|
|
974
948
|
return
|
|
975
949
|
}
|
|
976
950
|
},
|
|
951
|
+
handleOpenCustomTakePhoto() {
|
|
952
|
+
if (this.customTakePhotoItems.length === 1) {
|
|
953
|
+
this.handleCustomTakePhotoSelect(this.customTakePhotoItems[0])
|
|
954
|
+
return
|
|
955
|
+
}
|
|
956
|
+
this.showCustomTakePhoto = true
|
|
957
|
+
},
|
|
977
958
|
async handleChooseFile() {
|
|
978
959
|
const { sizeType, sourceType, crop } = this.getChooseFileOpts
|
|
979
960
|
try {
|
|
@@ -1052,13 +1033,6 @@ export default {
|
|
|
1052
1033
|
async handleChooseImage() {
|
|
1053
1034
|
const { sizeType, sourceType, crop } = this.getChooseImageOpts
|
|
1054
1035
|
|
|
1055
|
-
this.currentSourceType = sourceType
|
|
1056
|
-
|
|
1057
|
-
if (this.customTakePhoto) {
|
|
1058
|
-
this.showCustomTakePhoto = true
|
|
1059
|
-
return
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
1036
|
this.handleUniChooseImage(sizeType, sourceType, crop)
|
|
1063
1037
|
},
|
|
1064
1038
|
async handleUniChooseImage(sizeType, sourceType, crop) {
|
|
@@ -1081,11 +1055,11 @@ export default {
|
|
|
1081
1055
|
}
|
|
1082
1056
|
// #ifndef H5
|
|
1083
1057
|
// 压缩图片
|
|
1084
|
-
if (
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1058
|
+
if (this.compressOption) {
|
|
1059
|
+
tempFilePaths = await handleCompressImgs(
|
|
1060
|
+
tempFilePaths,
|
|
1061
|
+
this.compressOption
|
|
1062
|
+
)
|
|
1089
1063
|
}
|
|
1090
1064
|
// #endif
|
|
1091
1065
|
if (
|
|
@@ -1117,14 +1091,11 @@ export default {
|
|
|
1117
1091
|
}
|
|
1118
1092
|
},
|
|
1119
1093
|
async handleChooseVideo() {
|
|
1120
|
-
const { sourceType
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
this.showCustomTakePhoto = true
|
|
1126
|
-
return
|
|
1127
|
-
}
|
|
1094
|
+
const { sourceType } = this.getChooseVideoOpts
|
|
1095
|
+
this.handleUniChooseVideo(sourceType)
|
|
1096
|
+
},
|
|
1097
|
+
async handleUniChooseVideo(sourceType) {
|
|
1098
|
+
const { maxDuration, camera } = this.getChooseVideoOpts
|
|
1128
1099
|
|
|
1129
1100
|
try {
|
|
1130
1101
|
const [cancel, result] = await uni.chooseVideo({
|
|
@@ -1156,20 +1127,22 @@ export default {
|
|
|
1156
1127
|
}
|
|
1157
1128
|
},
|
|
1158
1129
|
async handleChooseMedia() {
|
|
1159
|
-
const { sourceType
|
|
1130
|
+
const { sourceType } =
|
|
1160
1131
|
this.type === 'video' ? this.getChooseVideoOpts : this.getChooseFileOpts
|
|
1161
1132
|
|
|
1162
|
-
this.
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1133
|
+
this.handleUniChooseMedia(
|
|
1134
|
+
this.type === 'video' ? ['video'] : ['mix'],
|
|
1135
|
+
sourceType
|
|
1136
|
+
)
|
|
1137
|
+
},
|
|
1138
|
+
async handleUniChooseMedia(mediaType, sourceType) {
|
|
1139
|
+
const { maxDuration, camera } =
|
|
1140
|
+
this.type === 'video' ? this.getChooseVideoOpts : this.getChooseFileOpts
|
|
1168
1141
|
|
|
1169
1142
|
try {
|
|
1170
1143
|
const [cancel, result] = await uni.chooseMedia({
|
|
1171
1144
|
count: this.getUploadMaxCount,
|
|
1172
|
-
mediaType
|
|
1145
|
+
mediaType,
|
|
1173
1146
|
extension: this.getExtension,
|
|
1174
1147
|
sourceType,
|
|
1175
1148
|
maxDuration,
|
|
@@ -1195,9 +1168,10 @@ export default {
|
|
|
1195
1168
|
const tempFilePath = tempFile.tempFilePath
|
|
1196
1169
|
let filePath = ''
|
|
1197
1170
|
if (isImageFilename(tempFilePath)) {
|
|
1198
|
-
const filePaths = await
|
|
1199
|
-
tempFile.tempFilePath
|
|
1200
|
-
|
|
1171
|
+
const filePaths = await handleCompressImgs(
|
|
1172
|
+
[tempFile.tempFilePath],
|
|
1173
|
+
this.compressOption
|
|
1174
|
+
)
|
|
1201
1175
|
filePath = filePaths[0]
|
|
1202
1176
|
} else {
|
|
1203
1177
|
filePath = await this.handleCompressVideo(tempFile.tempFilePath)
|
|
@@ -1257,10 +1231,30 @@ export default {
|
|
|
1257
1231
|
})
|
|
1258
1232
|
},
|
|
1259
1233
|
handleCustomTakePhotoSelect(type) {
|
|
1260
|
-
if (type.id === '
|
|
1234
|
+
if (type.id === 'takeVideo') {
|
|
1235
|
+
// #ifndef MP-WEIXIN
|
|
1236
|
+
this.handleUniChooseVideo(['camera'])
|
|
1237
|
+
// #endif
|
|
1238
|
+
|
|
1239
|
+
// #ifdef MP-WEIXIN
|
|
1240
|
+
this.handleUniChooseMedia(['video'], ['camera'])
|
|
1241
|
+
// #endif
|
|
1242
|
+
return
|
|
1243
|
+
}
|
|
1244
|
+
if (type.id === 'chooseImageFromAlbum') {
|
|
1261
1245
|
// 从手机相册选择
|
|
1262
1246
|
const { sizeType, crop } = this.getChooseImageOpts
|
|
1263
1247
|
this.handleUniChooseImage(sizeType, ['album'], crop)
|
|
1248
|
+
return
|
|
1249
|
+
} else if (type.id === 'chooseVideoFromAlbum') {
|
|
1250
|
+
// #ifndef MP-WEIXIN
|
|
1251
|
+
this.handleUniChooseVideo(['album'])
|
|
1252
|
+
// #endif
|
|
1253
|
+
|
|
1254
|
+
// #ifdef MP-WEIXIN
|
|
1255
|
+
this.handleUniChooseMedia(['video'], ['album'])
|
|
1256
|
+
// #endif
|
|
1257
|
+
return
|
|
1264
1258
|
} else {
|
|
1265
1259
|
this.handleEmitTakePhoto(type.id)
|
|
1266
1260
|
}
|
package/package.json
CHANGED
package/utils/file-utils.js
CHANGED
|
@@ -220,3 +220,54 @@ export function calculateCompressDimensions(imageInfo, compressOption = {}) {
|
|
|
220
220
|
|
|
221
221
|
return { width: targetWidth, height: targetHeight }
|
|
222
222
|
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 压缩图片处理
|
|
226
|
+
* @param {Array<string>} tempFilePaths - 临时文件路径数组
|
|
227
|
+
* @param {Object} compressOption - 压缩配置选项
|
|
228
|
+
* @returns {Promise<Array<string>>} 压缩后的文件路径数组
|
|
229
|
+
*/
|
|
230
|
+
export async function handleCompressImgs(tempFilePaths, compressOption = {}) {
|
|
231
|
+
const compressUrls = []
|
|
232
|
+
|
|
233
|
+
for (let index = 0; index < tempFilePaths.length; index++) {
|
|
234
|
+
const url = tempFilePaths[index]
|
|
235
|
+
|
|
236
|
+
// 获取图片信息并计算智能压缩参数
|
|
237
|
+
const imageInfo = await getImageInfo(url)
|
|
238
|
+
const compressDimensions = calculateCompressDimensions(
|
|
239
|
+
imageInfo,
|
|
240
|
+
compressOption
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
// 构建压缩参数
|
|
244
|
+
const compressParams = {
|
|
245
|
+
src: url,
|
|
246
|
+
...compressOption
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// 如果启用了智能压缩,添加宽高参数
|
|
250
|
+
if (compressOption && compressOption.smartCompress) {
|
|
251
|
+
compressParams.compressedWidth = compressDimensions.width
|
|
252
|
+
compressParams.compressedHeight = compressDimensions.height
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
console.log('图片压缩参数', compressParams)
|
|
256
|
+
|
|
257
|
+
const [, result] = await uni.compressImage(compressParams)
|
|
258
|
+
const { errMsg, tempFilePath } = result
|
|
259
|
+
|
|
260
|
+
if (
|
|
261
|
+
errMsg == 'compressImage:ok' &&
|
|
262
|
+
tempFilePath &&
|
|
263
|
+
typeof tempFilePath == 'string'
|
|
264
|
+
) {
|
|
265
|
+
compressUrls.push(tempFilePath)
|
|
266
|
+
console.log('智能压缩图片成功', tempFilePath)
|
|
267
|
+
} else {
|
|
268
|
+
compressUrls.push(url)
|
|
269
|
+
console.log('压缩图片失败,使用原图', tempFilePath, 'src=' + url)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return compressUrls
|
|
273
|
+
}
|