user-article.vue 3.5 KB
<template>
  <Article
    ref="article"
    :title="userName"
    type="userArticle"
    :thumbs="articleUserThumbList"
    :on-fetch="onFetch"
    :pos-id="sceneId"
    :user-uid="userUid">
  </Article>
</template>

<script>
import {get} from 'lodash';
import Article from './components/article/article';
import ArticleItem from './components/article/article-item';
import {createNamespacedHelpers} from 'vuex';
import YAS from 'utils/yas-constants';
const {mapState, mapActions} = createNamespacedHelpers('article');

export default {
  name: 'ArticlePage',
  data() {
    return {
      page: 1,
      id: 0,
      authorUid: 0,
      authorType: 0,
      type: '',
      userName: '',
      sceneId: YAS.scene.authorList,
      userUid: -1
    };
  },
  created() {
    this.id = +this.$route.params.id;
    this.authorUid = this.$route.params.authorUid;
    this.authorType = this.$route.params.authorType;
    this.type = this.$route.params.type;
    this.userName = this.$route.query.userName;
  },
  activated() {
    if (+this.$route.params.id !== this.id) {
      this.id = +this.$route.params.id;
      this.authorUid = this.$route.params.authorUid;
      this.authorType = this.$route.params.authorType;
      this.type = this.$route.params.type;
      this.userName = this.$route.query.userName;
      this.init();
    }
  },
  beforeRouteUpdate(to, from, next) {
    if (+this.$route.params.id !== +to.params.id) {
      this.id = +to.params.id;
      this.authorUid = to.params.authorUid;
      this.authorType = to.params.authorType;
      this.type = to.params.type;
      this.userName = to.query.userName;
      this.init();
    }
    next();
  },
  async serverPrefetch() {
    const articleId = parseInt(this.id, 10);

    if (!articleId) {
      return;
    }
    return this.fetchArticleUserList({
      articleId,
      limit: 2,
      thumb: true,
      authorUid: this.authorUid,
      authorType: this.authorType,
      type: this.type
    });
  },
  computed: {
    ...mapState(['articleUserThumbList'])
  },
  methods: {
    ...mapActions(['fetchArticleUserList', 'fetchArticleProductFavs']),
    init() {
      this.page = 1;
      this.$refs.article.init();
    },
    async onFetch() {
      const articleId = parseInt(this.id, 10);

      if (!articleId) {
        return;
      }
      const result = await this.fetchArticleUserList({
        articleId,
        page: this.page,
        authorUid: this.authorUid,
        authorType: this.authorType,
        type: this.type
      });

      if (result.code === 200) {
        if (get(result, 'data.detailList', []).length) {
          this.page++;
          this.$sdk.getUser().then(user => {
            if (user && user.uid) {
              this.userUid = +user.uid;

              // this.fetchArticleProductFavs({
              //   articles: result.data.detailList
              // });
            }
          });
          return new Promise(resolve => {
            if (this.page === 2) {
              setTimeout(() => {
                resolve(result.data.detailList);
              }, 300);
            } else {
              resolve(result.data.detailList);
            }
          });
        }
        return Promise.resolve(false);
      } else {
        this.$createToast && this.$createToast({
          txt: result.message || '服务器开小差了',
          type: 'warn',
          time: 1000
        }).show();
      }
    },
  },
  components: {
    Article,
    ArticleItem
  }
};
</script>

<style lang="scss" scoped>
.article-page {
  background-color: #f0f0f0;
}
</style>