(component): 新增拖拽上传支持, 待测试待整理

This commit is contained in:
就眠儀式 2022-10-13 15:36:57 +08:00
parent c2b1940942
commit 02facb2f01

View File

@ -12,6 +12,7 @@ import {
ComputedRef, ComputedRef,
getCurrentInstance, getCurrentInstance,
nextTick, nextTick,
onMounted,
ref, ref,
useSlots, useSlots,
withDefaults, withDefaults,
@ -129,22 +130,17 @@ const props = withDefaults(defineProps<LayUploadProps>(), {
const slot = useSlots(); const slot = useSlots();
const slots = slot.default && slot.default(); const slots = slot.default && slot.default();
const context = getCurrentInstance(); const context = getCurrentInstance();
const emit = defineEmits([ const emit = defineEmits(["choose","before","done","error","cutdone","cutcancel"]);
"choose",
"before",
"done",
"error",
"cutdone",
"cutcancel",
]);
const isDragEnter = ref(false); const isDragEnter = ref(false);
const activeUploadFiles = ref<any[]>([]); const activeUploadFiles = ref<any[]>([]);
const activeUploadFilesImgs = ref<any[]>([]); const activeUploadFilesImgs = ref<any[]>([]);
const orgFileInput = templateRef<HTMLElement>("orgFileInput"); const orgFileInput = templateRef<HTMLElement>("orgFileInput");
let _cropper: any = null; let _cropper: any = null;
let computedCutLayerOption: ComputedRef<LayerModal>; let computedCutLayerOption: ComputedRef<LayerModal>;
if (props.cutOptions && props.cutOptions.layerOption) {
if (props.cutOptions && props.cutOptions.layerOption) {
computedCutLayerOption = computed(() => computedCutLayerOption = computed(() =>
Object.assign(defaultCutLayerOption, props.cutOptions.layerOption) Object.assign(defaultCutLayerOption, props.cutOptions.layerOption)
); );
@ -231,24 +227,13 @@ const localUpload = (option: localUploadOption, callback: Function) => {
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
let currentTimeStamp = new Date().valueOf(); let currentTimeStamp = new Date().valueOf();
if (xhr.readyState === 1) { if (xhr.readyState === 1) {
if ( if ((xhr.status >= 200 && xhr.status <= 300) || xhr.status === 304 || xhr.status == 0) {
(xhr.status >= 200 && xhr.status <= 300) ||
xhr.status === 304 ||
xhr.status == 0
) {
let successText = "上传开始"; let successText = "上传开始";
emit( emit("before",Object.assign({ currentTimeStamp, msg: successText, ...option }));
"before",
Object.assign({ currentTimeStamp, msg: successText, ...option })
);
} }
} else if (xhr.readyState === 4) { } else if (xhr.readyState === 4) {
let successText = xhr.responseText ? xhr.responseText : uploadSuccess; let successText = xhr.responseText ? xhr.responseText : uploadSuccess;
if ( if ((xhr.status >= 200 && xhr.status <= 300) || xhr.status === 304 || xhr.status == 0) {
(xhr.status >= 200 && xhr.status <= 300) ||
xhr.status === 304 ||
xhr.status == 0
) {
let data = xhr.responseText; let data = xhr.responseText;
emit("done", { currentTimeStamp, msg: successText, data: data }); emit("done", { currentTimeStamp, msg: successText, data: data });
} }
@ -362,7 +347,25 @@ const clickOrgInput = () => {
emit("choose", currentTimeStamp); emit("choose", currentTimeStamp);
}; };
const cutTransaction = () => {}; const dragRef = ref();
function dragEnter(e: any){
e.stopPropagation();
e.preventDefault();
}
function dragOver(e: any){
e.stopPropagation();
e.preventDefault();
}
onMounted(() => {
nextTick(() => {
dragRef.value.addEventListener("dragenter",dragEnter,false);
dragRef.value.addEventListener("dragover",dragOver,false);
dragRef.value.addEventListener("drop",getUploadChange,false);
})
})
</script> </script>
<template> <template>
@ -394,14 +397,9 @@ const cutTransaction = () => {};
</div> </div>
<div <div
v-else v-else
ref="dragRef"
class="layui-upload-drag" class="layui-upload-drag"
:class=" :class="disabled ? 'layui-upload-drag-disable' : isDragEnter ? 'layui-upload-drag-draging' : ''"
disabled
? 'layui-upload-drag-disable'
: isDragEnter
? 'layui-upload-drag-draging'
: ''
"
@click.stop="chooseFile" @click.stop="chooseFile"
> >
<i class="layui-icon"></i> <i class="layui-icon"></i>