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

<script>
import {createNamespacedHelpers} from 'vuex';
import YAS from 'utils/yas-constants';
const {mapActions} = createNamespacedHelpers('user');

export default {
  name: 'WidgetFollow',
  props: {
    authorUid: [Number, String],
    follow: Boolean,
    authorType: {
      type: [Number, String],
      default: 1
    },
    topicId: [Number, String],
    share: Boolean,
    posId: Number
  },
  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', 'followTopic']),
    syncServiceFollow() {
      if (this.topicId) {
        return this.followTopic({
          topicId: this.topicId,
          status: this.followStatus ? 1 : 0
        });
      } else {
        return this.followUser({
          followUid: this.authorUid,
          status: this.followStatus ? 1 : 0,
          authorType: this.authorType
        });
      }
    },
    async onFollow() {
      if (this.share) {
        return this.$links.toDownloadApp();
      }
      if (this.loading) {
        return;
      }
      this.loading = true;

      const result = await this.syncServiceFollow();

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

      this.$store.dispatch('reportYas', {
        params: {
          appop: YAS.eventName.fellow,
          param: {
            AUTH_ID: this.authorUid,
            POS_ID: this.posId
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.btn-follow {
  min-width: 110px;
  height: 50px;
  padding: 0 10px;
  font-size: 24px;
  color: #222;
  border-radius: 26px;
  border: solid 1px #222;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;

  &.follow {
    border-color: #b0b0b0;
    color: #b0b0b0;
  }

  > span {
    line-height: 1;
  }
}
</style>