| ¶Ô±ÈÐÂÎļþ |
| | |
| | | <template>
|
| | | <div class="upload-file">
|
| | | <el-upload multiple :action="uploadFileUrl" :before-upload="handleBeforeUpload" :file-list="fileList" :data="data"
|
| | | :limit="limit" :on-error="handleUploadError" :on-exceed="handleExceed" :on-success="handleUploadSuccess"
|
| | | :show-file-list="false" :headers="headers" class="upload-file-uploader" ref="fileUpload" v-if="!disabled">
|
| | | <!-- ä¸ä¼ æé® -->
|
| | | <el-button type="primary">éåæä»¶</el-button>
|
| | | </el-upload>
|
| | | <!-- ä¸ä¼ æç¤º -->
|
| | | <div class="el-upload__tip" v-if="showTip && !disabled">
|
| | | 请ä¸ä¼
|
| | | <template v-if="fileSize">
|
| | | 大å°ä¸è¶
è¿ <b style="color: #f56c6c">{{ fileSize }}MB</b>
|
| | | </template>
|
| | | <template v-if="fileType">
|
| | | æ ¼å¼ä¸º <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
|
| | | </template>
|
| | | çæä»¶
|
| | | </div>
|
| | | <!-- æä»¶å表 -->
|
| | | <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
|
| | | <li :key="file.uid" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
|
| | | <el-link :href="`${baseUrl}${file.url}`" :underline="false" target="_blank">
|
| | | <span class="el-icon-document"> {{ getFileName(file.name) }} </span>
|
| | | </el-link>
|
| | | <div class="ele-upload-list__item-content-action">
|
| | | <el-link :underline="false" @click="handleDelete(index)" type="danger" v-if="!disabled"> å é¤</el-link>
|
| | | </div>
|
| | | </li>
|
| | | </transition-group>
|
| | | </div>
|
| | | </template>
|
| | |
|
| | | <script setup>
|
| | | import { getToken } from "@/utils/auth";
|
| | | import Sortable from "sortablejs";
|
| | |
|
| | | const props = defineProps({
|
| | | modelValue: [String, Object, Array],
|
| | | // ä¸ä¼ æ¥å£å°å
|
| | | action: {
|
| | | type: String,
|
| | | default: "/common/upload",
|
| | | },
|
| | | // ä¸ä¼ æºå¸¦çåæ°
|
| | | data: {
|
| | | type: Object,
|
| | | },
|
| | | // æ°ééå¶
|
| | | limit: {
|
| | | type: Number,
|
| | | default: 5,
|
| | | },
|
| | | // 大å°éå¶(MB)
|
| | | fileSize: {
|
| | | type: Number,
|
| | | default: 5,
|
| | | },
|
| | | // æä»¶ç±»å, ä¾å¦['png', 'jpg', 'jpeg']
|
| | | fileType: {
|
| | | type: Array,
|
| | | default: () => ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "pdf"],
|
| | | },
|
| | | // æ¯å¦æ¾ç¤ºæç¤º
|
| | | isShowTip: {
|
| | | type: Boolean,
|
| | | default: true,
|
| | | },
|
| | | // ç¦ç¨ç»ä»¶ï¼ä»
æ¥çæä»¶ï¼
|
| | | disabled: {
|
| | | type: Boolean,
|
| | | default: false,
|
| | | },
|
| | | // æå¨æåº
|
| | | drag: {
|
| | | type: Boolean,
|
| | | default: true,
|
| | | },
|
| | | });
|
| | |
|
| | | const { proxy } = getCurrentInstance();
|
| | | const emit = defineEmits();
|
| | | const number = ref(0);
|
| | | const uploadList = ref([]);
|
| | | const baseUrl = import.meta.env.VITE_APP_BASE_API;
|
| | | const uploadFileUrl = ref(import.meta.env.VITE_APP_BASE_API + props.action); // ä¸ä¼ æä»¶æå¡å¨å°å
|
| | | const headers = ref({ Authorization: "Bearer " + getToken() });
|
| | | const fileList = ref([]);
|
| | | const showTip = computed(
|
| | | () => props.isShowTip && (props.fileType || props.fileSize)
|
| | | );
|
| | |
|
| | | watch(
|
| | | () => props.modelValue,
|
| | | (val) => {
|
| | | if (val) {
|
| | | let temp = 1;
|
| | | // é¦å
å°å¼è½¬ä¸ºæ°ç»
|
| | | const list = Array.isArray(val) ? val : props.modelValue.split(",");
|
| | | // ç¶åå°æ°ç»è½¬ä¸ºå¯¹è±¡æ°ç»
|
| | | fileList.value = list.map((item) => {
|
| | | if (typeof item === "string") {
|
| | | item = { name: item, url: item };
|
| | | }
|
| | | item.uid = item.uid || new Date().getTime() + temp++;
|
| | | return item;
|
| | | });
|
| | | } else {
|
| | | fileList.value = [];
|
| | | return [];
|
| | | }
|
| | | },
|
| | | { deep: true, immediate: true }
|
| | | );
|
| | |
|
| | | // ä¸ä¼ åæ ¡æ£æ ¼å¼å大å°
|
| | | function handleBeforeUpload(file) {
|
| | | // æ ¡æ£æä»¶ç±»å
|
| | | if (props.fileType.length) {
|
| | | const fileName = file.name.split(".");
|
| | | const fileExt = fileName[fileName.length - 1];
|
| | | const isTypeOk = props.fileType.indexOf(fileExt) >= 0;
|
| | | if (!isTypeOk) {
|
| | | proxy.$modal.msgError(
|
| | | `æä»¶æ ¼å¼ä¸æ£ç¡®ï¼è¯·ä¸ä¼ ${props.fileType.join("/")}æ ¼å¼æä»¶!`
|
| | | );
|
| | | return false;
|
| | | }
|
| | | }
|
| | | // æ ¡æ£æä»¶åæ¯å¦å
å«ç¹æ®å符
|
| | | if (file.name.includes(",")) {
|
| | | proxy.$modal.msgError("æä»¶å䏿£ç¡®ï¼ä¸è½å
å«è±æéå·!");
|
| | | return false;
|
| | | }
|
| | | // æ ¡æ£æä»¶å¤§å°
|
| | | if (props.fileSize) {
|
| | | const isLt = file.size / 1024 / 1024 < props.fileSize;
|
| | | if (!isLt) {
|
| | | proxy.$modal.msgError(`ä¸ä¼ æä»¶å¤§å°ä¸è½è¶
è¿ ${props.fileSize} MB!`);
|
| | | return false;
|
| | | }
|
| | | }
|
| | | proxy.$modal.loading("æ£å¨ä¸ä¼ æä»¶ï¼è¯·ç¨å...");
|
| | | number.value++;
|
| | | return true;
|
| | | }
|
| | |
|
| | | // æä»¶ä¸ªæ°è¶
åº
|
| | | function handleExceed() {
|
| | | proxy.$modal.msgError(`ä¸ä¼ æä»¶æ°éä¸è½è¶
è¿ ${props.limit} 个!`);
|
| | | }
|
| | |
|
| | | // ä¸ä¼ 失败
|
| | | function handleUploadError(err) {
|
| | | proxy.$modal.msgError("ä¸ä¼ æä»¶å¤±è´¥");
|
| | | proxy.$modal.closeLoading();
|
| | | }
|
| | |
|
| | | // ä¸ä¼ æååè°
|
| | | function handleUploadSuccess(res, file) {
|
| | | if (res.code === 200) {
|
| | | uploadList.value.push({ name: res.fileName, url: res.fileName });
|
| | | uploadedSuccessfully();
|
| | | } else {
|
| | | number.value--;
|
| | | proxy.$modal.closeLoading();
|
| | | proxy.$modal.msgError(res.msg);
|
| | | proxy.$refs.fileUpload.handleRemove(file);
|
| | | uploadedSuccessfully();
|
| | | }
|
| | | }
|
| | |
|
| | | // å 餿件
|
| | | function handleDelete(index) {
|
| | | fileList.value.splice(index, 1);
|
| | | emit("update:modelValue", listToString(fileList.value));
|
| | | }
|
| | |
|
| | | // ä¸ä¼ ç»æå¤ç
|
| | | function uploadedSuccessfully() {
|
| | | if (number.value > 0 && uploadList.value.length === number.value) {
|
| | | fileList.value = fileList.value
|
| | | .filter((f) => f.url !== undefined)
|
| | | .concat(uploadList.value);
|
| | | uploadList.value = [];
|
| | | number.value = 0;
|
| | | emit("update:modelValue", listToString(fileList.value));
|
| | | proxy.$modal.closeLoading();
|
| | | }
|
| | | }
|
| | |
|
| | | // è·åæä»¶åç§°
|
| | | function getFileName(name) {
|
| | | // 妿æ¯urlé£ä¹åæåçåå 妿䏿¯ç´æ¥è¿å
|
| | | if (name.lastIndexOf("/") > -1) {
|
| | | return name.slice(name.lastIndexOf("/") + 1);
|
| | | } else {
|
| | | return name;
|
| | | }
|
| | | }
|
| | |
|
| | | // 对象转ææå®å符串åé
|
| | | function listToString(list, separator) {
|
| | | let strs = "";
|
| | | separator = separator || ",";
|
| | | for (let i in list) {
|
| | | if (list[i].url) {
|
| | | strs += list[i].url + separator;
|
| | | }
|
| | | }
|
| | | return strs != "" ? strs.substr(0, strs.length - 1) : "";
|
| | | }
|
| | |
|
| | | // åå§åææ½æåº
|
| | | onMounted(() => {
|
| | | if (props.drag) {
|
| | | nextTick(() => {
|
| | | const element = document.querySelector(".upload-file-list");
|
| | | Sortable.create(element, {
|
| | | ghostClass: "file-upload-darg",
|
| | | onEnd: (evt) => {
|
| | | const movedItem = fileList.value.splice(evt.oldIndex, 1)[0];
|
| | | fileList.value.splice(evt.newIndex, 0, movedItem);
|
| | | emit("update:modelValue", listToString(fileList.value));
|
| | | },
|
| | | });
|
| | | });
|
| | | }
|
| | | });
|
| | | </script>
|
| | | <style scoped lang="scss">
|
| | | .file-upload-darg {
|
| | | opacity: 0.5;
|
| | | background: #c8ebfb;
|
| | | }
|
| | |
|
| | | .upload-file-uploader {
|
| | | margin-bottom: 5px;
|
| | | }
|
| | |
|
| | | .upload-file-list .el-upload-list__item {
|
| | | border: 1px solid #e4e7ed;
|
| | | line-height: 2;
|
| | | margin-bottom: 10px;
|
| | | position: relative;
|
| | | transition: none !important;
|
| | | }
|
| | |
|
| | | .upload-file-list .ele-upload-list__item-content {
|
| | | display: flex;
|
| | | justify-content: space-between;
|
| | | align-items: center;
|
| | | color: inherit;
|
| | | }
|
| | |
|
| | | .ele-upload-list__item-content-action .el-link {
|
| | | margin-right: 10px;
|
| | | }
|
| | | </style>
|