Browse Source

fix: 删除评论功能

mjh 4 years ago
parent
commit
6e6a55563f
2 changed files with 41 additions and 16 deletions
  1. 1 1
      src/api/message.js
  2. 40 15
      src/views/CoursePage/CourseDetail.vue

+ 1 - 1
src/api/message.js

@@ -19,7 +19,7 @@ function updateMessage(data) {
 }
 }
 
 
 function deleteMessage(id) {
 function deleteMessage(id) {
-  return axios.delete(`${prefix}`, {params: id});
+  return axios.delete(`${prefix}`, {params: {id}});
 }
 }
 
 
 function getCommentsByCourseId(courseId) {
 function getCommentsByCourseId(courseId) {

+ 40 - 15
src/views/CoursePage/CourseDetail.vue

@@ -59,8 +59,11 @@
           <div v-if="isCreator">
           <div v-if="isCreator">
             <el-popover v-if="tabName==='task'" trigger="hover">
             <el-popover v-if="tabName==='task'" trigger="hover">
               <div style="display: flex;justify-content: space-around;">
               <div style="display: flex;justify-content: space-around;">
-                <el-button v-if="courseInfo.coderId!==null" type="text" @click="addTest('coder')" id="coder-button">Coder</el-button>
-                <el-button v-if="courseInfo.evalId!==null" type="text" @click="addTest('eval')" id="eval-button">Eval</el-button>
+                <el-button v-if="courseInfo.coderId!==null" type="text" @click="addTest('coder')" id="coder-button">
+                  Coder
+                </el-button>
+                <el-button v-if="courseInfo.evalId!==null" type="text" @click="addTest('eval')" id="eval-button">Eval
+                </el-button>
               </div>
               </div>
               <template #reference>
               <template #reference>
                 <el-button>添加测试</el-button>
                 <el-button>添加测试</el-button>
@@ -207,8 +210,11 @@
                         {{ comments.mainMessage.createdAt.substring(0, 16).replace("T", " ") }}
                         {{ comments.mainMessage.createdAt.substring(0, 16).replace("T", " ") }}
                       </el-row>
                       </el-row>
                       <el-row>
                       <el-row>
-                        {{ comments.mainMessage.content }}<span @click="handleReplyClick(comments.mainMessage, index)"
-                                                                class="reply-button">回复</span>
+                        {{ comments.mainMessage.content }}
+                        <span @click="handleReplyClick(comments.mainMessage, index)" class="reply-button">回复</span>
+                        <span v-if="isOwnComment(comments.mainMessage.sender.id)"
+                              @click="deleteComment(comments.mainMessage.id)"
+                              class="reply-button">删除</span>
                       </el-row>
                       </el-row>
                     </div>
                     </div>
                   </el-row>
                   </el-row>
@@ -231,8 +237,12 @@
                       <el-row>
                       <el-row>
                         <span style="color: black;">{{
                         <span style="color: black;">{{
                             child.targetId === null ? "" : `@${comments.children[child.targetId].sender.name}: `
                             child.targetId === null ? "" : `@${comments.children[child.targetId].sender.name}: `
-                          }}</span>{{ child.content }}<span
-                        @click="handleReplyClick(child,index)" class="reply-button">回复</span>
+                          }}</span>
+                        {{ child.content }}
+                        <span @click="handleReplyClick(child,index)" class="reply-button">回复</span>
+                        <span v-if="isOwnComment(child.sender.id)"
+                              @click="deleteComment(child.id)"
+                              class="reply-button">删除</span>
                       </el-row>
                       </el-row>
                     </div>
                     </div>
                   </el-row>
                   </el-row>
@@ -273,7 +283,7 @@
 <script>
 <script>
 import NoticeItem from "./components/NoticeItem";
 import NoticeItem from "./components/NoticeItem";
 import FileEntry from "./components/FileEntry";
 import FileEntry from "./components/FileEntry";
-import { ElMessageBox, ElMessage } from "element-plus";
+import {ElMessageBox, ElMessage} from "element-plus";
 import variables from "@/utils/variables";
 import variables from "@/utils/variables";
 
 
 export default {
 export default {
@@ -327,11 +337,11 @@ export default {
         color: "",
         color: "",
       },
       },
       noticeTypeMap: {
       noticeTypeMap: {
-        announce: { type: "公告", title: "发布公告", color: themeColor.orange },
-        homework: { type: "作业", title: "发布作业", color: themeColor.blue },
-        exam: { type: "考试", title: "发布考试", color: themeColor.red },
+        announce: {type: "公告", title: "发布公告", color: themeColor.orange},
+        homework: {type: "作业", title: "发布作业", color: themeColor.blue},
+        exam: {type: "考试", title: "发布考试", color: themeColor.red},
       },
       },
-      newNoticeInfo: { noticeDescription: "", priority: "1", fileList: [] },
+      newNoticeInfo: {noticeDescription: "", priority: "1", fileList: []},
       postNoticeInfo: {},
       postNoticeInfo: {},
       fileEntryList: [{
       fileEntryList: [{
         id: 1,
         id: 1,
@@ -393,6 +403,12 @@ export default {
           });
           });
       }
       }
     },
     },
