ModifyTeacherSimple.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <el-dialog
  3. class="modify-teacher-simple"
  4. :visible.sync="modalIsShow"
  5. title="添加教师"
  6. top="10px"
  7. width="800px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. append-to-body
  11. @open="visibleChange"
  12. >
  13. <div class="mb-4 tab-btns">
  14. <el-button
  15. v-for="tab in tabs"
  16. :key="tab.val"
  17. size="medium"
  18. :type="curTab == tab.val ? 'primary' : 'default'"
  19. @click="selectMenu(tab.val)"
  20. >{{ tab.name }}
  21. </el-button>
  22. </div>
  23. <!-- input -->
  24. <div v-if="curTab === 'input'" class="tab-body">
  25. <el-form
  26. ref="modalFormComp"
  27. :model="modalForm"
  28. :rules="rules"
  29. :key="modalForm.id"
  30. label-width="110px"
  31. >
  32. <el-form-item prop="teacherName" label="姓名:">
  33. <el-input
  34. v-model.trim="modalForm.teacherName"
  35. placeholder="请输入姓名"
  36. clearable
  37. ></el-input>
  38. </el-form-item>
  39. <el-form-item prop="teacherCode" label="用户名/工号:">
  40. <el-input
  41. v-model.trim="modalForm.teacherCode"
  42. placeholder="请输入用户名/工号"
  43. clearable
  44. ></el-input>
  45. </el-form-item>
  46. </el-form>
  47. </div>
  48. <!-- select -->
  49. <div v-if="curTab === 'select'" class="tab-body">
  50. <select-simple-teacher
  51. ref="SelectSimpleTeacher"
  52. v-model="selectedTeacherIds"
  53. ></select-simple-teacher>
  54. </div>
  55. <div slot="footer">
  56. <el-button type="primary" :disabled="isSubmit" @click="submit"
  57. >确认</el-button
  58. >
  59. <el-button @click="cancel">取消</el-button>
  60. </div>
  61. </el-dialog>
  62. </template>
  63. <script>
  64. import { updateTeacherSimple, batchAddTeacherSimple } from "../../api";
  65. import SelectSimpleTeacher from "./SelectSimpleTeacher.vue";
  66. import templateDownload from "@/mixins/templateDownload";
  67. const initModalForm = {
  68. teachCourseId: null,
  69. teacherName: "",
  70. teacherCode: "",
  71. };
  72. export default {
  73. name: "modify-teacher-simple",
  74. components: { SelectSimpleTeacher },
  75. mixins: [templateDownload],
  76. props: {
  77. course: {
  78. type: Object,
  79. default() {
  80. return {};
  81. },
  82. },
  83. },
  84. data() {
  85. return {
  86. modalIsShow: false,
  87. isSubmit: false,
  88. curTab: "select",
  89. tabs: [
  90. {
  91. name: "教师库添加",
  92. val: "select",
  93. },
  94. {
  95. name: "手动添加",
  96. val: "input",
  97. },
  98. ],
  99. modalForm: { ...initModalForm },
  100. courses: [],
  101. rules: {
  102. teacherName: [
  103. {
  104. required: true,
  105. // pattern: /^[0-9a-zA-Z\u4E00-\u9FA5]{1,20}$/,
  106. // message: "姓名只能输入汉字、数字和字母,长度不能超过20",
  107. message: "姓名不能超过30个字",
  108. max: 30,
  109. trigger: "change",
  110. },
  111. ],
  112. teacherCode: [
  113. {
  114. required: true,
  115. pattern: /^[0-9a-zA-Z_-]{3,30}$/,
  116. message: "用户名/工号只能由数字字母短横线组成,长度在3-30之间",
  117. trigger: "change",
  118. },
  119. ],
  120. },
  121. selectedTeacherIds: [],
  122. };
  123. },
  124. methods: {
  125. visibleChange() {
  126. this.modalForm = Object.assign({}, initModalForm, {
  127. teachCourseId: this.course.id,
  128. });
  129. },
  130. cancel() {
  131. this.modalIsShow = false;
  132. },
  133. open() {
  134. this.modalIsShow = true;
  135. },
  136. selectMenu(tab) {
  137. this.curTab = tab;
  138. },
  139. submit() {
  140. const submitFunc = {
  141. input: this.submitInput,
  142. select: this.submitSelect,
  143. };
  144. submitFunc[this.curTab]();
  145. },
  146. async submitInput() {
  147. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  148. if (!valid) return;
  149. if (this.isSubmit) return;
  150. this.isSubmit = true;
  151. let datas = { ...this.modalForm };
  152. const data = await updateTeacherSimple(datas).catch(() => {
  153. this.isSubmit = false;
  154. });
  155. if (!data) return;
  156. this.isSubmit = false;
  157. this.$message.success("添加成功!");
  158. this.$emit("modified");
  159. this.cancel();
  160. },
  161. async submitSelect() {
  162. if (!this.selectedTeacherIds.length) {
  163. this.$message.error("请选择教师");
  164. return;
  165. }
  166. if (this.isSubmit) return;
  167. this.isSubmit = true;
  168. const data = await batchAddTeacherSimple({
  169. teachCourseId: this.course.id,
  170. userIdList: this.selectedTeacherIds.join(),
  171. }).catch(() => {});
  172. this.isSubmit = false;
  173. if (!data) return;
  174. this.$message.success("添加成功!");
  175. this.$emit("modified");
  176. this.selectedTeacherIds = [];
  177. this.$refs.SelectSimpleTeacher.clearSelection();
  178. this.cancel();
  179. },
  180. },
  181. };
  182. </script>