actions.js 3.95 KB
import * as Types from './types';
import { get } from 'lodash';
import * as guangProcess from './guangProcess';


export default {
  async fetchArticleList({ commit }, { articleId, limit = 5, page = 1 }) {
    commit(Types.FETCH_ARTICLE_DETAIL_REQUEST);
    const result = await this.$api.get('/api/grass/columnArticleDetail', {
      articleId,
      limit,
      page,
      columnType: 1001
    });

    if (result && result.code === 200) {
      if (!result.data.detailList) {
        result.data.detailList = [];
      }
      commit(Types.FETCH_ARTICLE_DETAIL_SUCCESS, {
        data: result.data.detailList,
        page
      });
    } else {
      commit(Types.FETCH_ARTICLE_DETAIL_FAILD);
    }
    return result;
  },
  async fetchArticleListByTopic({commit, state}, {labelId, limit = 5, page = 1}) {
    commit(Types.FETCH_ARTICLE_TOPIC_REQUEST, {page});
    const result = await this.$api.get('/api/grass/labelRealtedArticleDetail', {
      labelId,
      limit,
      page,
      lastedTime: state.articleLastedTimeByTopic || void 0
    });

    if (result && result.code === 200) {
      if (!result.data.detailList) {
        result.data.detailList = [];
      }
      commit(Types.FETCH_ARTICLE_TOPIC_SUCCESS, {
        data: result.data,
        page
      });
    } else {
      commit(Types.FETCH_ARTICLE_TOPIC_FAILD);
    }
    return result;
  },
  async getDetail({ commit }, { article_id, grass_id }) {
    let result = {
      getAuthor: {},
      getArticle: {},
      getArticleContent: [],
      getRecommendProducts: [],
      footer: {},
      zanBar: {},
      tagBar: [],
    };

    const data = await this.$api.get('/api/guang/article/detail', {
      article_id
    });

    if (!data || data.code !== 200) {
      result.code = 400;
      return result;
    }

    let article = result.getArticle = data && data.data || {};

    let promises = [
      this.$api.get('/api/guang/article/author', { author_id: article.author_id }),
      this.$api.get('/api/guang/article/content', { article_id }),
      this.$api.get('/api/guang/article/zan', { articleId: grass_id })
    ];

    const [author, content, zan] = await Promise.all(promises);

    if (author && author.code === 200 && author.data) {
      result.getAuthor = author.data;
    }

    if (content && content.code === 200 && content.data) {
      const recommendProducts = content.data.filter(i => 'recommend_products' in i).reduce((t, c) => {
        t = t.concat(c.recommend_products.data);
        return t;
      }, []);

      result.getRecommendProducts = recommendProducts;

      const processContents = guangProcess.processArticleDetail(content.data);

      // 插入商品
      const goodsList = await this.$api.get('/api/guang/article/queryGoods', {
        query: processContents.allgoods.join(','),
        order: 's_t_desc',
        limit: processContents.allgoods.length || 1
      }).then(res => {
        return get(res, 'data.product_list', []);
      });

      result.getArticleContent = guangProcess.pushGoodsInfo(processContents.finalDetail, goodsList);
    }

    if (zan && zan.code === 200 && zan.data) {
      result.footer = zan.data;

      result.zanBar.praiseHeadIco = result.footer.praiseHeadIco;
      result.zanBar.praiseCount = result.footer.praiseCount;
      result.zanBar.publish_time = article.publish_time;
    }

    if (article && article.tags) {
      result.tagBar = article.tags;
    }

    commit(Types.FETCH_GUANG_SUCCESS, result);
  },
  async setFav({ commit }, { status, articleId }) {
    const FAV = {
      0: 'N', // 取消
      1: 'Y' // 收藏
    };

    const result = await this.$api.get('/api/guang/article/setFav', { isAdd: FAV[status], articleId });

    commit(Types.FETCH_FAV_SUCCESS, result);
  },

  async setZan({ commit }, { status, articleId }) {
    const ZAN = {
      0: 1, // 取消
      1: 0 // 收藏
    };

    const result = await this.$api.get('/api/guang/article/setZan', { status: ZAN[status], articleId });

    commit(Types.FETCH_ZAN_SUCCESS, result);
  },

};