ModifyCourseSimple.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <el-dialog
  3. class="modify-course-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-position="top"
  31. >
  32. <el-form-item prop="courseName" label="课程名称:">
  33. <el-input
  34. v-model.trim="modalForm.courseName"
  35. placeholder="请输入课程名称"
  36. clearable
  37. ></el-input>
  38. </el-form-item>
  39. <el-form-item prop="courseCode" label="课程编码:">
  40. <el-input
  41. v-model.trim="modalForm.courseCode"
  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-course
  51. ref="SelectSimpleCourse"
  52. v-model="selectedCourseIds"
  53. ></select-simple-course>
  54. </div>
  55. <!-- import -->
  56. <div v-if="curTab === 'import'" class="tab-body">
  57. <el-button
  58. type="success"
  59. icon="el-icon-download"
  60. @click="downloadTemplate('teachCourse')"
  61. >模板下载</el-button
  62. >
  63. <upload-button
  64. btn-icon="el-icon-circle-plus-outline"
  65. btn-content="批量导入"
  66. btn-type="success"
  67. :upload-url="uploadUrl"
  68. :format="['xls', 'xlsx']"
  69. accept=".xls,.xlsx"
  70. @valid-error="validError"
  71. @upload-success="uploadSuccess"
  72. >
  73. </upload-button>
  74. </div>
  75. <div v-if="!IS_IMPORT" slot="footer">
  76. <el-button type="primary" :disabled="isSubmit" @click="submit"
  77. >确认</el-button
  78. >
  79. <el-button @click="cancel">取消</el-button>
  80. </div>
  81. </el-dialog>
  82. </template>
  83. <script>
  84. import { updateCourseSimple, batchAddCourseSimple } from "../../api";
  85. import UploadButton from "@/components/UploadButton";
  86. import SelectSimpleCourse from "./SelectSimpleCourse.vue";
  87. import templateDownload from "@/mixins/templateDownload";
  88. const initModalForm = {
  89. id: null,
  90. courseName: "",
  91. courseCode: "",
  92. };
  93. export default {
  94. name: "modify-course-simple",
  95. components: { UploadButton, SelectSimpleCourse },
  96. mixins: [templateDownload],
  97. computed: {
  98. IS_IMPORT() {
  99. return this.curTab === "import";
  100. },
  101. },
  102. data() {
  103. return {
  104. modalIsShow: false,
  105. isSubmit: false,
  106. curTab: "select",
  107. tabs: [
  108. {
  109. name: "手动添加",
  110. val: "input",
  111. },
  112. {
  113. name: "课程表选择",
  114. val: "select",
  115. },
  116. {
  117. name: "批量导入",
  118. val: "import",
  119. },
  120. ],
  121. modalForm: { ...initModalForm },
  122. courses: [],
  123. rules: {
  124. courseName: [
  125. {
  126. required: true,
  127. // pattern: /^[0-9a-zA-Z\u4E00-\u9FA5]{1,20}$/,
  128. // message: "课程名称只能输入汉字、数字和字母,长度不能超过20",
  129. message: "课程名称不能超过30个字",
  130. max: 30,
  131. trigger: "change",
  132. },
  133. ],
  134. courseCode: [
  135. {
  136. required: true,
  137. pattern: /^[0-9a-zA-Z_-]{3,30}$/,
  138. message: "课程编码只能由数字字母短横线组成,长度在3-30之间",
  139. trigger: "change",
  140. },
  141. ],
  142. },
  143. selectedCourseIds: [],
  144. // import
  145. uploadUrl: "/api/admin/teach/course/import",
  146. dfilename: "教学课程导入模板.xlsx",
  147. };
  148. },
  149. methods: {
  150. visibleChange() {
  151. this.modalForm = { ...initModalForm };
  152. },
  153. cancel() {
  154. this.modalIsShow = false;
  155. },
  156. open() {
  157. this.modalIsShow = true;
  158. },
  159. selectMenu(tab) {
  160. this.curTab = tab;
  161. },
  162. submit() {
  163. const submitFunc = {
  164. input: this.submitInput,
  165. select: this.submitSelect,
  166. };
  167. submitFunc[this.curTab]();
  168. },
  169. async submitInput() {
  170. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  171. if (!valid) return;
  172. if (this.isSubmit) return;
  173. this.isSubmit = true;
  174. let datas = { ...this.modalForm };
  175. const data = await updateCourseSimple(datas).catch(() => {
  176. this.isSubmit = false;
  177. });
  178. if (!data) return;
  179. this.isSubmit = false;
  180. this.$message.success("添加成功!");
  181. this.$emit("modified");
  182. this.cancel();
  183. },
  184. async submitSelect() {
  185. if (!this.selectedCourseIds.length) {
  186. this.$message.error("请选择科目");
  187. return;
  188. }
  189. if (this.isSubmit) return;
  190. this.isSubmit = true;
  191. const data = await batchAddCourseSimple({
  192. basicCourseIdList: this.selectedCourseIds.join(),
  193. }).catch(() => {});
  194. this.isSubmit = false;
  195. if (!data) return;
  196. this.$message.success("添加成功!");
  197. this.$emit("modified");
  198. this.selectedCourseIds = [];
  199. this.$refs.SelectSimpleCourse.clearSelection();
  200. },
  201. // import
  202. validError(errorData) {
  203. this.$message.error(errorData.message);
  204. },
  205. uploadSuccess(data) {
  206. this.$message.success(data.data || "课程导入成功!");
  207. this.$emit("modified");
  208. this.cancel();
  209. },
  210. },
  211. };
  212. </script>