index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <div class="upload-file">
  3. <el-upload
  4. multiple
  5. :action="uploadFileUrl"
  6. :before-upload="handleBeforeUpload"
  7. :file-list="fileList"
  8. :limit="limit"
  9. :on-error="handleUploadError"
  10. :on-exceed="handleExceed"
  11. :on-success="handleUploadSuccess"
  12. :show-file-list="false"
  13. :headers="headers"
  14. class="upload-file-uploader"
  15. ref="upload"
  16. >
  17. <!-- 上传按钮 -->
  18. <el-button type="primary">选取文件</el-button>
  19. </el-upload>
  20. <!-- 上传提示 -->
  21. <div class="el-upload__tip" v-if="showTip">
  22. 请上传
  23. <template v-if="fileSize">
  24. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  25. </template>
  26. <template v-if="fileType">
  27. 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
  28. </template>
  29. 的文件
  30. </div>
  31. <!-- 文件列表 -->
  32. <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
  33. <li :key="file.uid" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
  34. <el-link :href="file.url" :underline="false" target="_blank">
  35. <span class="el-icon-document"> {{ file.name || file.url }} </span>
  36. </el-link>
  37. <div class="ele-upload-list__item-content-action">
  38. <el-link :underline="false" @click="handleDelete(index)" type="danger">删除</el-link>
  39. </div>
  40. </li>
  41. </transition-group>
  42. </div>
  43. </template>
  44. <script setup>
  45. import { getToken } from "@/utils/auth";
  46. const props = defineProps({
  47. modelValue: {
  48. type: [String, Object, Array],
  49. default: "",
  50. },
  51. // 数量限制
  52. limit: {
  53. type: Number,
  54. default: 5,
  55. },
  56. // 大小限制(MB)
  57. fileSize: {
  58. type: Number,
  59. default: 5,
  60. },
  61. // 文件类型, 例如['png', 'jpg', 'jpeg']
  62. fileType: {
  63. type: Array,
  64. // default: () => ["doc", "xls", "ppt", "txt", "pdf"],
  65. default: () => [],
  66. },
  67. // 是否显示提示
  68. isShowTip: {
  69. type: Boolean,
  70. default: true,
  71. },
  72. });
  73. const { proxy } = getCurrentInstance();
  74. const emit = defineEmits();
  75. const uploadFileUrl = ref(import.meta.env.VITE_APP_BASE_API + "/service-file/upload"); // 上传的文件服务器地址
  76. const headers = ref({ Authorization: localStorage.getItem("token725") });
  77. const fileList = ref([]);
  78. const showTip = computed(() => props.isShowTip && (props.fileType || props.fileSize));
  79. // 删除文件
  80. function handleDelete(index) {
  81. fileList.value.splice(index, 1);
  82. if (props.limit > 1) {
  83. } else {
  84. emit("update", listToString(fileList.value));
  85. }
  86. }
  87. // 上传成功回调
  88. function handleUploadSuccess(res, file) {
  89. fileList.value.push(res.data);
  90. if (props.limit > 1) {
  91. emit("update:modelValue", fileList.value);
  92. } else {
  93. emit("update", listToString(fileList.value));
  94. }
  95. proxy.$modal.closeLoading();
  96. }
  97. // 上传前校检格式和大小
  98. function handleBeforeUpload(file) {
  99. // 校检文件类型
  100. if (props.fileType.length) {
  101. let fileExtension = "";
  102. if (file.name.lastIndexOf(".") > -1) {
  103. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  104. }
  105. const isTypeOk = props.fileType.some((type) => {
  106. if (file.type.indexOf(type) > -1) return true;
  107. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  108. return false;
  109. });
  110. if (!isTypeOk) {
  111. proxy.$modal.msgError(`文件格式不正确, 请上传${props.fileType.join("/")}格式文件!`);
  112. return false;
  113. }
  114. }
  115. // 校检文件大小
  116. if (props.fileSize) {
  117. const isLt = file.size / 1024 / 1024 < props.fileSize;
  118. if (!isLt) {
  119. proxy.$modal.msgError(`上传文件大小不能超过 ${props.fileSize} MB!`);
  120. return false;
  121. }
  122. }
  123. proxy.$modal.loading("正在上传文件,请稍候...");
  124. }
  125. // 文件个数超出
  126. function handleExceed() {
  127. proxy.$modal.msgError(`上传文件数量不能超过 ${props.limit} 个!`);
  128. }
  129. // 上传失败
  130. function handleUploadError(err) {
  131. proxy.$modal.msgError("上传文件失败");
  132. proxy.$modal.closeLoading();
  133. }
  134. // 对象转成指定字符串分隔
  135. function listToString(list, separator) {
  136. let strs = "";
  137. separator = separator || ",";
  138. for (let i in list) {
  139. if (undefined !== list[i].url) {
  140. strs += list[i].url + separator;
  141. }
  142. }
  143. return strs != "" ? strs.substr(0, strs.length - 1) : "";
  144. }
  145. watch(
  146. () => props.modelValue,
  147. (val) => {
  148. if (props.limit > 1) {
  149. fileList.value = val;
  150. } else {
  151. fileList.value = val ? [{ name: val.split("/")[val.split("/").length - 1], url: val }] : [];
  152. }
  153. },
  154. { deep: true, immediate: true }
  155. );
  156. onMounted(() => {
  157. if (props.modelValue) {
  158. fileList.value = props.limit > 1 ? props.modelValue : [{ name: props.modelValue.split("/")[props.modelValue.split("/").length - 1], url: props.modelValue }];
  159. }
  160. });
  161. </script>
  162. <style scoped lang="scss">
  163. .upload-file-uploader {
  164. margin-bottom: 5px;
  165. }
  166. .upload-file-list .el-upload-list__item {
  167. border: 1px solid #e4e7ed;
  168. line-height: 2;
  169. margin-bottom: 10px;
  170. position: relative;
  171. }
  172. .upload-file-list .ele-upload-list__item-content {
  173. height: 32px;
  174. line-height: 32px;
  175. padding: 0 10px 0 10px;
  176. display: flex;
  177. justify-content: space-between;
  178. align-items: center;
  179. color: inherit;
  180. }
  181. </style>