detail.js 3.08 KB
const FETCH_DETAIL_INFO = 'FETCH_DETAIL_INFO';
const UPDATE_ARTICLE_PRAISE_INFO = 'UPDATE_ARTICLE_PRAISE_INFO';
const FETCH_RECOMMEND_ARTICLES = 'FETCH_RECOMMEND_ARTICLES';

import { UPDATE_ARTICLE_PRAISE } from './list/types';

export default function() {
  return {
    namespaced: true,
    state: {
      // sort: 1 笔记 4 视频
      detailInfo: {},
      recommendArticleList: []
    },
    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(`article/articleList/${UPDATE_ARTICLE_PRAISE}`, {articleId, status}, {root: true});
          commit(UPDATE_ARTICLE_PRAISE_INFO);
        }
      },
      async fetchRecommendArticle({commit}, {articleId}) {
        const res = await this.$api.get('/api/grass/xianyuOtherArticles', {
          articleId
        });

        if (res.code === 200) {
          commit(FETCH_RECOMMEND_ARTICLES, res.data);
        }
      }
    },
    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
        };
      },
      [FETCH_RECOMMEND_ARTICLES](state, list = []) {
        state.recommendArticleList = list.map((item)=> {
          let imageHeight = item.imageHeight;
          let imageWidth = item.imageWidth;

          if (imageWidth > 350) {
            imageWidth = 350;
            imageHeight = parseInt(350 * item.imageHeight / item.imageWidth, 10);
          }

          if (item.dataType === 1) {
            item.authorHeadIco = item.authorHeadIco.replace(/{mode}/, 2).replace(/{width}/, 40).replace(/{height}/, 40);
            item.coverImage = item.coverImage.replace(/{mode}/, 2).replace(/{width}/, imageWidth).replace(/{height}/, imageHeight).replace(/\/format\/webp/, '');
          } else if (item.dataType === 2) {
            item.resourceSrc = item.resourceSrc.replace(/{mode}/, 2).replace(/{width}/, imageWidth).replace(/{height}/, imageHeight).replace(/\/format\/webp/, '');
          }

          item.imageNewWidth = imageWidth;
          item.imageNewHeight = imageHeight;

          return item;
        });
      }
    },
    getters: {
      authorInfo({detailInfo}) {
        const {authorHeadIco, authorName, authorType, authorUid} = detailInfo;

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