detail.js 1.56 KB
const FETCH_DETAIL_INFO = 'FETCH_DETAIL_INFO';
const UPDATE_ARTICLE_PRAISE_INFO = 'UPDATE_ARTICLE_PRAISE_INFO';

export default function() {
  return {
    namespaced: true,
    state: {
      // sort: 1 笔记 4 视频
      detailInfo: {}
    },
    actions: {
      async fetchDetailInfo({commit}, {articleId}) {
        const res = await this.$api.get('/api/grass/columnArticleDetail', {
          articleId,
          singleDetail: 'Y',
          fromXianyu: 'Y'
        });

        if (res.code === 200) {
          // 历史原因返回为List
          commit(FETCH_DETAIL_INFO, res.data.detailList[0] || {});
        }
      },
      async updatePraiseStatus({commit}, {articleId, status}) {
        const res = await this.$api.get('/api/grass/updateArticlePraise', {
          articleId,
          status
        });

        if (res.code === 200) {
          commit(UPDATE_ARTICLE_PRAISE_INFO);
        }
      }
    },
    mutations: {
      // 获取详情
      [FETCH_DETAIL_INFO](state, detailInfo) {
        state.detailInfo = detailInfo;
      },
      [UPDATE_ARTICLE_PRAISE_INFO](state) {
        let {hasPraise, praiseCount} = state.detailInfo;

        state.detailInfo = {
          ...state.detailInfo,
          hasPraise: hasPraise === 'Y' ? 'N' : 'Y',
          praiseCount: hasPraise === 'Y' ? praiseCount - 1 : praiseCount + 1
        };
      }
    },
    getters: {
      authorInfo({detailInfo}) {
        const {authorHeadIco, authorName, authorType, authorUid} = detailInfo;

        return {authorHeadIco, authorName, authorType, authorUid};
      }
    }
  };
}