|
@@ -32,6 +32,11 @@ Page({
|
|
|
onLoad: function (options) {
|
|
|
const isReplied = options.isReplied === 'true';
|
|
|
|
|
|
+ // 更新地址信息,确保是最新的户号地址
|
|
|
+ this.setData({
|
|
|
+ address: app.globalData.currentAccountInfo.address
|
|
|
+ });
|
|
|
+
|
|
|
if (options.mode === 'preview') {
|
|
|
this.setData({
|
|
|
isPreviewMode: true,
|
|
@@ -153,15 +158,215 @@ Page({
|
|
|
}
|
|
|
|
|
|
if (validFiles.length > 0) {
|
|
|
- let newImageList = that.data.imageList.concat(validFiles);
|
|
|
- that.setData({
|
|
|
- imageList: newImageList
|
|
|
+ wx.showLoading({
|
|
|
+ title: '图片压缩中...',
|
|
|
+ mask: true
|
|
|
+ });
|
|
|
+
|
|
|
+ let processedFiles = new Array(validFiles.length);
|
|
|
+ let processCount = 0;
|
|
|
+
|
|
|
+ // 处理每个图片
|
|
|
+ validFiles.forEach((file, index) => {
|
|
|
+ that.compressImage(file.tempFilePath).then(compressedPath => {
|
|
|
+ processCount++;
|
|
|
+ // 获取压缩后图片的信息
|
|
|
+ wx.getFileInfo({
|
|
|
+ filePath: compressedPath,
|
|
|
+ success: (fileInfo) => {
|
|
|
+ console.log('原始大小:', file.size / 1024, 'KB');
|
|
|
+ console.log('压缩后大小:', fileInfo.size / 1024, 'KB');
|
|
|
+
|
|
|
+ // 在对应位置添加压缩后的图片路径
|
|
|
+ processedFiles[index] = {
|
|
|
+ tempFilePath: compressedPath,
|
|
|
+ size: fileInfo.size
|
|
|
+ };
|
|
|
+
|
|
|
+ // 检查是否所有图片都已处理完成
|
|
|
+ if (processCount === validFiles.length) {
|
|
|
+ // 过滤掉可能的空值,并按顺序合并
|
|
|
+ let newImageList = that.data.imageList.concat(processedFiles.filter(item => item));
|
|
|
+ that.setData({
|
|
|
+ imageList: newImageList
|
|
|
+ });
|
|
|
+ wx.hideLoading();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('获取文件信息失败:', err);
|
|
|
+ processCount++;
|
|
|
+ // 如果失败,也要检查是否处理完所有图片
|
|
|
+ if (processCount === validFiles.length) {
|
|
|
+ wx.hideLoading();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }).catch(err => {
|
|
|
+ console.error('图片压缩失败:', err);
|
|
|
+ processCount++;
|
|
|
+ if (processCount === validFiles.length) {
|
|
|
+ wx.hideLoading();
|
|
|
+ }
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
+ // 使用canvas进行图片压缩
|
|
|
+ compressImage: function(imagePath) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ // 获取图片信息
|
|
|
+ wx.getImageInfo({
|
|
|
+ src: imagePath,
|
|
|
+ success: (res) => {
|
|
|
+ // 先获取原图大小,单位KB
|
|
|
+ wx.getFileInfo({
|
|
|
+ filePath: imagePath,
|
|
|
+ success: (fileInfo) => {
|
|
|
+ const originalSize = fileInfo.size / 1024; // 原始大小,单位KB
|
|
|
+ // 根据原图大小确定压缩质量
|
|
|
+ let targetQuality = 0.8; // 默认压缩质量
|
|
|
+
|
|
|
+ if (originalSize <= 500) {
|
|
|
+ // 如果原图已经小于500KB,保持较高质量
|
|
|
+ targetQuality = 0.9;
|
|
|
+ } else if (originalSize <= 1024) {
|
|
|
+ // 1MB以下,适当压缩
|
|
|
+ targetQuality = 0.7;
|
|
|
+ } else if (originalSize <= 2048) {
|
|
|
+ // 2MB以下,中等压缩
|
|
|
+ targetQuality = 0.5;
|
|
|
+ } else if (originalSize <= 5120) {
|
|
|
+ // 5MB以下,较大压缩
|
|
|
+ targetQuality = 0.3;
|
|
|
+ } else {
|
|
|
+ // 超过5MB,大幅压缩
|
|
|
+ targetQuality = 0.2;
|
|
|
+ }
|
|
|
+ wx.createSelectorQuery()
|
|
|
+ .select('#compressCanvas')
|
|
|
+ .fields({ node: true, size: true })
|
|
|
+ .exec((canvasRes) => {
|
|
|
+ if (!canvasRes || !canvasRes[0] || !canvasRes[0].node) {
|
|
|
+ // 如果找不到canvas节点,则返回原图
|
|
|
+ resolve(imagePath);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const canvas = canvasRes[0].node;
|
|
|
+ const ctx = canvas.getContext('2d');
|
|
|
+ const image = canvas.createImage();
|
|
|
+
|
|
|
+ image.onload = () => {
|
|
|
+ // 计算要缩放的尺寸,保持宽高比
|
|
|
+ let ratio = 1;
|
|
|
+ // 根据原图大小调整尺寸
|
|
|
+ if (originalSize > 5120) { // 5MB以上
|
|
|
+ ratio = Math.min(1, 1200 / res.width, 1200 / res.height);
|
|
|
+ } else if (originalSize > 2048) { // 2MB以上
|
|
|
+ ratio = Math.min(1, 1500 / res.width, 1500 / res.height);
|
|
|
+ } else if (originalSize > 1024) { // 1MB以上
|
|
|
+ ratio = Math.min(1, 1800 / res.width, 1800 / res.height);
|
|
|
+ } else {
|
|
|
+ ratio = Math.min(1, 2000 / res.width, 2000 / res.height);
|
|
|
+ }
|
|
|
+
|
|
|
+ const targetWidth = Math.round(res.width * ratio);
|
|
|
+ const targetHeight = Math.round(res.height * ratio);
|
|
|
+
|
|
|
+ // 设置canvas尺寸
|
|
|
+ canvas.width = targetWidth;
|
|
|
+ canvas.height = targetHeight;
|
|
|
+ // 清除画布
|
|
|
+ ctx.clearRect(0, 0, targetWidth, targetHeight);
|
|
|
+ // 绘制图片
|
|
|
+ ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
|
|
|
+ // 压缩参数
|
|
|
+ const compressOptions = {
|
|
|
+ canvas: canvas,
|
|
|
+ width: targetWidth,
|
|
|
+ height: targetHeight,
|
|
|
+ destWidth: targetWidth,
|
|
|
+ destHeight: targetHeight,
|
|
|
+ fileType: 'jpg',
|
|
|
+ quality: targetQuality
|
|
|
+ };
|
|
|
+
|
|
|
+ // 第一次尝试压缩
|
|
|
+ const tryCompress = (quality, attempt = 1) => {
|
|
|
+ compressOptions.quality = quality;
|
|
|
+
|
|
|
+ wx.canvasToTempFilePath({
|
|
|
+ ...compressOptions,
|
|
|
+ success: (result) => {
|
|
|
+ // 检查压缩后的大小
|
|
|
+ wx.getFileInfo({
|
|
|
+ filePath: result.tempFilePath,
|
|
|
+ success: (compressedInfo) => {
|
|
|
+ const compressedSize = compressedInfo.size / 1024; // 压缩后大小,单位KB
|
|
|
+ console.log(`压缩后大小 (质量:${quality})`, compressedSize, 'KB');
|
|
|
+ // 如果压缩后大小仍大于500KB且尝试次数未达上限,继续压缩
|
|
|
+ if (compressedSize > 500 && attempt < 3) {
|
|
|
+ // 递减质量
|
|
|
+ const newQuality = Math.max(0.1, quality - 0.2);
|
|
|
+ console.log(`尝试重新压缩,质量降低至${newQuality}`);
|
|
|
+ tryCompress(newQuality, attempt + 1);
|
|
|
+ }
|
|
|
+ // 如果压缩后小于200KB且质量非最高,尝试提高质量
|
|
|
+ else if (compressedSize < 200 && quality < 0.9 && attempt < 3) {
|
|
|
+ // 递增质量
|
|
|
+ const newQuality = Math.min(0.9, quality + 0.2);
|
|
|
+ console.log(`尝试重新压缩,质量提高至${newQuality}`);
|
|
|
+ tryCompress(newQuality, attempt + 1);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // 返回压缩后的图片
|
|
|
+ resolve(result.tempFilePath);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ console.error('获取压缩后图片信息失败:', error);
|
|
|
+ resolve(result.tempFilePath);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ console.error('Canvas转图片失败:', error);
|
|
|
+ // 如果转换失败则返回原图
|
|
|
+ resolve(imagePath);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ // 开始尝试压缩
|
|
|
+ tryCompress(targetQuality);
|
|
|
+ };
|
|
|
+
|
|
|
+ image.onerror = () => {
|
|
|
+ console.error('图片加载失败');
|
|
|
+ resolve(imagePath);
|
|
|
+ };
|
|
|
+
|
|
|
+ image.src = imagePath;
|
|
|
+ });
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ console.error('获取原始图片信息失败:', error);
|
|
|
+ resolve(imagePath);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ console.error('获取图片信息失败:', error);
|
|
|
+ resolve(imagePath);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
previewImage: function (e) {
|
|
|
let index = e.currentTarget.dataset.index;
|
|
|
wx.previewImage({
|
|
@@ -506,6 +711,11 @@ Page({
|
|
|
this.setData({
|
|
|
formSubmitted: false
|
|
|
});
|
|
|
+ } else if (!this.data.isPreviewMode) {
|
|
|
+ // 非预览模式下,更新地址信息
|
|
|
+ this.setData({
|
|
|
+ address: app.globalData.currentAccountInfo.address
|
|
|
+ });
|
|
|
}
|
|
|
},
|
|
|
|