index.vue 2.93 KB
<template>
  <div>
    <div class="ant-result-title">项目名称:{{title}}</div>
    <a-list
      v-if="comments.length"
      :data-source="comments"
      :header="`${comments.length} ${
        comments.length > 1 ? 'replies' : 'reply'
      }`"
      item-layout="horizontal"
    >
      <a-list-item slot="renderItem" slot-scope="item">
        <a-comment
          :author="item.author ? item.author : 'Some One'"
          :avatar="
            item.avatar
              ? item.avatar
              : 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png'
          "
          :content="item.content"
          :datetime="moment(item.create_at).fromNow()"
        />
      </a-list-item>
    </a-list>
    <a-comment>
      <a-avatar
        slot="avatar"
        src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
        alt="Han Solo"
      />
      <div slot="content">
        <a-form-item>
          <a-textarea :rows="4" :value="value" @change="handleChange" />
        </a-form-item>
        <a-form-item>
          <a-button
            html-type="submit"
            :loading="submitting"
            type="primary"
            @click="handleSubmit"
          >
            Add Comment
          </a-button>
        </a-form-item>
      </div>
    </a-comment>
  </div>
</template>
<script>
import moment from "moment";
import FormItem from "ant-design-vue/lib/form/FormItem";
export default {
  data() {
    return {
      comments: [],
      submitting: false,
      value: "",
      moment,
      projectId: "",
      title: "",
    };
  },
  components: { [FormItem.name]: FormItem },
  mounted() {
    this.getProjectId();
  },
  methods: {
    handleSubmit() {
      if (!this.value) {
        return;
      }
      this.submitting = true;

      //创建评论
      let params = {
        // user_id:'',
        // create_at:''
        project_id: this.$route.query.id,
        content: this.value,
      };
      this.api.commons
        .create(params)
        .then((res) => {
          // console.log("res: ", res);
          if (res.id) {
            this.$message.success("评论成功");
            this.value = "";
            this.submitting = false;
            this.getProjectId();
          } else {
            this.$message.error("评论失败");
          }
        })
        .catch(() => {
          this.$message.error("评论失败");
        });
    },
    handleChange(e) {
      this.value = e.target.value;
    },
    getProjectId() {
      const projectId = this.$route.query.id;
      this.title = this.$route.query.title;
      if (!projectId) return;
      this.getComments(projectId);
    },
    async getComments(id) {
      const res = await this.api.commons.getList(id);
      if (res.length === 0) return;
      this.comments = res;
    },
  },
};
</script>
<style>
.ant-result-title {
  margin-bottom: 50px;
  color: rgba(0, 0, 0, 0.85);
  font-size: 24px;
  line-height: 1.8;
  text-align: center;
}
</style>