PageModal.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="registerModal" okText="保存" :title="getTitle" @ok="handleSubmit" :width="700" destroyOnClose>
  3. <BasicForm @register="registerForm" />
  4. <template #appendFooter>
  5. <a-button type="primary" @click="handleSubmit(1)">保存并设计</a-button>
  6. </template>
  7. </BasicModal>
  8. </template>
  9. <script lang="ts" setup>
  10. import { ref, computed, unref } from 'vue';
  11. import { BasicModal, useModalInner } from '/@/components/Modal';
  12. import { BasicForm, useForm } from '/@/components/Form/index';
  13. import { formSchema } from '../page.data';
  14. import { saveOrUpdate } from '../page.api';
  15. import { removeCacheByDynKey } from '/@/utils/auth';
  16. import { JDragConfigEnum } from '/@/enums/jeecgEnum';
  17. import { encryptByBase64, decodeByBase64 } from '/@/utils/cipher.ts';
  18. // 声明Emits
  19. const emit = defineEmits(['success', 'register']);
  20. const isUpdate = ref(true);
  21. //表单配置
  22. const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  23. schemas: formSchema,
  24. showActionButtonGroup: false,
  25. });
  26. //表单赋值
  27. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  28. //重置表单
  29. await resetFields();
  30. setModalProps({ confirmLoading: false });
  31. isUpdate.value = !!data?.isUpdate;
  32. if (unref(isUpdate)) {
  33. let obj = { ...data.record };
  34. //解密
  35. if (obj.protectionCode && obj.protectionCode.length > 0) {
  36. obj.protectionCode = decodeByBase64(obj.protectionCode);
  37. }
  38. //表单赋值
  39. await setFieldsValue({
  40. ...obj,
  41. });
  42. }
  43. });
  44. //设置标题
  45. const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
  46. /**
  47. * 表单提交事件
  48. * @param flag 标识0:保存/1:保存并设计
  49. */
  50. async function handleSubmit(flag = 0) {
  51. try {
  52. const values = await validate();
  53. setModalProps({ confirmLoading: true });
  54. //加密
  55. if (values.protectionCode) {
  56. values.protectionCode = encryptByBase64(values.protectionCode);
  57. }
  58. //提交表单
  59. const res = await saveOrUpdate(values, isUpdate.value);
  60. //编辑后,将缓存中的密码清除掉
  61. values.id && removeCacheByDynKey(JDragConfigEnum.DRAG_CACHE_PREFIX + values.id);
  62. //关闭弹窗
  63. closeModal();
  64. //刷新列表
  65. emit('success', flag === 1 ? res : null);
  66. } finally {
  67. setModalProps({ confirmLoading: false });
  68. }
  69. }
  70. </script>