houzhongjian
2024-07-11 759b1c71011abd6b58c37d2566f3f3c208c2f1b2
提交 | 用户 | 时间
759b1c 1 <template>
H 2   <div class="app-container">
3     <!-- 对话框(添加 / 修改) -->
4     <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
5       <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
6         <el-form-item label="名字" prop="name">
7           <el-input v-model="formData.name" placeholder="请输入名字" />
8         </el-form-item>
9         <el-form-item label="性别" prop="sex">
10           <el-radio-group v-model="formData.sex">
11             <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
12                       :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
13           </el-radio-group>
14         </el-form-item>
15         <el-form-item label="出生年" prop="birthday">
16           <el-date-picker clearable v-model="formData.birthday" type="date" value-format="timestamp" placeholder="选择出生年" />
17         </el-form-item>
18         <el-form-item label="简介">
19           <Editor v-model="formData.description" :min-height="192"/>
20         </el-form-item>
21         <el-form-item label="头像">
22           <ImageUpload v-model="formData.avatar"/>
23         </el-form-item>
24       </el-form>
25       <div slot="footer" class="dialog-footer">
26         <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
27         <el-button @click="dialogVisible = false">取 消</el-button>
28       </div>
29     </el-dialog>
30   </div>
31 </template>
32
33 <script>
34 import * as Demo01ContactApi from '@/api/infra/demo01';
35 import ImageUpload from '@/components/ImageUpload';
36 import Editor from '@/components/Editor';
37 export default {
38   name: "Demo01ContactForm",
39   components: {
40     ImageUpload,
41     Editor,
42   },
43   data() {
44     return {
45       // 弹出层标题
46       dialogTitle: "",
47       // 是否显示弹出层
48       dialogVisible: false,
49       // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
50       formLoading: false,
51       // 表单参数
52       formData: {
53         id: undefined,
54         name: undefined,
55         sex: undefined,
56         birthday: undefined,
57         description: undefined,
58         avatar: undefined,
59       },
60       // 表单校验
61       formRules: {
62         name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
63         sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
64         birthday: [{ required: true, message: '出生年不能为空', trigger: 'blur' }],
65         description: [{ required: true, message: '简介不能为空', trigger: 'blur' }],
66       },
67     };
68   },
69   methods: {
70     /** 打开弹窗 */
71     async open(id) {
72       this.dialogVisible = true;
73       this.reset();
74       // 修改时,设置数据
75       if (id) {
76         this.formLoading = true;
77         try {
78           const res = await Demo01ContactApi.getDemo01Contact(id);
79           this.formData = res.data;
80           this.title = "修改示例联系人";
81         } finally {
82           this.formLoading = false;
83         }
84       }
85       this.title = "新增示例联系人";
86     },
87     /** 提交按钮 */
88     async submitForm() {
89       // 校验主表
90       await this.$refs["formRef"].validate();
91       this.formLoading = true;
92       try {
93         const data = this.formData;
94         // 修改的提交
95         if (data.id) {
96           await Demo01ContactApi.updateDemo01Contact(data);
97           this.$modal.msgSuccess("修改成功");
98           this.dialogVisible = false;
99           this.$emit('success');
100           return;
101         }
102         // 添加的提交
103         await Demo01ContactApi.createDemo01Contact(data);
104         this.$modal.msgSuccess("新增成功");
105         this.dialogVisible = false;
106         this.$emit('success');
107       } finally {
108         this.formLoading = false;
109       }
110     },
111     /** 表单重置 */
112     reset() {
113       this.formData = {
114         id: undefined,
115         name: undefined,
116         sex: undefined,
117         birthday: undefined,
118         description: undefined,
119         avatar: undefined,
120       };
121       this.resetForm("formRef");
122     }
123   }
124 };
125 </script>