安装
1 | npm install vuedraggable |
引入
1 | import vuedraggable from 'vuedraggable' |
写成组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | <template> <div class="upload-container"> <vuedraggable v-model="sortImages" class="draggable-container" @change="changeImages"> <div v-for="(image, index) in sortImages" :key="index" class="image-container" @mouseover="mouseover(index)" @mouseleave="mouseleave(index)"> <img v-if="type === 1" class="image" :src="image"> <template v-else> <div v-if="index >= 0"> <i class="el-icon-document" />{{ image.fileName }} </div> </template> <i v-if="index === currentIndex" class="el-icon-close close" @click="removeImage(image)" /> </div> </vuedraggable> <el-upload action="#" :accept="acceptConfig" :show-file-list="false" :on-change="customUpload" :auto-upload="false" :disabled="disabled" > <el-button v-if="sortImages.length < limit" size="small" :disabled="disabled">{{ type === 1 ? '添加图片' : '添加文件' }}</el-button> <el-button v-if="sortImages.length === limit" size="small" :disabled="disabled">{{ type === 1 ? '更换图片' : '更换文件' }}</el-button> </el-upload> </div> </template> <script> import upload from '@/utils/upload' import vuedraggable from 'vuedraggable' import moment from 'moment' export default { name: 'SortFile', components: { vuedraggable }, props: { // eslint-disable-next-line vue/require-default-prop images: [String, Array], limit: { type: Number, default: 1 }, type: { type: Number, default: 1 }, uploadConfig: { type: Object, required: true, default: function() { return {} } }, disabled: { type: Boolean, default: false } }, data() { return { currentIndex: -1, sortImages: !this.images ? [] : [].concat(this.images), acceptConfig: 'image/jpeg,image/png,image/jpg' } }, watch: { images(val) { this.sortImages = !this.images ? [] : [].concat(this.images) } }, created() { if (this.type === 2) { this.acceptConfig = 'application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,.txt' } }, methods: { customUpload(file) { const that = this if (that.type === 1) { const isLt2M = file.size / 1024 / 1024 < 2 if (!isLt2M) { this.$message.error('上传图片大小不能超过2M') return false } if (['image/jpeg', 'image/png', 'image/jpg'].indexOf(file.raw.type) === -1) { this.$message.error('格式为JPG、JPEG、PNG') return false } } if (that.type === 2) { const isLt100M = file.size / 1024 / 1024 < 100 if (!isLt100M) { this.$message.error('上传附件大小不能超过100M') return false } } const currentTime = moment(Date.now()).format('YYYYMMDDHHmmss') const name = file.name.split('.')[0] const type = file.name.split('.')[1] const copyFile = new File([file.raw], `${name}_${currentTime}.${type}`) upload(copyFile, this.uploadConfig, function(url) { if (url) { if (that.sortImages.length < that.limit) { if (that.type === 1) { that.sortImages.push(url) } if (that.type === 2) { that.sortImages.push({ uri: url, fileName: file.name, fileSize: file.size / 1024 / 1024 }) } } else { that.sortImages.splice(that.sortImages.length - 1, 1, that.type === 1 ? url : { uri: url, fileName: file.name, fileSize: file.size / 1024 / 1024 }) } that.$emit('update:images', that.sortImages) } }) }, mouseover(index) { this.currentIndex = index }, mouseleave(index) { this.currentIndex = -1 }, removeImage(image) { const index = this.sortImages.findIndex(item => { return item === image }) this.sortImages.splice(index, 1) this.$emit('update:images', this.sortImages) }, changeImages() { this.$emit('update:images', this.sortImages) } } } </script> <style lang="scss" scoped> .upload-container { width: 100%; display: flex; align-items: flex-end; .draggable-container { display: flex; .image-container { position: relative; width: 152px; height: 86px; margin-right: 17px; img { width: 100%; height: 100%; } .close { position: absolute; top: 4px; right: 4px; background: rgba(255, 255, 255, 1); border-radius: 50%; font-weight: 600; } } } .el-button { background: #EEF5FF; border: 1px solid #CFE3FD; color: #5E9FF8; } } </style> |
引用组件
1 | <SortFile :images.sync="logoUrl" :limit="1" :upload-config="uploadLogoConfig" /> |
参数说明
import upload from '@/utils/upload' 这里的upload.js在: https://blog.csdn.net/i15730384741/article/details/106300278- limit:当前上传限制图片张数
- type : 默认为1,1代表上传图片,2代表上传文件
- upload-config : 图片上传需要的config配置