|
@@ -1,21 +1,93 @@
|
|
|
<script setup lang="ts">
|
|
|
+import { ElForm } from "element-plus";
|
|
|
import { ref } from "vue";
|
|
|
-// import { mapPropToLabel, defaultFormData, helper } from "../utils/map";
|
|
|
+import { responseHandle } from "/@/utils/responseHandle";
|
|
|
+import { useNav } from "/@/layout/hooks/nav";
|
|
|
+import { create, edit } from "../utils/dialog-handler";
|
|
|
+import { createRules } from "../utils/create-rules";
|
|
|
+import {
|
|
|
+ mapPropToLabel,
|
|
|
+ defaultFormData,
|
|
|
+ helper
|
|
|
+} from "../utils/dialog-helper";
|
|
|
|
|
|
-// const visible = ref(true);
|
|
|
-// const formData = ref({ ...defaultFormData });
|
|
|
+enum DIALOG_TYPE {
|
|
|
+ create = "create",
|
|
|
+ edit = "edit"
|
|
|
+}
|
|
|
+
|
|
|
+const emit = defineEmits(["reload"]);
|
|
|
+
|
|
|
+const visible = ref(false);
|
|
|
+const editId = ref("");
|
|
|
+const formData = ref({ ...defaultFormData });
|
|
|
+const formRef = ref<InstanceType<typeof ElForm>>(null);
|
|
|
+const FROM_TYPE = ref<DIALOG_TYPE>(DIALOG_TYPE.create);
|
|
|
+const loading = ref(false);
|
|
|
+
|
|
|
+const rules = createRules();
|
|
|
+const { logout } = useNav();
|
|
|
+
|
|
|
+//显示dialog
|
|
|
+function show(item: any) {
|
|
|
+ visible.value = true;
|
|
|
+ const keys = Object.keys(formData.value);
|
|
|
+ const isCreate = Object.keys(item).length === 0;
|
|
|
+
|
|
|
+ editId.value = isCreate ? "" : item.id;
|
|
|
+
|
|
|
+ keys.forEach(key => {
|
|
|
+ formData.value[key] = isCreate ? "" : item[key];
|
|
|
+ });
|
|
|
+
|
|
|
+ FROM_TYPE.value = isCreate ? DIALOG_TYPE.create : DIALOG_TYPE.edit;
|
|
|
+}
|
|
|
+
|
|
|
+//创建/编辑 请求
|
|
|
+function handleSave() {
|
|
|
+ formRef.value.validate(async valid => {
|
|
|
+ if (!valid) return;
|
|
|
+ const handler = FROM_TYPE.value === DIALOG_TYPE.create ? create : edit;
|
|
|
+ const { data, api } = handler(formData.value, editId.value);
|
|
|
+ loading.value = true;
|
|
|
+ const { code, message } = await api(data);
|
|
|
+
|
|
|
+ responseHandle({
|
|
|
+ code,
|
|
|
+ message,
|
|
|
+ logout,
|
|
|
+ handler: () => emit("reload")
|
|
|
+ });
|
|
|
+
|
|
|
+ visible.value = false;
|
|
|
+ loading.value = false;
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+defineExpose({
|
|
|
+ show
|
|
|
+});
|
|
|
</script>
|
|
|
|
|
|
<template>
|
|
|
- <el-dialog v-model="visible">
|
|
|
- <el-form>
|
|
|
+ <el-dialog v-model="visible" destroy-on-close @close="loading = false">
|
|
|
+ <el-form ref="formRef" :model="formData" :rules="rules">
|
|
|
<el-form-item
|
|
|
v-for="(key, index) in Object.keys(mapPropToLabel)"
|
|
|
- :label="helper.getLabel(key)"
|
|
|
+ :label="helper.getLabel(key) + ':'"
|
|
|
+ label-width="150px"
|
|
|
+ :prop="key"
|
|
|
:key="index"
|
|
|
>
|
|
|
- <el-input />
|
|
|
+ <el-input v-model="formData[key]" />
|
|
|
</el-form-item>
|
|
|
+
|
|
|
+ <div class="flex justify-end">
|
|
|
+ <el-button @click="visible = false">取消</el-button>
|
|
|
+ <el-button :loading="loading" type="primary" @click="handleSave"
|
|
|
+ >保存</el-button
|
|
|
+ >
|
|
|
+ </div>
|
|
|
</el-form>
|
|
|
</el-dialog>
|
|
|
</template>
|