actions.js 5.34 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 fetchArticleProductFavs({commit}, {articles}) {
    const products = [], ufoProducts = [];

    articles.forEach(article => {
      get(article, 'productList', []).forEach(product => {
        if (article.articleProductType === 1) {
          products.push(product.id);
        } else if (article.articleProductType === 2) {
          ufoProducts.push(product.id);
        }
      });
    });

    if (products.length) {
      const result = await this.$api.get('/api/favorite/batchCheckIsFavorite', {
        favIds: products.join(','),
        type: 'product'
      });

      if (result.code === 200) {
        if (!result.data) {
          result.data = [];
        }
        commit(Types.FETCH_ARTICLE_PRODUCT_SUCCESS, {articles, favs: result.data, articleProductType: 1});
      }
    }
    if (ufoProducts.length) {
      const result = await this.$api.get('/api/ufo/user/isFavoriteBatch', {
        productIds: ufoProducts.join(','),
      });

      commit(Types.FETCH_ARTICLE_PRODUCT_SUCCESS, {articles, favs: result.data, articleProductType: 2});
    }
  },
  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;
      result.getAuthor.authorUid = result.getAuthor.author_id;
      result.getAuthor.fellow = get(zan, 'data.hasAttention', false);
    }

    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, favsList] = await Promise.all([this.$api.get('/api/guang/article/queryGoods', {
        query: processContents.allgoods.join(','),
        order: 's_t_desc',
        limit: processContents.allgoods.length || 1
      }), this.$api.get('/api/favorite/batchCheckIsFavorite', {
        favIds: processContents.allgoods.join(','),
        type: 'product'
      })]).then(([res1, res2]) => {
        return [get(res1, 'data.product_list', []), get(res2, 'data', [])];
      });

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

    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);
  },

};