sadais-piui 1.2.7-beta151 → 1.2.7-beta153
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-swiper/index.vue +1 -2
- package/components/pi-upload-img/index.vue +122 -14
- package/config/uploadImg.js +21 -0
- package/index.js +15 -2
- package/package.json +1 -1
|
@@ -26,8 +26,7 @@
|
|
|
26
26
|
:style="[
|
|
27
27
|
{
|
|
28
28
|
borderRadius: `${borderRadius}rpx`,
|
|
29
|
-
transform:
|
|
30
|
-
effect3d && val !== index ? 'scaleY(0.9)' : 'scaleY(1)',
|
|
29
|
+
transform: effect3d && val !== index ? 'scale(0.9)' : 'scale(1)',
|
|
31
30
|
padding: effect3d && val !== index ? '0 20rpx' : 0
|
|
32
31
|
}
|
|
33
32
|
]"
|
|
@@ -829,8 +829,6 @@ export default {
|
|
|
829
829
|
|
|
830
830
|
for (const filePath of filePaths) {
|
|
831
831
|
try {
|
|
832
|
-
// #ifdef APP-PLUS || H5
|
|
833
|
-
// uni.getFileInfo 在 app 和 h5 中可用
|
|
834
832
|
const [, fileInfo] = await uni.getFileInfo({
|
|
835
833
|
filePath: filePath
|
|
836
834
|
})
|
|
@@ -844,13 +842,6 @@ export default {
|
|
|
844
842
|
// 如果无法获取文件信息,保持原路径(跳过大小检查)
|
|
845
843
|
fileInfos.push(filePath)
|
|
846
844
|
}
|
|
847
|
-
// #endif
|
|
848
|
-
|
|
849
|
-
// #ifndef APP-PLUS || H5
|
|
850
|
-
// 在小程序环境中,由于安全限制,可能无法获取文件大小
|
|
851
|
-
// 这种情况下跳过文件大小检查
|
|
852
|
-
fileInfos.push(filePath)
|
|
853
|
-
// #endif
|
|
854
845
|
} catch (error) {
|
|
855
846
|
console.log(TAG, '获取文件信息失败', error)
|
|
856
847
|
// 如果获取文件信息失败,保持原路径(跳过大小检查)
|
|
@@ -860,6 +851,107 @@ export default {
|
|
|
860
851
|
|
|
861
852
|
return fileInfos
|
|
862
853
|
},
|
|
854
|
+
// 获取图片的宽高信息
|
|
855
|
+
async getImageInfo(imagePath) {
|
|
856
|
+
try {
|
|
857
|
+
const [, imageInfo] = await uni.getImageInfo({
|
|
858
|
+
src: imagePath
|
|
859
|
+
})
|
|
860
|
+
|
|
861
|
+
if (imageInfo && imageInfo.width && imageInfo.height) {
|
|
862
|
+
// 处理可能的宽高反转问题
|
|
863
|
+
let { width, height, orientation } = imageInfo
|
|
864
|
+
|
|
865
|
+
// 根据图片方向调整宽高
|
|
866
|
+
if (orientation && (orientation === 6 || orientation === 8)) {
|
|
867
|
+
// 旋转90度或270度的图片,宽高需要交换
|
|
868
|
+
;[width, height] = [height, width]
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
return {
|
|
872
|
+
width,
|
|
873
|
+
height,
|
|
874
|
+
orientation,
|
|
875
|
+
aspectRatio: width / height,
|
|
876
|
+
maxDimension: Math.max(width, height)
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
return null
|
|
881
|
+
} catch (error) {
|
|
882
|
+
console.log(TAG, '获取图片信息失败', error)
|
|
883
|
+
return null
|
|
884
|
+
}
|
|
885
|
+
},
|
|
886
|
+
// 智能压缩算法:根据图片比例和尺寸计算目标宽高
|
|
887
|
+
calculateCompressDimensions(imageInfo) {
|
|
888
|
+
if (!imageInfo) {
|
|
889
|
+
// 如果无法获取图片信息,使用默认压缩参数
|
|
890
|
+
return { width: 1080, height: 1080 }
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
const { width, height, aspectRatio, maxDimension } = imageInfo
|
|
894
|
+
|
|
895
|
+
// 获取智能压缩配置
|
|
896
|
+
const smartConfig = this.compressOption?.smartCompressConfig || {}
|
|
897
|
+
const baseThreshold = smartConfig.baseThreshold || 1080
|
|
898
|
+
const ratioStrategies = smartConfig.ratioStrategies || {
|
|
899
|
+
normal: { maxSide: 1664, ratioRange: [1, 9 / 16] },
|
|
900
|
+
portrait: { maxSide: 1440, ratioRange: [9 / 16, 1 / 2] },
|
|
901
|
+
extreme: { maxSide: 1280, ratioRange: [1 / 2, Infinity] }
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// 当原图宽/高最大值≤基础阈值时,保持原始分辨率,仅进行无损压缩优化
|
|
905
|
+
if (maxDimension <= baseThreshold) {
|
|
906
|
+
return { width, height }
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
// 根据图片比例确定压缩后的最长边限制
|
|
910
|
+
let maxSideLength = baseThreshold // 默认最长边
|
|
911
|
+
let strategy = 'default'
|
|
912
|
+
|
|
913
|
+
// 遍历策略配置,找到匹配的比例范围
|
|
914
|
+
for (const [strategyName, strategyConfig] of Object.entries(
|
|
915
|
+
ratioStrategies
|
|
916
|
+
)) {
|
|
917
|
+
const [minRatio, maxRatio] = strategyConfig.ratioRange
|
|
918
|
+
if (aspectRatio >= minRatio && aspectRatio <= maxRatio) {
|
|
919
|
+
maxSideLength = strategyConfig.maxSide
|
|
920
|
+
strategy = strategyName
|
|
921
|
+
break
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
// 按原图比例等比压缩,使最长边不超过限制
|
|
926
|
+
let targetWidth = width
|
|
927
|
+
let targetHeight = height
|
|
928
|
+
|
|
929
|
+
if (width > height) {
|
|
930
|
+
// 横向图片
|
|
931
|
+
if (width > maxSideLength) {
|
|
932
|
+
targetWidth = maxSideLength
|
|
933
|
+
targetHeight = Math.round((height * maxSideLength) / width)
|
|
934
|
+
}
|
|
935
|
+
} else {
|
|
936
|
+
// 纵向图片或正方形
|
|
937
|
+
if (height > maxSideLength) {
|
|
938
|
+
targetHeight = maxSideLength
|
|
939
|
+
targetWidth = Math.round((width * maxSideLength) / height)
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
console.log(TAG, '智能压缩计算', {
|
|
944
|
+
原始尺寸: `${width}x${height}`,
|
|
945
|
+
宽高比: aspectRatio.toFixed(3),
|
|
946
|
+
最长边: maxDimension,
|
|
947
|
+
目标尺寸: `${targetWidth}x${targetHeight}`,
|
|
948
|
+
最长边限制: maxSideLength,
|
|
949
|
+
压缩策略: strategy,
|
|
950
|
+
基础阈值: baseThreshold
|
|
951
|
+
})
|
|
952
|
+
|
|
953
|
+
return { width: targetWidth, height: targetHeight }
|
|
954
|
+
},
|
|
863
955
|
async handleUpload(paths) {
|
|
864
956
|
const beforeUpload = this.beforeUpload[PI_DEFAULT_FN_FLAG]
|
|
865
957
|
? this.beforeUpload.fn
|
|
@@ -954,21 +1046,38 @@ export default {
|
|
|
954
1046
|
|
|
955
1047
|
for (let index = 0; index < tempFilePaths.length; index++) {
|
|
956
1048
|
const url = tempFilePaths[index]
|
|
957
|
-
|
|
1049
|
+
|
|
1050
|
+
// 获取图片信息并计算智能压缩参数
|
|
1051
|
+
const imageInfo = await this.getImageInfo(url)
|
|
1052
|
+
const compressDimensions = this.calculateCompressDimensions(imageInfo)
|
|
1053
|
+
|
|
1054
|
+
// 构建压缩参数
|
|
1055
|
+
const compressParams = {
|
|
958
1056
|
src: url,
|
|
959
1057
|
...this.compressOption
|
|
960
|
-
}
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
// 如果启用了智能压缩,添加宽高参数
|
|
1061
|
+
if (this.compressOption && this.compressOption.smartCompress) {
|
|
1062
|
+
compressParams.compressedWidth = compressDimensions.width
|
|
1063
|
+
compressParams.compressedHeight = compressDimensions.height
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
console.log('图片压缩参数', compressParams)
|
|
1067
|
+
|
|
1068
|
+
const [, result] = await uni.compressImage(compressParams)
|
|
961
1069
|
const { errMsg, tempFilePath } = result
|
|
1070
|
+
|
|
962
1071
|
if (
|
|
963
1072
|
errMsg == 'compressImage:ok' &&
|
|
964
1073
|
tempFilePath &&
|
|
965
1074
|
typeof tempFilePath == 'string'
|
|
966
1075
|
) {
|
|
967
1076
|
compressUrls.push(tempFilePath)
|
|
968
|
-
console.log(TAG, '
|
|
1077
|
+
console.log(TAG, '智能压缩图片成功', tempFilePath)
|
|
969
1078
|
} else {
|
|
970
1079
|
compressUrls.push(url)
|
|
971
|
-
console.log(TAG, '
|
|
1080
|
+
console.log(TAG, '压缩图片失败,使用原图', tempFilePath, 'src=' + url)
|
|
972
1081
|
}
|
|
973
1082
|
}
|
|
974
1083
|
return compressUrls
|
|
@@ -1151,7 +1260,6 @@ export default {
|
|
|
1151
1260
|
|
|
1152
1261
|
// 对于选择的图片路径,我们需要构造合适的文件对象进行大小检查
|
|
1153
1262
|
// uni-app 的 chooseImage 返回的 tempFilePaths 不包含文件大小信息
|
|
1154
|
-
// 所以我们需要获取文件信息
|
|
1155
1263
|
const tempFilesWithSize = await this.getFileInfoForPaths(tempFilePaths)
|
|
1156
1264
|
const sizeCheckResult = this.checkFileSize(tempFilesWithSize)
|
|
1157
1265
|
|
package/config/uploadImg.js
CHANGED
|
@@ -75,6 +75,27 @@ export default {
|
|
|
75
75
|
)
|
|
76
76
|
}
|
|
77
77
|
},
|
|
78
|
+
compressOption: {
|
|
79
|
+
quality: 100,
|
|
80
|
+
width: 'auto',
|
|
81
|
+
height: 'auto',
|
|
82
|
+
// 智能压缩配置
|
|
83
|
+
smartCompress: true, // 是否启用智能压缩,默认启用
|
|
84
|
+
// 智能压缩策略配置
|
|
85
|
+
smartCompressConfig: {
|
|
86
|
+
// 基础分辨率阈值,当图片最长边小于等于此值时保持原分辨率
|
|
87
|
+
baseThreshold: 1080,
|
|
88
|
+
// 不同比例区间的压缩策略
|
|
89
|
+
ratioStrategies: {
|
|
90
|
+
// 比例范围 (1:1~9:16):压缩后最长边不超过 1664px
|
|
91
|
+
normal: { maxSide: 1664, ratioRange: [1, 9 / 16] },
|
|
92
|
+
// 比例范围 (9:16~1:2):压缩后最长边不超过 1440px
|
|
93
|
+
portrait: { maxSide: 1440, ratioRange: [9 / 16, 1 / 2] },
|
|
94
|
+
// 比例范围 (1:2~1:∞):压缩后最长边不超过 1280px
|
|
95
|
+
extreme: { maxSide: 1280, ratioRange: [1 / 2, Infinity] }
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}, // 压缩配置
|
|
78
99
|
beforeUpload: null, // 上传之前的钩子
|
|
79
100
|
onProgress: null, // 文件上传时的钩子
|
|
80
101
|
beforeRemove: null, // 删除之前的钩子
|
package/index.js
CHANGED
|
@@ -3,8 +3,18 @@ import { getConfig, setConfig } from './config'
|
|
|
3
3
|
|
|
4
4
|
const logStyle = 'color:#ff6a00;'
|
|
5
5
|
|
|
6
|
+
// overwriteMerge 函数实现
|
|
7
|
+
function overwriteMerge(target, source, options) {
|
|
8
|
+
// 直接返回 source 数组,完全覆盖 target
|
|
9
|
+
return source.map(function(element) {
|
|
10
|
+
return options.cloneUnlessOtherwiseSpecified(element, options)
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
|
|
6
14
|
const install = function(Vue, config = {}) {
|
|
7
|
-
const piConfig = tools.lang.mergeDeep(getConfig(), config
|
|
15
|
+
const piConfig = tools.lang.mergeDeep(getConfig(), config, {
|
|
16
|
+
arrayMerge: overwriteMerge
|
|
17
|
+
})
|
|
8
18
|
setConfig(piConfig)
|
|
9
19
|
Vue.prototype.$piConfig = piConfig
|
|
10
20
|
Vue.prototype.$pi = tools
|
|
@@ -30,7 +40,10 @@ const install = function(Vue, config = {}) {
|
|
|
30
40
|
// #ifndef APP-PLUS
|
|
31
41
|
console.group && console.group('piui')
|
|
32
42
|
|
|
33
|
-
console.log(
|
|
43
|
+
console.log(
|
|
44
|
+
'%cpiui 已安装,教程:https://piui.sadais.com/docs/components/Config.html',
|
|
45
|
+
logStyle
|
|
46
|
+
)
|
|
34
47
|
console.log('%cpiui 组件全局配置:', logStyle, piConfig)
|
|
35
48
|
console.log('%cpiui tools 已挂载,使用方式 this.$pi.xxx:', logStyle, tools)
|
|
36
49
|
|