+    isOwnComment(){
+      const _this = this;
+      return function (senderId) {
+        return senderId === Number(_this.$store.state.user.id);
+      };
+    }
   },
   },
   mounted() {
   mounted() {
     switch (this.$route.params.jump_from_message) {
     switch (this.$route.params.jump_from_message) {
@@ -551,7 +567,7 @@ export default {
 
 
       function createAnnouncement() {
       function createAnnouncement() {
         let announcementVO = {
         let announcementVO = {
-          sender: { id: _this.$store.state.user.id, },
+          sender: {id: _this.$store.state.user.id,},
           courseId: _this.courseInfo.id,
           courseId: _this.courseInfo.id,
           title: _this.announcementForm.title,
           title: _this.announcementForm.title,
           content: _this.announcementForm.content,
           content: _this.announcementForm.content,
@@ -639,7 +655,7 @@ export default {
         confirmButtonText: "立即选课",
         confirmButtonText: "立即选课",
         cancelButtonText: "我再想想",
         cancelButtonText: "我再想想",
       })
       })
-        .then(({ value }) => {
+        .then(({value}) => {
           if (value !== this.courseInfo.invitationCode) {
           if (value !== this.courseInfo.invitationCode) {
             ElMessage.error("选课码错误");
             ElMessage.error("选课码错误");
           } else {
           } else {
@@ -733,18 +749,27 @@ export default {
           .then(res => {
           .then(res => {
             const msg = res.data.data;
             const msg = res.data.data;
             msg.createdAt = this.timestampToTime(msg.createdAt);
             msg.createdAt = this.timestampToTime(msg.createdAt);
-            this.commentTree.push({ mainMessage: msg, children: {} });
+            this.commentTree.push({mainMessage: msg, children: {}});
             this.commentContent = "";
             this.commentContent = "";
             ElMessage.success("发送成功");
             ElMessage.success("发送成功");
           });
           });
       }
       }
     },
     },
+    async deleteComment(commentId) {
+      const res = (await this.$apis.deleteMessage(commentId)).data.data || null;
+      if (res == null) {
+        alert(res);
+        return;
+      }
+      this.$message.success(res);
+      this.commentTree = (await this.$apis.getCommentsByCourseId(this.$route.params.id)).data.data;
+    },
     handleReplyClick(target, index) {
     handleReplyClick(target, index) {
       ElMessageBox.prompt("请输入评论内容", "回复", {
       ElMessageBox.prompt("请输入评论内容", "回复", {
         confirmButtonText: "发送",
         confirmButtonText: "发送",
         cancelButtonText: "取消",
         cancelButtonText: "取消",
       })
       })
-        .then(({ value }) => {
+        .then(({value}) => {
           if (value === "") {
           if (value === "") {
             ElMessage.error("无法发表空评论");
             ElMessage.error("无法发表空评论");
           } else {
           } else {