!105 重构upload,支持跟随表单提交,补充form相关文档

Merge pull request !105 from geekreal/refactor-upload-withform
This commit is contained in:
就眠儀式 2022-11-12 15:01:40 +00:00 committed by Gitee
commit 238ff5fc11
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 41 additions and 4 deletions

View File

@ -17,6 +17,7 @@ import {
ref, ref,
useSlots, useSlots,
withDefaults, withDefaults,
watch,
} from "vue"; } from "vue";
import { templateRef } from "@vueuse/core"; import { templateRef } from "@vueuse/core";
import { LayLayer } from "@layui/layer-vue"; import { LayLayer } from "@layui/layer-vue";
@ -70,6 +71,7 @@ export interface UploadProps {
cutOptions?: CutOptions; cutOptions?: CutOptions;
text?: string; text?: string;
dragText?: string; dragText?: string;
modelValue?: any;
} }
const getCutDownResult = () => { const getCutDownResult = () => {
@ -83,6 +85,11 @@ const getCutDownResult = () => {
Object.assign({ currentTimeStamp, cutResult: imgData, orginal: orgInfo }) Object.assign({ currentTimeStamp, cutResult: imgData, orginal: orgInfo })
); );
let newFile = dataURLtoFile(imgData); let newFile = dataURLtoFile(imgData);
if (isInFormItem.value) {
emit("update:modelValue", [newFile]);
clearLightCutEffect();
return;
}
commonUploadTransaction([newFile]); commonUploadTransaction([newFile]);
nextTick(() => clearAllCutEffect()); nextTick(() => clearAllCutEffect());
} else { } else {
@ -104,6 +111,13 @@ const clearAllCutEffect = () => {
_cropper = null; _cropper = null;
}; };
const clearLightCutEffect = () => {
activeUploadFiles.value = [];
activeUploadFilesImgs.value = [];
innerCutVisible.value = false;
_cropper = null;
};
const { t } = useI18n(); const { t } = useI18n();
const text = computed(() => { const text = computed(() => {
return props.text ? props.text : t("upload.text"); return props.text ? props.text : t("upload.text");
@ -148,6 +162,7 @@ const props = withDefaults(defineProps<UploadProps>(), {
disabledPreview: false, disabledPreview: false,
cut: false, cut: false,
cutOptions: void 0, cutOptions: void 0,
modelValue: null,
}); });
const slot = useSlots(); const slot = useSlots();
@ -160,8 +175,21 @@ const emit = defineEmits([
"error", "error",
"cutdone", "cutdone",
"cutcancel", "cutcancel",
"update:modelValue",
]); ]);
const isInFormItem = computed(() => context?.parent?.type.name === 'LayFormItem');
watch(
() => props.modelValue,
() => {
if (!props.modelValue) {
clearAllCutEffect();
}
}
);
const isDragEnter = ref(false); const isDragEnter = ref(false);
const activeUploadFiles = ref<any[]>([]); const activeUploadFiles = ref<any[]>([]);
const activeUploadFilesImgs = ref<any[]>([]); const activeUploadFilesImgs = ref<any[]>([]);
@ -219,7 +247,7 @@ const localUploadTransaction = (option: localUploadTransaction) => {
const dataURLtoFile = (dataurl: string) => { const dataURLtoFile = (dataurl: string) => {
let arr: any[] = dataurl.split(","); let arr: any[] = dataurl.split(",");
let mime: string = ""; let mime = "";
if (arr.length > 0) { if (arr.length > 0) {
mime = arr[0].match(/:(.*?);/)[1]; mime = arr[0].match(/:(.*?);/)[1];
} }
@ -323,12 +351,13 @@ const uploadChange = (e: any) => {
} }
let arm1 = let arm1 =
props.cut && props.cut &&
props.acceptMime.indexOf("image") != -1 && (props.acceptMime.indexOf("image") != -1 || isInFormItem.value) &&
props.multiple == false; props.multiple == false;
let arm2 = let arm2 =
props.cut && props.cut &&
props.acceptMime.indexOf("image") != -1 && (props.acceptMime.indexOf("image") != -1 || isInFormItem.value) &&
props.multiple == true; props.multiple == true;
if (arm1) { if (arm1) {
innerCutVisible.value = true; innerCutVisible.value = true;
setTimeout(() => { setTimeout(() => {
@ -347,6 +376,10 @@ const uploadChange = (e: any) => {
if (arm2) { if (arm2) {
console.warn(cannotSupportCutMsg.value); console.warn(cannotSupportCutMsg.value);
} }
if (isInFormItem.value) {
emit("update:modelValue", _files);
return;
}
commonUploadTransaction(_files); commonUploadTransaction(_files);
} }
}; };

View File

@ -173,6 +173,9 @@ export default {
<lay-form-item label="描述" prop="desc"> <lay-form-item label="描述" prop="desc">
<lay-textarea placeholder="请输入描述" v-model="validateModel.desc"></lay-textarea> <lay-textarea placeholder="请输入描述" v-model="validateModel.desc"></lay-textarea>
</lay-form-item> </lay-form-item>
<lay-form-item label="文件" prop="file">
<lay-upload v-model="validateModel.file" :cut="true"/>
</lay-form-item>
<lay-form-item> <lay-form-item>
<lay-button @click="validate">提交</lay-button> <lay-button @click="validate">提交</lay-button>
<lay-button @click="clearValidate">清除校验</lay-button> <lay-button @click="clearValidate">清除校验</lay-button>
@ -192,7 +195,8 @@ export default {
username: "", username: "",
password: "", password: "",
specialty: "1", specialty: "1",
hobbys: "" hobbys: "",
file: null,
}) })
const layFormRef = ref(null); const layFormRef = ref(null);