潘志宝
2024-09-09 ed81b7371e376df35448b81531d30dd9024bd44a
提交 | 用户 | 时间
e7c126 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="description">
10                       <el-input v-model="formData.description" type="textarea" placeholder="请输入内容" />
11                     </el-form-item>
12                     <el-form-item label="出生日期" prop="birthday">
13                       <el-date-picker clearable v-model="formData.birthday" type="date" value-format="timestamp" placeholder="选择出生日期" />
14                     </el-form-item>
15                     <el-form-item label="性别" prop="sex">
16                       <el-select v-model="formData.sex" placeholder="请选择性别">
17                             <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
18                                        :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
19                       </el-select>
20                     </el-form-item>
21                     <el-form-item label="是否有效" prop="enabled">
22                       <el-radio-group v-model="formData.enabled">
23                             <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
24                                       :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
25                       </el-radio-group>
26                     </el-form-item>
27                     <el-form-item label="头像" prop="avatar">
28                       <ImageUpload v-model="formData.avatar"/>
29                     </el-form-item>
30                     <el-form-item label="附件" prop="video">
31                       <FileUpload v-model="formData.video"/>
32                     </el-form-item>
33                     <el-form-item label="备注" prop="memo">
34                       <editor v-model="formData.memo" :min-height="192"/>
35                     </el-form-item>
36       </el-form>
37       <div slot="footer" class="dialog-footer">
38         <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
39         <el-button @click="dialogVisible = false">取 消</el-button>
40       </div>
41     </el-dialog>
42   </div>
43 </template>
44
45 <script>
46   import * as StudentApi from '@/api/infra/demo';
47       import ImageUpload from '@/components/ImageUpload';
48       import FileUpload from '@/components/FileUpload';
49       import Editor from '@/components/Editor';
50   export default {
51     name: "StudentContactForm",
52     components: {
53           ImageUpload,
54           FileUpload,
55           Editor,
56     },
57     data() {
58       return {
59         // 弹出层标题
60         dialogTitle: "",
61         // 是否显示弹出层
62         dialogVisible: false,
63         // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
64         formLoading: false,
65         // 表单参数
66         formData: {
67                             id: undefined,
68                             studentId: undefined,
69                             name: undefined,
70                             description: undefined,
71                             birthday: undefined,
72                             sex: undefined,
73                             enabled: undefined,
74                             avatar: undefined,
75                             video: undefined,
76                             memo: undefined,
77         },
78         // 表单校验
79         formRules: {
80                         studentId: [{ required: true, message: "学生编号不能为空", trigger: "blur" }],
81                         name: [{ required: true, message: "名字不能为空", trigger: "blur" }],
82                         description: [{ required: true, message: "简介不能为空", trigger: "blur" }],
83                         birthday: [{ required: true, message: "出生日期不能为空", trigger: "blur" }],
84                         sex: [{ required: true, message: "性别不能为空", trigger: "change" }],
85                         enabled: [{ required: true, message: "是否有效不能为空", trigger: "blur" }],
86                         avatar: [{ required: true, message: "头像不能为空", trigger: "blur" }],
87                         memo: [{ required: true, message: "备注不能为空", trigger: "blur" }],
88         },
89       };
90     },
91     methods: {
92       /** 打开弹窗 */
93       async open(id, studentId) {
94         this.dialogVisible = true;
95         this.reset();
96         this.formData.studentId = studentId;
97         // 修改时,设置数据
98         if (id) {
99           this.formLoading = true;
100           try {
101             const res = await StudentApi.getStudentContact(id);
102             this.formData = res.data;
103             this.dialogTitle = "修改学生联系人";
104           } finally {
105             this.formLoading = false;
106           }
107         }
108         this.dialogTitle = "新增学生联系人";
109       },
110       /** 提交按钮 */
111       async submitForm() {
112         await this.$refs["formRef"].validate();
113         this.formLoading = true;
114         try {
115             const data = this.formData;
116             // 修改的提交
117             if (data.id) {
118             await  StudentApi.updateStudentContact(data);
119             this.$modal.msgSuccess("修改成功");
120             this.dialogVisible = false;
121             this.$emit('success');
122               return;
123             }
124             // 添加的提交
125               await StudentApi.createStudentContact(data);
126               this.$modal.msgSuccess("新增成功");
127               this.dialogVisible = false;
128               this.$emit('success');
129         }finally {
130           this.formLoading = false;
131         }
132       },
133       /** 表单重置 */
134       reset() {
135         this.formData = {
136                             id: undefined,
137                             studentId: undefined,
138                             name: undefined,
139                             description: undefined,
140                             birthday: undefined,
141                             sex: undefined,
142                             enabled: undefined,
143                             avatar: undefined,
144                             video: undefined,
145                             memo: undefined,
146         };
147         this.resetForm("formRef");
148       },
149     }
150   };
151 </script>