1
0

AccountModal.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
  3. <BasicForm @register="registerForm" />
  4. </BasicModal>
  5. </template>
  6. <script lang="ts">
  7. import { defineComponent, ref, computed, unref } from 'vue';
  8. import { BasicModal, useModalInner } from '/@/components/Modal';
  9. import { BasicForm, useForm } from '/@/components/Form/index';
  10. import { accountFormSchema } from './account.data';
  11. import { getDeptList } from '/@/api/demo/system';
  12. export default defineComponent({
  13. name: 'AccountModal',
  14. components: { BasicModal, BasicForm },
  15. emits: ['success', 'register'],
  16. setup(_, { emit }) {
  17. const isUpdate = ref(true);
  18. const rowId = ref('');
  19. const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
  20. labelWidth: 100,
  21. schemas: accountFormSchema,
  22. showActionButtonGroup: false,
  23. actionColOptions: {
  24. span: 23,
  25. },
  26. });
  27. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  28. resetFields();
  29. setModalProps({ confirmLoading: false });
  30. isUpdate.value = !!data?.isUpdate;
  31. if (unref(isUpdate)) {
  32. rowId.value = data.record.id;
  33. setFieldsValue({
  34. ...data.record,
  35. });
  36. }
  37. const treeData = await getDeptList();
  38. updateSchema([
  39. {
  40. field: 'pwd',
  41. show: !unref(isUpdate),
  42. },
  43. {
  44. field: 'dept',
  45. componentProps: { treeData },
  46. },
  47. ]);
  48. });
  49. const getTitle = computed(() => (!unref(isUpdate) ? '新增账号' : '编辑账号'));
  50. async function handleSubmit() {
  51. try {
  52. const values = await validate();
  53. setModalProps({ confirmLoading: true });
  54. // TODO custom api
  55. console.log(values);
  56. closeModal();
  57. emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
  58. } finally {
  59. setModalProps({ confirmLoading: false });
  60. }
  61. }
  62. return { registerModal, registerForm, getTitle, handleSubmit };
  63. },
  64. });
  65. </script>