parent
cfb3675f99
commit
24ba936ab2
@ -0,0 +1,223 @@
|
|||||||
|
<template>
|
||||||
|
<div class="upload-box">
|
||||||
|
<div :class="sortable && 'upload-box__sortable'">
|
||||||
|
<ul v-if="sortable" ref="sortableRef" class="sortable">
|
||||||
|
<li v-for="(item, idx) in imgList" :key="item.uid" class="sortable--item">
|
||||||
|
<img :src="item.url" />
|
||||||
|
<span class="sortable--item-hover">
|
||||||
|
<el-icon class="sortable--item-icon" name="ZoomIn" @click="handlePreview(item)" />
|
||||||
|
<el-icon class="sortable--item-icon" name="Delete" @click="handleRemove(idx)" />
|
||||||
|
</span>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<el-upload
|
||||||
|
v-bind="props"
|
||||||
|
action="none"
|
||||||
|
:before-upload="handleBeforeUpload"
|
||||||
|
:class="{ max: imgList.length === props.limit, 'sortable--submit': sortable }"
|
||||||
|
:file-list="imgList"
|
||||||
|
:http-request="handleUpload"
|
||||||
|
list-type="text"
|
||||||
|
:on-exceed="handleExceed"
|
||||||
|
:show-file-list="!sortable"
|
||||||
|
>
|
||||||
|
<el-button type="primary">
|
||||||
|
<el-icon name="Plus" style="top: 0" />
|
||||||
|
<span>上传文件</span>
|
||||||
|
</el-button>
|
||||||
|
</el-upload>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="el-upload__tip">支持小于 {{ fmtSize }} 的 文件</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<script setup lang="jsx">
|
||||||
|
import { upload } from '@/api/file';
|
||||||
|
import { ElMessage } from '@/plugins/element-plus';
|
||||||
|
import 'element-plus/es/components/image/style/css';
|
||||||
|
import Sortable from 'sortablejs';
|
||||||
|
const props = defineProps({
|
||||||
|
configId: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
sortable: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
|
serviceName: {
|
||||||
|
type: String,
|
||||||
|
default: 'mall-product',
|
||||||
|
},
|
||||||
|
drag: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
|
multiple: {
|
||||||
|
type: Boolean,
|
||||||
|
defualt: false,
|
||||||
|
},
|
||||||
|
disabled: {
|
||||||
|
type: Boolean,
|
||||||
|
defualt: false,
|
||||||
|
},
|
||||||
|
limit: {
|
||||||
|
type: Number,
|
||||||
|
default: 1,
|
||||||
|
},
|
||||||
|
size: {
|
||||||
|
type: Number,
|
||||||
|
default: 1024 * 1024 * 20,
|
||||||
|
},
|
||||||
|
accept: {
|
||||||
|
type: String,
|
||||||
|
default: 'image/*',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const emits = defineEmits(['update:modelValue']);
|
||||||
|
const imgList = ref([]);
|
||||||
|
const attrs = useAttrs();
|
||||||
|
watch(
|
||||||
|
() => attrs.modelValue,
|
||||||
|
(value) => {
|
||||||
|
value = value instanceof Array ? value : [value];
|
||||||
|
if (
|
||||||
|
unref(imgList)
|
||||||
|
.map((item) => item.url)
|
||||||
|
.join(',') !== value?.join(',')
|
||||||
|
) {
|
||||||
|
imgList.value = value
|
||||||
|
.filter((item) => item)
|
||||||
|
.map((item) => {
|
||||||
|
return {
|
||||||
|
name: item,
|
||||||
|
response: item,
|
||||||
|
url: item,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ immediate: true, deep: true }
|
||||||
|
);
|
||||||
|
watch(
|
||||||
|
() => imgList,
|
||||||
|
() => {
|
||||||
|
const arr = unref(imgList).map((item) => item.response);
|
||||||
|
if (arr.every((item) => !!item)) {
|
||||||
|
const value = props.limit === 1 ? arr[0] : arr;
|
||||||
|
emits('update:modelValue', value);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ deep: true }
|
||||||
|
);
|
||||||
|
const handleRemove = (idx) => {
|
||||||
|
imgList.value.splice(idx, 1);
|
||||||
|
};
|
||||||
|
const handleExceed = (list) => {
|
||||||
|
console.info('[upload] exceed', list);
|
||||||
|
ElMessage.error('超出最大上传数量');
|
||||||
|
};
|
||||||
|
const handleBeforeUpload = (file) => {
|
||||||
|
console.info('[upload] upload', file);
|
||||||
|
let res = true;
|
||||||
|
if (file.type.startsWith('image/')) {
|
||||||
|
if (file.size >= props.size) {
|
||||||
|
ElMessage.error('超出文件大小限制');
|
||||||
|
res = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ElMessage.error('只允许上传图片');
|
||||||
|
res = false;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
const handleUpload = async ({ file }) => {
|
||||||
|
return await upload(props.serviceName, props.configId, file);
|
||||||
|
};
|
||||||
|
const fmtSize = computed(() => {
|
||||||
|
const units = ['byte', 'KB', 'MB', 'GB', 'TB'];
|
||||||
|
let res = props.size,
|
||||||
|
unit = 0;
|
||||||
|
while (res >= 800) {
|
||||||
|
res /= 1024;
|
||||||
|
unit++;
|
||||||
|
}
|
||||||
|
return res + units[unit];
|
||||||
|
});
|
||||||
|
const sortableRef = ref(null);
|
||||||
|
const sortableInit = () => {
|
||||||
|
new Sortable(sortableRef.value, {
|
||||||
|
animation: 150,
|
||||||
|
// swapThreshold: 1,
|
||||||
|
// fallbackOnBody: true,
|
||||||
|
onUpdate({ newIndex, oldIndex }) {
|
||||||
|
const newData = imgList.value[newIndex];
|
||||||
|
const oldData = imgList.value[oldIndex];
|
||||||
|
imgList.value[newIndex] = oldData;
|
||||||
|
imgList.value[oldIndex] = newData;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
onMounted(() => {
|
||||||
|
if (props.sortable) {
|
||||||
|
sortableInit();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<style lang="less" scoped>
|
||||||
|
.max {
|
||||||
|
:deep(.el-upload) {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.upload-box__sortable {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
.sortable {
|
||||||
|
--img-size: 148px;
|
||||||
|
display: inline-flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
margin: 0;
|
||||||
|
&--item {
|
||||||
|
overflow: hidden;
|
||||||
|
background-color: var(--el-fill-color-blank);
|
||||||
|
border: 1px solid #c0ccda;
|
||||||
|
border-radius: 6px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
width: var(--img-size);
|
||||||
|
height: var(--img-size);
|
||||||
|
margin: 0 8px 8px 0;
|
||||||
|
padding: 0;
|
||||||
|
display: inline-flex;
|
||||||
|
position: relative;
|
||||||
|
img {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
object-fit: contain;
|
||||||
|
}
|
||||||
|
&-icon {
|
||||||
|
margin: 0 10px;
|
||||||
|
font-size: 20px;
|
||||||
|
}
|
||||||
|
&-hover {
|
||||||
|
background: rgba(0, 0, 0, 0.5);
|
||||||
|
position: absolute;
|
||||||
|
left: 0;
|
||||||
|
top: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
|
color: #fff;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
opacity: 0;
|
||||||
|
display: flex;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
&:hover {
|
||||||
|
.sortable--item-hover {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
Loading…
Reference in new issue