index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <div class="component-upload-image">
  3. <el-upload
  4. :action="uploadImgUrl"
  5. list-type="picture-card"
  6. :on-success="handleUploadSuccess"
  7. :before-upload="handleBeforeUpload"
  8. :limit="limit"
  9. :on-error="handleUploadError"
  10. :on-exceed="handleExceed"
  11. name="file"
  12. :on-remove="handleRemove"
  13. :show-file-list="true"
  14. :file-list="fileList"
  15. :on-preview="handlePictureCardPreview"
  16. :class="{hide: this.fileList.length >= this.limit}"
  17. >
  18. <i class="el-icon-plus"></i>
  19. </el-upload>
  20. <!-- 上传提示 -->
  21. <!-- <div class="el-upload__tip" slot="tip" v-if="showTip">
  22. 请上传
  23. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  24. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  25. 的文件
  26. </div> -->
  27. <el-dialog
  28. :visible.sync="dialogVisible"
  29. title="预览"
  30. width="800"
  31. append-to-body
  32. >
  33. <img
  34. :src="dialogImageUrl"
  35. style="display: block; max-width: 100%; margin: 0 auto"
  36. />
  37. </el-dialog>
  38. </div>
  39. </template>
  40. <script>
  41. import { getToken } from "@/utils/auth";
  42. import { Loading } from 'element-ui';
  43. export default {
  44. props: {
  45. value: [String, Object, Array],
  46. // 图片数量限制
  47. limit: {
  48. type: Number,
  49. default: 10,
  50. },
  51. // 大小限制(MB)
  52. fileSize: {
  53. type: Number,
  54. default: 500,
  55. },
  56. // 文件类型, 例如['png', 'jpg', 'jpeg']
  57. fileType: {
  58. type: Array,
  59. default: () => ["png", "jpg", "jpeg"],
  60. },
  61. // 是否显示提示
  62. isShowTip: {
  63. type: Boolean,
  64. default: true
  65. }
  66. },
  67. data() {
  68. return {
  69. finalQuality:1,
  70. dialogImageUrl: "",
  71. dialogVisible: false,
  72. hideUpload: false,
  73. baseUrl: process.env.VUE_APP_BASE_API,
  74. uploadImgUrl: process.env.VUE_APP_BASE_API+"/common/uploadOSS", // 上传的图片服务器地址
  75. headers: {
  76. Authorization: "Bearer " + getToken(),
  77. },
  78. fileList: []
  79. };
  80. },
  81. watch: {
  82. value: {
  83. handler(val) {
  84. if (val) {
  85. // 首先将值转为数组
  86. const list = Array.isArray(val) ? val : this.value.split(',');
  87. // 然后将数组转为对象数组
  88. this.fileList = list.map(item => {
  89. if (typeof item === "string") {
  90. if (item.indexOf(this.baseUrl) === -1) {
  91. item = { name: item, url: item };
  92. } else {
  93. item = { name: item, url: item };
  94. }
  95. }
  96. return item;
  97. });
  98. } else {
  99. this.fileList = [];
  100. return [];
  101. }
  102. },
  103. deep: true,
  104. immediate: true
  105. }
  106. },
  107. computed: {
  108. // 是否显示提示
  109. showTip() {
  110. return this.isShowTip && (this.fileType || this.fileSize);
  111. },
  112. },
  113. methods: {
  114. // 删除图片
  115. handleRemove(file, fileList) {
  116. const findex = this.fileList.map(f => f.name).indexOf(file.name);
  117. if(findex > -1) {
  118. this.fileList.splice(findex, 1);
  119. this.$emit("input", this.listToString(this.fileList));
  120. }
  121. },
  122. // 上传成功回调
  123. handleUploadSuccess(res) {
  124. console.log(res)
  125. this.fileList.push({ name: res.url, url: res.url });
  126. this.$emit("input", this.listToString(this.fileList));
  127. this.loading.close();
  128. },
  129. // 上传前loading加载
  130. handleBeforeUpload(file) {
  131. let isImg = false;
  132. if (this.fileType.length) {
  133. let fileExtension = "";
  134. if (file.name.lastIndexOf(".") > -1) {
  135. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  136. }
  137. isImg = this.fileType.some(type => {
  138. if (file.type.indexOf(type) > -1) return true;
  139. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  140. return false;
  141. });
  142. } else {
  143. isImg = file.type.indexOf("image") > -1;
  144. }
  145. if (!isImg) {
  146. this.$message.error(
  147. `文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`
  148. );
  149. return false;
  150. }
  151. return new Promise((resolve, reject) => {
  152. if (file.size / 1024 / 1024 > 3) {
  153. this.$message.error('上传的图片不能超过3MB');
  154. reject();
  155. return;
  156. }
  157. if (file.size / 1024 / 1024 > 1) {
  158. const loadingInstance = Loading.service({ text: '图片内存过大正在压缩图片...' });
  159. // 文件大于1MB时进行压缩
  160. this.compressImage(file).then((compressedFile) => {
  161. loadingInstance.close();
  162. if (compressedFile.size / 1024 > 1000) {
  163. this.$message.error('图片压缩后仍大于1000KB');
  164. reject();
  165. } else {
  166. // this.$message.success(`图片压缩成功,最终质量为: ${this.finalQuality.toFixed(2)}`);
  167. console.log(`图片压缩成功,最终质量为: ${this.finalQuality.toFixed(2)}`);
  168. console.log(`最终内存大小为: ${(compressedFile.size/1024).toFixed(2)}KB`);
  169. resolve(compressedFile);
  170. }
  171. }).catch((err) => {
  172. loadingInstance.close();
  173. console.error(err);
  174. reject();
  175. });
  176. } else {
  177. resolve(file);
  178. }
  179. this.loading = this.$loading({
  180. lock: true,
  181. text: "上传中",
  182. background: "rgba(0, 0, 0, 0.7)",
  183. });
  184. });
  185. // if (this.fileSize) {
  186. // const isLt = file.size / 1024 < this.fileSize;
  187. // if (!isLt) {
  188. // this.$message.error(`上传头像图片大小不能超过 ${this.fileSize} KB!`);
  189. // return false;
  190. // }
  191. // }
  192. },
  193. compressImage(file) {
  194. return new Promise((resolve, reject) => {
  195. const reader = new FileReader();
  196. reader.readAsDataURL(file);
  197. reader.onload = (event) => {
  198. const img = new Image();
  199. img.src = event.target.result;
  200. img.onload = () => {
  201. const canvas = document.createElement('canvas');
  202. const ctx = canvas.getContext('2d');
  203. const width = img.width;
  204. const height = img.height;
  205. canvas.width = width;
  206. canvas.height = height;
  207. ctx.drawImage(img, 0, 0, width, height);
  208. let quality = 1; // 初始压缩质量
  209. let dataURL = canvas.toDataURL('image/jpeg', quality);
  210. // 逐步压缩,直到图片大小小于500KB并且压缩质量不再降低
  211. while (dataURL.length / 1024 > 500 && quality > 0.1) {
  212. quality -= 0.01;
  213. dataURL = canvas.toDataURL('image/jpeg', quality);
  214. }
  215. this.finalQuality = quality; // 存储最终的压缩质量
  216. if (dataURL.length / 1024 > 1000) {
  217. reject(new Error('压缩后图片仍然大于1000KB'));
  218. return;
  219. }
  220. const arr = dataURL.split(',');
  221. const mime = arr[0].match(/:(.*?);/)[1];
  222. const bstr = atob(arr[1]);
  223. let n = bstr.length;
  224. const u8arr = new Uint8Array(n);
  225. while (n--) {
  226. u8arr[n] = bstr.charCodeAt(n);
  227. }
  228. const compressedFile = new Blob([u8arr], { type: mime });
  229. compressedFile.name = file.name;
  230. resolve(compressedFile);
  231. };
  232. img.onerror = (error) => {
  233. reject(error);
  234. };
  235. };
  236. reader.onerror = (error) => {
  237. reject(error);
  238. };
  239. });
  240. },
  241. // 文件个数超出
  242. handleExceed() {
  243. this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
  244. },
  245. // 上传失败
  246. handleUploadError() {
  247. this.$message({
  248. type: "error",
  249. message: "上传失败",
  250. });
  251. this.loading.close();
  252. },
  253. // 预览
  254. handlePictureCardPreview(file) {
  255. console.log(file)
  256. this.dialogImageUrl = file.url;
  257. this.dialogVisible = true;
  258. },
  259. // 对象转成指定字符串分隔
  260. listToString(list, separator) {
  261. let strs = "";
  262. separator = separator || ",";
  263. for (let i in list) {
  264. strs += list[i].url.replace(this.baseUrl, "") + separator;
  265. }
  266. return strs != '' ? strs.substr(0, strs.length - 1) : '';
  267. }
  268. }
  269. };
  270. </script>
  271. <style scoped lang="scss">
  272. // .el-upload--picture-card 控制加号部分
  273. ::v-deep.hide .el-upload--picture-card {
  274. display: none;
  275. }
  276. // 去掉动画效果
  277. ::v-deep .el-list-enter-active,
  278. ::v-deep .el-list-leave-active {
  279. transition: all 0s;
  280. }
  281. ::v-deep .el-list-enter, .el-list-leave-active {
  282. opacity: 0;
  283. transform: translateY(0);
  284. }
  285. </style>