widget-follow.vue 1.66 KB
<template>
  <button class="btn-follow hover-opacity" :class="followClass" @click="onFollow">{{followText}}</button>
</template>

<script>
import {createNamespacedHelpers} from 'vuex';
const {mapActions} = createNamespacedHelpers('user');

export default {
  name: 'WidgetFollow',
  props: {
    authorUid: Number,
    follow: Boolean
  },
  data() {
    return {
      loading: false,
      followStatus: this.follow
    };
  },
  watch: {
    follow(val) {
      this.followStatus = val;
    }
  },
  computed: {
    followClass() {
      return {
        loading: this.loading,
        follow: this.followStatus
      };
    },
    followText() {
      return this.followStatus ? '已关注' : '关注';
    }
  },
  methods: {
    ...mapActions(['followUser']),
    async onFollow() {
      if (this.loading) {
        return;
      }
      this.loading = true;
      const result = await this.followUser({followUid: this.authorUid, status: this.followStatus ? 1 : 0});

      this.loading = false;
      if (result.code === 200) {
        this.followStatus = !this.followStatus;
        this.$emit('on-follow', this.followStatus);
      } else {
        this.$createToast && this.$createToast({
          txt: result.message || '服务器开小差了',
          type: 'warn',
          time: 1000
        }).show();
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.btn-follow {
  width: 120px;
  height: 50px;
  padding: 0;
  font-size: 26px;
  border-radius: 3PX;
  background-color: #222;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;

  &.follow {
    border: solid 1px #4a4a4a;
    background-color: #fff;
    color: #000;
  }
}
</style>