mjh 4 лет назад
Родитель
Сommit
648ef866e7
2 измененных файлов с 32 добавлено и 12 удалено
  1. 1 0
      src/api/user.js
  2. 31 12
      src/views/ManagePage/ManagePage.vue

+ 1 - 0
src/api/user.js

@@ -19,6 +19,7 @@ function resetAPI(data) {
 }
 
 function getUserAPI(data){
+  console.log(data);
   return axios.get(`${prefix}/findUserById`, {
     params: {
       id: data,

+ 31 - 12
src/views/ManagePage/ManagePage.vue

@@ -7,10 +7,10 @@
         <div>
           <el-select v-model="value" class="m-2" placeholder="Select">
             <el-option
-                v-for="item in options"
-                :key="item.value"
-                :label="item.label"
-                :value="item.value"
+              v-for="item in options"
+              :key="item.value"
+              :label="item.label"
+              :value="item.value"
             >
             </el-option>
           </el-select>
@@ -28,7 +28,7 @@
       </el-pagination>
     </div>
   </div>
-  <el-dialog v-model="dialogVisible" title="新增课程" width="50%">
+  <el-dialog v-model="dialogVisible" title="新增课程" width="50%" :before-close="handleFormCancel">
     <el-form ref="formRef" :model="form" label-width="120px">
       <el-form-item label="课程名称">
         <el-input v-model="form.name" placeholder="请输入课程名称"></el-input>
@@ -111,7 +111,7 @@ const form = reactive({
     null,
   ],
   invitationCode: "",
-  price: "",//input均会将Number类型转化成为字符串类型
+  price: "0",//input均会将Number类型转化成为字符串类型
   moocUrl: null,
   imageUrl: null,
 });
@@ -236,7 +236,14 @@ const courseListExample = [
 ];
 
 const handleFormConfirm = async () => {
-  checkForm();
+  const checkRes = checkForm();
+  if (!checkRes.res) {
+    ElMessage({
+      message: checkRes.msg,
+      type: "error",
+    });
+    return;
+  }
   const res = await _this.$apis.createCourse({
     name: form.name,
     teacher: {
@@ -250,7 +257,7 @@ const handleFormConfirm = async () => {
     invitationCode: form.invitationCode,
     moocUrl: form.moocUrl,
     createdTime: new Date(),
-    price: form.price,
+    price: Number(form.price),
   });
   console.log(res);
   if (res.code === 0) {
@@ -258,6 +265,9 @@ const handleFormConfirm = async () => {
       message: "新建课程成功!",
       type: "success",
     });
+    clearForm();
+    dialogVisible.value = false;
+    // todo: 刷新课程列表
   } else {
     ElMessage({
       message: res.data.message,
@@ -287,13 +297,22 @@ const handleConfirmFormCancel = function () {
 //点击确定新增课程按钮时检查填写内容是否合法
 const checkForm = function () {
   const nameValid = form.name.length > 0;
-  const semesterValid = (form.semester.season != null && form.semester.season.length > 0) && (form.semester.year != null && form.semester.year.length > 0);
+  if (!nameValid) return {res: false, msg: "请输入课程名称!"};
+  const yearValid = form.semester.year !== "";
+  if (!yearValid) return {res: false, msg: "请选择课程所在年份!"};
+  const seasonValid = form.semester.season !== "";
+  if (!seasonValid) return {res: false, msg: "请选择课程所在季节!"};
   const descriptionValid = form.description.length > 0;
+  if (!descriptionValid) return {res: false, msg: "请输入课程简介!"};
   const courseTimeValid = form.courseTime[0] !== null && form.courseTime[1] !== null;
+  if (!courseTimeValid) return {res: false, msg: "请选择课程时间!"};
   const invitationCodeValid = form.invitationCode.length > 0;
-  const priceValid = form.price.length > 0 && (parseInt(form.price) > 0);
+  if (!invitationCodeValid) return {res: false, msg: "请输入课程邀请码!"};
+  const priceValid = form.price.length > 0 && (parseInt(form.price) >= 0);
+  if (!priceValid) return {res: false, msg: "请正确输入课程价格!"};
   //moocUrl和imgUrl可以为null
-  return nameValid && semesterValid && descriptionValid && courseTimeValid && priceValid && invitationCodeValid;
+  // todo: 对moocUrl的检验,查询该链接是否有效
+  return {res: true, msg: "表格填写完成!"};
 };
 //
 const clearForm = function () {
@@ -303,7 +322,7 @@ const clearForm = function () {
   form.description = "";
   form.courseTime[0] = form.courseTime[1] = null;
   form.invitationCode = "";
-  form.price = "";
+  form.price = "0";
   form.moocUrl = null;
   form.imageUrl = null;
 };