Ver código fonte

feat: 新建课程或修改课程时,图片将通过后端上传至seec的阿里云服务器上

mjh 4 anos atrás
pai
commit
86613115bc
1 arquivos alterados com 70 adições e 81 exclusões
  1. 70 81
      src/views/ManagePage/ManagePage.vue

+ 70 - 81
src/views/ManagePage/ManagePage.vue

@@ -340,93 +340,82 @@ export default {
     },
     handleFormConfirm() {
       const _this = this;
-      // 上传oss图片
-      this.$refs.formRef.validate((valid) => {
+      _this.form.imageUrl = null;
+      _this.$refs.formRef.validate((valid) => {
         if (valid) {
-          if (this.form.fileList.length === 0) {
-            this.form.imageUrl = null;
-            createOrUpdateCourse();
-          } else {
-            if (this.form.fileList[0].raw) {
-              console.log(2);
-              console.log(this.$apis);
-              this.$apis.uploadCourseImage(this.form.name, this.form.fileList[0].raw)
-                .then(res => console.log(res))
-                .catch();
-              // client.put("/seecoder/image-" + this.form.name + ".jpg", this.form.fileList[0].raw)
-              //   .then(res => {
-              //     this.form.imageUrl = res.res.requestUrls[0];
-              //     createOrUpdateCourse();
-              //   })
-              //   .catch(err => {
-              //     console.log(err);
-              //     ElMessage.error(err);
-              //   });
-            } else {
-              createOrUpdateCourse();
-            }
-          }
-        }
-      });
-
-      function createOrUpdateCourse() {
-        if (_this.form.id === null) {
-          _this.$apis.createCourse({
-            name: _this.form.name,
-            teacher: {
-              id: _this.$store.state.user.id,
-            },
-            semester: _this.form.year + _this.form.season,
-            imageUrl: _this.form.imageUrl,
-            description: _this.form.description,
-            startTime: _this.form.courseTime[0],
-            endTime: _this.form.courseTime[1],
-            invitationCode: _this.form.invitationCode,
-            moocUrl: _this.form.moocUrl,
-            createdTime: new Date(),
-            price: Number(_this.form.price),
-            coderId: _this.form.coderLink ? -1 : null,
-            evalId: _this.form.evalLink ? -1 : null,
-          })
-            .then((res) => {
-              _this.myCourseList.push(res.data.data);
-              _this.dialogVisible = false;
-              _this.clearForm();
-              ElMessage.success("新建课程成功");
-            })
-            .catch(err => {
-              ElMessage.error(err);
+          if (_this.form.id === null) {
+            // 首先创建课程
+            // 之后上传课程图片
+            _this.$apis.createCourse({
+              name: _this.form.name,
+              teacher: {
+                id: _this.$store.state.user.id,
+              },
+              semester: _this.form.year + _this.form.season,
+              imageUrl: null,
+              description: _this.form.description,
+              startTime: _this.form.courseTime[0],
+              endTime: _this.form.courseTime[1],
+              invitationCode: _this.form.invitationCode,
+              moocUrl: _this.form.moocUrl,
+              createdTime: new Date(),
+              price: Number(_this.form.price),
+              coderId: _this.form.coderLink ? -1 : null,
+              evalId: _this.form.evalLink ? -1 : null,
+            }).then(async (res) => {
+              const createdCourse = res.data.data;
+              if (createdCourse) {
+                if (_this.form.fileList.length !== 0) {
+                  const imageUrl = (await _this.$apis.uploadCourseImage(createdCourse.name, this.form.fileList[0].raw)).data.data;
+                  if (imageUrl) {
+                    createdCourse.imageUrl = imageUrl;
+                  }
+                }
+                _this.myCourseList.push(createdCourse);
+                _this.dialogVisible = false;
+                _this.clearForm();
+                ElMessage.success("新建课程成功");
+              } else {
+                console.log(res);
+              }
             });
-        } else {
-          _this.$apis.updateCourse({
-            id: _this.form.id,
-            name: _this.form.name,
-            semester: _this.form.year + _this.form.season,
-            imageUrl: _this.form.imageUrl,
-            description: _this.form.description,
-            startTime: _this.form.courseTime[0],
-            endTime: _this.form.courseTime[1],
-            invitationCode: _this.form.invitationCode,
-            moocUrl: _this.form.moocUrl,
-            price: Number(_this.form.price),
-          })
-            .then((res) => {
-              for (let i = 0; i < _this.myCourseList.length; i++) {
-                if (_this.myCourseList[i].id === _this.form.id) {
-                  _this.myCourseList[i] = res.data.data;
+          } else {
+            // 首先更新课程
+            // 之后更新课程图片
+            _this.$apis.updateCourse({
+              id: _this.form.id,
+              name: _this.form.name,
+              semester: _this.form.year + _this.form.season,
+              imageUrl: _this.form.imageUrl,
+              description: _this.form.description,
+              startTime: _this.form.courseTime[0],
+              endTime: _this.form.courseTime[1],
+              invitationCode: _this.form.invitationCode,
+              moocUrl: _this.form.moocUrl,
+              price: Number(_this.form.price),
+            }).then(async (res) => {
+              const updatedCourse = res.data.data;
+              if (updatedCourse) {
+                if (_this.form.fileList.length !== 0) {
+                  const imageUrl = (await _this.$apis.uploadCourseImage(createdCourse.name, this.form.fileList[0].raw)).data.data;
+                  if (imageUrl) {
+                    updatedCourse.imageUrl = imageUrl;
+                  }
+                }
+                for (let i = 0; i < _this.myCourseList.length; i++) {
+                  if (_this.myCourseList[i].id === _this.form.id) {
+                    _this.myCourseList[i] = createdCourse;
+                  }
                 }
+                _this.$refs["courseCard" + _this.form.id][0].$props.courseVO = createdCourse;
+                _this.dialogVisible = false;
+                _this.clearForm();
+                ElMessage.success("更新课程成功");
               }
-              _this.$refs["courseCard" + _this.form.id][0].$props.courseVO = res.data.data;
-              _this.dialogVisible = false;
-              _this.clearForm();
-              ElMessage.success("更新课程成功");
-            })
-            .catch(err => {
-              console.log(err);
-              ElMessage.error(err);
             });
+          }
         }
-      }
+      });
     },
     handleFormCancel() {
       ElMessageBox.confirm("是否确认退出?退出后将不保存表格信息。", "提示", {