ArticleComment.vue 5.5 KB
Newer Older
yma16's avatar
yma16 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* eslint-disable vue/valid-template-root */
<template>
  <div class="articleCommentsClass">
    <div class="showComment">
      <div class="contentClass">
        <template v-if="contentRes.length">
          <div v-for="(item, key) in contentRes" :key="key">
            <h5>{{ item.user }}({{ item.date }}):</h5>
            <p>{{ item.content }}</p>
          </div>
        </template>
        <template v-else>
          <h5>{{ noneMsg }}</h5>
        </template>
      </div>
    </div>
    <div class="postComment">
      <div style="position: relative; display: flex; width: 100%">
        <el-input
          type="text"
          :rows="2"
          placeholder="填写评论(20字以内)"
          v-model="articleCommit"
          maxlength="20"
        >
        </el-input>
        <el-button
          type="primary"
          style="margin-left: 5px"
          @click="increment(userId, articleId, articleCommit)"
yma16's avatar
yma16 已提交
31
          v-loading="submitLoading"
yma16's avatar
yma16 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
          >提交评论</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "ArticleComment",
  props: ["articleId"],
  // props定义则data()无需定义
  data() {
    return {
yma16's avatar
yma16 已提交
47
      submitLoading: false,
yma16's avatar
yma16 已提交
48 49
      noneMsg: "还没人评论>_<",
      msg: "评论区",
yma16's avatar
yma16 已提交
50
      baseurl: "/api/",
Y
yma16 已提交
51
      // baseurl: "http://yongma16.xyz/comment/",
yma16's avatar
yma16 已提交
52 53 54 55 56
      userImg: "",
      username: "匿名",
      contentRes: [],
      userId: "匿名",
      articleCommit: "",
yma16's avatar
yma16 已提交
57
      childArticleId: "",
yma16's avatar
yma16 已提交
58 59
    };
  },
yma16's avatar
yma16 已提交
60 61 62
  watch: {
    articleId: function (newData) {
      this.childArticleId = newData;
yma16's avatar
yma16 已提交
63
      return newData;
yma16's avatar
yma16 已提交
64 65
    },
  },
yma16's avatar
yma16 已提交
66
  mounted() {
yma16's avatar
yma16 已提交
67
    this.childArticleId && this.getComments(this.childArticleId);
yma16's avatar
yma16 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  },
  methods: {
    dateToString(dateValue) {
      let timeCreate = new Date(dateValue);
      let year = timeCreate.getFullYear(),
        month = timeCreate.getMonth() + 1,
        day = timeCreate.getDate(),
        hour =
          timeCreate.getHours() > 9
            ? timeCreate.getHours()
            : "0" + timeCreate.getHours(),
        minute =
          timeCreate.getMinutes() > 9
            ? timeCreate.getMinutes()
            : "0" + timeCreate.getMinutes(),
        second =
          timeCreate.getSeconds() > 9
            ? timeCreate.getSeconds()
            : "0" + timeCreate.getSeconds();
      return (
        year +
        "" +
        month +
        "" +
        day +
        "" +
        " " +
        hour +
        ":" +
        minute +
        ":" +
        second
      );
    },
    getComments(articleId) {
      let that = this;
      let params = {
yma16's avatar
yma16 已提交
105
        articleId: articleId || that.articleId,
yma16's avatar
yma16 已提交
106 107
      };
      axios
Y
yma16 已提交
108
        .post(that.baseurl + "comment/get/", params)
yma16's avatar
yma16 已提交
109 110 111 112 113 114 115 116
        .then((res) => {
          try {
            let resData = res.data;
            that.contentRes = resData.content;
            that.contentRes.map((item) => {
              item.date = that.dateToString(item.date);
            });
          } catch (e) {
yma16's avatar
yma16 已提交
117
            throw Error(e);
yma16's avatar
yma16 已提交
118 119 120
          }
        })
        .catch((r) => {
yma16's avatar
yma16 已提交
121
          throw Error(r);
yma16's avatar
yma16 已提交
122 123 124 125
        });
    },
    increment(userId, articleId, content) {
      let that = this;
yma16's avatar
yma16 已提交
126
      if (!localStorage.getItem("yma16siteUserInfo")) {
yma16's avatar
yma16 已提交
127 128 129 130 131
        that.$router
          .push({
            path: "/login",
          })
          .catch((error) => {
yma16's avatar
yma16 已提交
132
            throw Error(error);
yma16's avatar
yma16 已提交
133 134
          });
      } else {
yma16's avatar
yma16 已提交
135 136 137 138 139 140 141 142
        if (content === "" || !content) {
          that.$notify({
            title: "评论失败!",
            message: "评论内容不能为空",
            position: "top-left",
          });
          return false;
        }
yma16's avatar
yma16 已提交
143
        that.submitLoading = true;
yma16's avatar
yma16 已提交
144 145
        const userInfo = JSON.parse(localStorage.getItem("yma16siteUserInfo"));
        that.userId = userInfo.username;
yma16's avatar
yma16 已提交
146 147 148 149 150 151
        let params = {
          username: that.userId,
          articleId: articleId,
          content: content,
        };
        axios
Y
yma16 已提交
152
          .post(that.baseurl + "comment/post/", params)
yma16's avatar
yma16 已提交
153
          .then((res) => {
yma16's avatar
yma16 已提交
154
            that.submitLoading = false;
yma16's avatar
yma16 已提交
155 156 157 158 159 160 161 162
            that.articleCommit = null;
            try {
              let resData = res.data;
              that.contentRes = resData.content;
              that.contentRes.map((item) => {
                item.date = that.dateToString(item.date);
              });
            } catch (e) {
yma16's avatar
yma16 已提交
163
              that.submitLoading = false;
yma16's avatar
yma16 已提交
164
              throw Error(e);
yma16's avatar
yma16 已提交
165 166 167
            }
          })
          .catch((r) => {
yma16's avatar
yma16 已提交
168
            that.submitLoading = false;
yma16's avatar
yma16 已提交
169 170 171 172 173
            that.$router
              .push({
                path: "/login",
              })
              .catch((error) => {
yma16's avatar
yma16 已提交
174
                throw Error(error);
yma16's avatar
yma16 已提交
175 176
              });
            throw Error(r);
yma16's avatar
yma16 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
          });
      }
    },
  },
};
</script>

<style scoped>
.articleCommentsClass {
  position: relative;
  widows: 100%;
  height: 100%;
  /* background: burlywood; */
  margin-bottom: 10px;
}

.showComment {
  position: relative;
  width: 100%;
  height: auto;
  background: rgba(187, 168, 164, 0.8);
}

.contentClass {
  position: relative;
  width: 100%;
  margin: 10px;
  padding: 5px;
}

.postComment {
  position: relative;
  width: 100%;
  height: auto;
  line-height: 10px;
  display: flex;
  width: 100%;
  margin-top: 10px;
}

.loginUsernameClass {
  display: block;
  width: 20px;
  /*内联对象需加*/
  word-break: keep-all;
  /* 不换行 */
  white-space: nowrap;
  /* 不换行 */
  overflow: hidden;
  /* 内容超出宽度时隐藏超出部分的内容 */
  text-overflow: ellipsis;
}
</style>