gaoluyang
2 小时以前 787ccc59ba89bacc075562a161ecf02bc76ebadc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<script lang="ts" setup>
import type { Rule } from 'ant-design-vue/es/form';
 
import type { Demo03StudentApi } from '#/api/infra/demo/demo03/erp';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { Form, Input, message } from 'ant-design-vue';
 
import {
  createDemo03Course,
  getDemo03Course,
  updateDemo03Course,
} from '#/api/infra/demo/demo03/erp';
import { $t } from '#/locales';
 
const emit = defineEmits(['success']);
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['学生课程'])
    : $t('ui.actionTitle.create', ['学生课程']);
});
 
const formRef = ref();
const formData = ref<Partial<Demo03StudentApi.Demo03Course>>({
  id: undefined,
  studentId: undefined,
  name: undefined,
  score: undefined,
});
const rules: Record<string, Rule[]> = {
  studentId: [{ required: true, message: '学生编号不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
  score: [{ required: true, message: '分数不能为空', trigger: 'blur' }],
};
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    await formRef.value?.validate();
 
    modalApi.lock();
    // 提交表单
    const data = formData.value as Demo03StudentApi.Demo03Course;
    try {
      await (formData.value?.id
        ? updateDemo03Course(data)
        : createDemo03Course(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      resetForm();
      return;
    }
 
    // 加载数据
    let data = modalApi.getData<Demo03StudentApi.Demo03Course>();
    if (!data) {
      return;
    }
    if (data.id) {
      modalApi.lock();
      try {
        data = await getDemo03Course(data.id);
      } finally {
        modalApi.unlock();
      }
    }
    // 设置到 values
    formData.value = data;
  },
});
 
/** 重置表单 */
function resetForm() {
  formData.value = {
    id: undefined,
    studentId: undefined,
    name: undefined,
    score: undefined,
  };
  formRef.value?.resetFields();
}
</script>
 
<template>
  <Modal :title="getTitle">
    <Form
      ref="formRef"
      :model="formData"
      :rules="rules"
      :label-col="{ span: 5 }"
      :wrapper-col="{ span: 18 }"
    >
      <Form.Item label="学生编号" name="studentId">
        <Input
          v-model:value="formData.studentId"
          placeholder="请输入学生编号"
        />
      </Form.Item>
      <Form.Item label="名字" name="name">
        <Input v-model:value="formData.name" placeholder="请输入名字" />
      </Form.Item>
      <Form.Item label="分数" name="score">
        <Input v-model:value="formData.score" placeholder="请输入分数" />
      </Form.Item>
    </Form>
  </Modal>
</template>