ModifyCourseSimple.vue 5.6 KB

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