Преглед изворни кода

[修改]老师、学生课程部分表单提交

soleil пре 7 година
родитељ
комит
dbe4a66417

+ 1 - 1
mock/serializers/user/UserSerializer.js

@@ -14,7 +14,7 @@ const makeId = require("../../util/makeId");
  * @param role
  * @returns {UserSerializerType}
  */
-module.exports = (role = "TEACHER") => {
+module.exports = (role = "STUDENT") => {
   return {
     id: makeId(),
     role: role,

+ 20 - 8
src/views/student/Home/index.vue

@@ -27,6 +27,13 @@
       <div class="card">
         <div class="title">加入课程</div>
         <div class="content">
+          <b-notification
+            class="notification-self"
+            type="is-danger"
+            :active.sync="isServerError"
+          >
+            {{ errorMassage }}
+          </b-notification>
           <b-field
             label="选课码"
             :type="$v.coursenum.$error ? 'is-danger' : ''"
@@ -86,34 +93,39 @@ export default {
       courses: [],
       joinCouseActive: false,
       coursenum: "",
-      submitting: false
+      submitting: false,
+      isServerError: false,
+      errorMassage: "服务器错误,请稍后重试"
     };
   },
   mounted() {
-    getStudentCourses().then(value => (this.courses = value.data));
+    this.getCourses();
   },
   methods: {
+    getCourses() {
+      getStudentCourses().then(value => (this.courses = value.data));
+    },
     //加入课程
     joinSubject() {
+      if (this.submitting) return false;
       this.$v.$touch();
-      this.submitting = true;
       if (!this.$v.$invalid) {
+        this.submitting = true;
+        this.isServerError = false;
         postStudentCourse(this.coursenum)
           .then(res => {
             this.submitting = false;
             this.joinCouseActive = false;
-            console.log(res);
             this.$toast.open({
               message: `${res.data.name} 选课成功`,
               type: "is-success"
             });
+            this.getCourses();
           })
           .catch(e => {
             this.submitting = false;
-            this.$toast.open({
-              message: e.message,
-              type: "is-danger"
-            });
+            this.isServerError = true;
+            this.errorMassage = e.message;
           });
       }
     }

+ 22 - 8
src/views/teacher/Home/index.vue

@@ -27,6 +27,13 @@
       <div class="card">
         <div class="title">新建课程</div>
         <div class="content">
+          <b-notification
+            class="notification-self"
+            type="is-danger"
+            :active.sync="isServerError"
+          >
+            {{ errorMassage }}
+          </b-notification>
           <b-field
             label="课程名"
             :type="$v.coursename.$error ? 'is-danger' : ''"
@@ -115,17 +122,24 @@ export default {
       username: "",
       coursename: "",
       coursenum: "",
-      submitting: false
+      submitting: false,
+      isServerError: false,
+      errorMassage: "服务器错误,请稍后再试"
     };
   },
   mounted() {
-    getTeacherCourses().then(value => (this.courses = value.data));
+    this.getCourses();
   },
   methods: {
+    getCourses() {
+      getTeacherCourses().then(value => (this.courses = value.data));
+    },
     //新建课程
     newSubject() {
+      if (this.submitting) return;
       this.$v.$touch();
       if (!this.$v.$invalid) {
+        this.isServerError = false;
         let subjectInfo = {
           name: this.coursename,
           code: this.coursenum
@@ -134,18 +148,18 @@ export default {
         postTeacherCourse(subjectInfo)
           .then(res => {
             this.submitting = false;
-            console.log(res);
             this.$toast.open({
-              message: `${res.res.name}创建成功`,
+              message: `${res.data.name}创建成功`,
               type: "is-success"
             });
             this.newSubjectActive = false;
+            this.getCourses();
           })
           .catch(e => {
-            this.$toast.open({
-              message: e.message,
-              type: "is-danger"
-            });
+            if (this.errorMassage) {
+              this.errorMassage = e.message;
+            }
+            this.isServerError = true;
           });
       }
     }