index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <div class="component-upload-image">
  3. <el-upload
  4. multiple
  5. :action="uploadImgUrl"
  6. list-type="picture-card"
  7. :on-success="handleUploadSuccess"
  8. :before-upload="handleBeforeUpload"
  9. :limit="limit"
  10. :on-error="handleUploadError"
  11. :on-exceed="handleExceed"
  12. name="file"
  13. :on-remove="handleRemove"
  14. :show-file-list="true"
  15. :headers="headers"
  16. :file-list="fileList"
  17. :on-preview="handlePictureCardPreview"
  18. :class="{ hide: fileList.length >= limit }"
  19. >
  20. <el-icon class="avatar-uploader-icon"><plus /></el-icon>
  21. </el-upload>
  22. <!-- 上传提示 -->
  23. <div class="el-upload__tip" v-if="showTip">
  24. 请上传
  25. <template v-if="fileSize">
  26. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  27. </template>
  28. <template v-if="fileType">
  29. 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
  30. </template>
  31. 的文件
  32. </div>
  33. <el-dialog
  34. v-model="dialogVisible"
  35. title="预览"
  36. width="800px"
  37. append-to-body
  38. >
  39. <img
  40. :src="dialogImageUrl"
  41. style="display: block; max-width: 100%; margin: 0 auto"
  42. />
  43. </el-dialog>
  44. </div>
  45. </template>
  46. <script setup>
  47. import { getToken } from "@/utils/auth";
  48. const props = defineProps({
  49. modelValue: [String, Object, Array],
  50. // 图片数量限制
  51. limit: {
  52. type: Number,
  53. default: 5,
  54. },
  55. // 大小限制(MB)
  56. fileSize: {
  57. type: Number,
  58. default: 5,
  59. },
  60. // 文件类型, 例如['png', 'jpg', 'jpeg']
  61. fileType: {
  62. type: Array,
  63. default: () => ["png", "jpg", "jpeg"],
  64. },
  65. // 是否显示提示
  66. isShowTip: {
  67. type: Boolean,
  68. default: true
  69. },
  70. });
  71. const { proxy } = getCurrentInstance();
  72. const emit = defineEmits();
  73. const number = ref(0);
  74. const uploadList = ref([]);
  75. const dialogImageUrl = ref("");
  76. const dialogVisible = ref(false);
  77. const baseUrl = import.meta.env.VITE_APP_BASE_API;
  78. const uploadImgUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload"); // 上传的图片服务器地址
  79. const headers = ref({ Authorization: "Bearer " + getToken() });
  80. const fileList = ref([]);
  81. const showTip = computed(
  82. () => props.isShowTip && (props.fileType || props.fileSize)
  83. );
  84. watch(() => props.modelValue, val => {
  85. if (val) {
  86. // 首先将值转为数组
  87. const list = Array.isArray(val) ? val : props.modelValue.split(",");
  88. // 然后将数组转为对象数组
  89. fileList.value = list.map(item => {
  90. if (typeof item === "string") {
  91. if (item.indexOf(baseUrl) === -1) {
  92. item = { name: baseUrl + item, url: baseUrl + item };
  93. } else {
  94. item = { name: item, url: item };
  95. }
  96. }
  97. return item;
  98. });
  99. } else {
  100. fileList.value = [];
  101. return [];
  102. }
  103. },{ deep: true, immediate: true });
  104. // 删除图片
  105. function handleRemove(file, files) {
  106. emit("update:modelValue", listToString(fileList.value));
  107. }
  108. // 上传成功回调
  109. function handleUploadSuccess(res) {
  110. uploadList.value.push({ name: res.fileName, url: res.fileName });
  111. if (uploadList.value.length === number.value) {
  112. fileList.value = fileList.value.filter(f => f.url !== undefined).concat(uploadList.value);
  113. uploadList.value = [];
  114. number.value = 0;
  115. emit("update:modelValue", listToString(fileList.value));
  116. proxy.$modal.closeLoading();
  117. }
  118. }
  119. // 上传前loading加载
  120. function handleBeforeUpload(file) {
  121. let isImg = false;
  122. if (props.fileType.length) {
  123. let fileExtension = "";
  124. if (file.name.lastIndexOf(".") > -1) {
  125. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  126. }
  127. isImg = props.fileType.some(type => {
  128. if (file.type.indexOf(type) > -1) return true;
  129. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  130. return false;
  131. });
  132. } else {
  133. isImg = file.type.indexOf("image") > -1;
  134. }
  135. if (!isImg) {
  136. proxy.$modal.msgError(
  137. `文件格式不正确, 请上传${props.fileType.join("/")}图片格式文件!`
  138. );
  139. return false;
  140. }
  141. if (props.fileSize) {
  142. const isLt = file.size / 1024 / 1024 < props.fileSize;
  143. if (!isLt) {
  144. proxy.$modal.msgError(`上传头像图片大小不能超过 ${props.fileSize} MB!`);
  145. return false;
  146. }
  147. }
  148. proxy.$modal.loading("正在上传图片,请稍候...");
  149. number.value++;
  150. }
  151. // 文件个数超出
  152. function handleExceed() {
  153. proxy.$modal.msgError(`上传文件数量不能超过 ${props.limit} 个!`);
  154. }
  155. // 上传失败
  156. function handleUploadError() {
  157. proxy.$modal.msgError("上传图片失败");
  158. proxy.$modal.closeLoading();
  159. }
  160. // 预览
  161. function handlePictureCardPreview(file) {
  162. dialogImageUrl.value = file.url;
  163. dialogVisible.value = true;
  164. }
  165. // 对象转成指定字符串分隔
  166. function listToString(list, separator) {
  167. let strs = "";
  168. separator = separator || ",";
  169. for (let i in list) {
  170. if (undefined !== list[i].url && list[i].url.indexOf("blob:") !== 0) {
  171. strs += list[i].url.replace(baseUrl, "") + separator;
  172. }
  173. }
  174. return strs != "" ? strs.substr(0, strs.length - 1) : "";
  175. }
  176. </script>