All files / src/components Comment.vue

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 230 231                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
<template>
  <div>
    <div class="comment">
      <h2>评论</h2>
      <div class="comment-msg">
        <div class="comment-img">
          <img :src=attachImageUrl(avator) alt="">
        </div>
        <el-input
          class="comment-input"
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="textarea">
        </el-input>
      </div>
      <el-button type="primary" class="sub-btn" @click="postComment()">评论</el-button>
    </div>
    <p>精彩评论: 共 {{commentList.length}} 条评论</p>
    <ul class="popular" v-for="(item, index) in commentList" :key="index">
      <li>
        <div class="popular-img">
          <img :src=attachImageUrl(userPic[item.userId]) alt="">
        </div>
        <div class="popular-msg">
          <ul>
            <li class="name">{{userName[item.userId]}}</li>
            <li class="time">{{item.createTime}}</li>
            <li class="content">{{item.content}}</li>
          </ul>
        </div>
        <div class="up" ref="up" @click="postUp(item.id, item.up, index)">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-zan"></use>
          </svg>
          {{item.up}}
        </div>
      </li>
    </ul>
  </div>
</template>
 
<script>
import {mixin} from '../mixins'
import { mapGetters } from 'vuex'
 
export default {
  name: 'comment',
  mixins: [mixin],
  props: [
    'playId', // 歌曲ID或歌单ID
    'type' // 歌单(1)/歌曲(0)
  ],
  data () {
    return {
      commentList: [], // 存放评论内容
      userPic: [], // 保存评论用户头像
      userName: [], // 保存评论用户名字
      textarea: '' // 存放输入内容
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'userId', // 用户ID
      'index', // 列表中的序号
      'loginIn', // 用户是否登录
      'avator' // 用户头像
    ])
  },
  watch: {
    id () {
      this.getComment()
    }
  },
  mounted () {
    this.getComment()
  },
  methods: {
    // 获取所有评论
    getComment () {
      this.$api.commentAPI.getAllComment(this.type, this.playId)
        .then(res => {
          this.commentList = res.data
          // const temp = []
          for (let item of res.data) {
            // temp.push(item.userId)
            this.getUsers(item.userId)
          }
          // const promises = temp.map((item, index) => {
          //   return this.getUsers(item)
          // })
          // this.userPic = []
          // this.userName = []
          // Promise.all(promises).then((allData) => {
          //   for (let i = 0; i < allData.length; i++) {
          //     const temp2 = allData[i].split(',')
          //     this.userPic.push(temp2[0])
          //     this.userName.push(temp2[1])
          //   }
          // })
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 获取评论用户的昵称和头像
    getUsers (id) {
      return new Promise((resolve, reject) => {
        this.$api.userAPI.getUserOfId(id)
          .then(res => {
            this.$set(this.userPic, id, res.data[0].avator)
            this.$set(this.userName, id, res.data[0].username)
            // resolve(res.data[0].avator + ',' + res.data[0].username)
          })
          .catch(err => {
            console.log(err)
          })
      })
    },
    // 提交评论
    postComment () {
      if (this.loginIn) {
        this.$api.commentAPI.setComment(this.type, this.playId, this.userId, this.textarea)
          .then(res => {
            if (res.data.code === 1) {
              this.textarea = ''
              this.getComment()
              this.notify('评论成功', 'success')
            } else {
              this.notify('评论失败', 'error')
            }
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        this.notify('请先登录', 'warning')
      }
    },
    // 点赞
    postUp (id, up, index) {
      if (this.loginIn) {
        this.$api.commentAPI.setLike(id, up)
          .then(res => {
            if (res.data.code === 1) {
              this.$refs.up[index].children[0].style.color = '#2796dd'
              this.getComment()
            }
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        this.notify('请先登录', 'warning')
      }
    }
  }
}
</script>
 
<style scoped>
  /*评论*/
  .comment h2 {
    margin-bottom: 20px;
    text-align: center;
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid black;
  }
  .comment-msg {
    display: flex;
    flex-direction: row;
  }
  .comment-img {
    width: 70px;
  }
  .comment-img img {
    width: 100%;
  }
  .comment-input {
    margin-left: 10px;
    flex: 1;
  }
  .sub-btn{
    margin-top: 10px;
    margin-left: 90%;
  }
  /*热门评论*/
  .popular {
    width: 100%;
  }
  .popular > li{
    border-bottom: solid 1px rgba(0, 0, 0, 0.1);
    padding: 15px 0;
    display: flex;
    flex-direction: row;
  }
  .popular-img {
    width: 50px;
  }
  .popular-img img {
    width: 100%;
  }
  .popular-msg {
    padding: 0 20px;
    flex: 1;
  }
  .popular-msg li {
    width: 100%;
  }
  .popular-msg .time {
    font-size: .6rem;
    color: rgba(0, 0, 0, 0.5);
  }
  .popular-msg .content {
    font-size: 1rem;
  }
  .up {
    width: 50px;
    line-height: 60px;
  }
  .icon {
    width: 1em;
    height: 1em;
    fill: currentColor;
    color: black;
    font-size: 1.5em;
  }
</style>