From 26cfa7afc4c30c909f82b635e7ea130c2937e5fc Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期五, 23 二月 2024 15:24:31 +0800
Subject: [PATCH] !84 图片上传组件增加压缩支持 Merge pull request !84 from 缝合怪/dev

---
 src/components/ImageUpload/index.vue |   22 +++++++++++++++++++---
 1 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/components/ImageUpload/index.vue b/src/components/ImageUpload/index.vue
index ae5cd45..1c6dcd1 100644
--- a/src/components/ImageUpload/index.vue
+++ b/src/components/ImageUpload/index.vue
@@ -44,6 +44,7 @@
 import { OssVO } from '@/api/system/oss/types';
 import { propTypes } from '@/utils/propTypes';
 import { globalHeaders } from '@/utils/request';
+import { compressAccurately } from 'image-conversion';
 
 const props = defineProps({
   modelValue: {
@@ -60,7 +61,14 @@
   isShowTip: {
     type: Boolean,
     default: true
-  }
+  },
+  // 鏄惁鏀寔鍘嬬缉锛岄粯璁ゅ惁
+  compressSupport: {
+    type: Boolean,
+    default: false
+  },
+  // 鍘嬬缉鐩爣澶у皬锛屽崟浣岾B銆傞粯璁�300KB浠ヤ笂鏂囦欢鎵嶅帇缂╋紝骞跺帇缂╄嚦300KB浠ュ唴
+  compressTargetSize: propTypes.number.def(300)
 });
 
 const { proxy } = getCurrentInstance() as ComponentInternalInstance;
@@ -138,8 +146,16 @@
       return false;
     }
   }
-  proxy?.$modal.loading('姝e湪涓婁紶鍥剧墖锛岃绋嶅��...');
-  number.value++;
+
+  //鍘嬬缉鍥剧墖锛屽紑鍚帇缂╁苟涓斿ぇ浜庢寚瀹氱殑鍘嬬缉澶у皬鏃舵墠鍘嬬缉
+  if (props.compressSupport && file.size / 1024 > props.compressTargetSize) {
+    proxy?.$modal.loading('姝e湪涓婁紶鍥剧墖锛岃绋嶅��...');
+    number.value++;
+    return compressAccurately(file, props.compressTargetSize);
+  } else {
+    proxy?.$modal.loading('姝e湪涓婁紶鍥剧墖锛岃绋嶅��...');
+    number.value++;
+  }
 };
 
 // 鏂囦欢涓暟瓒呭嚭

--
Gitblit v1.9.3