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

export default {
  async fetchArticleList({ commit }, { articleId, authorUid, authorType, type, limit = 5, page = 1 }) {
    commit(Types.FETCH_ARTICLE_LIST_REQUEST, {refresh: page === 1});
    let api;

    if (type === 'fav') {
      api = '/api/grass/userFavouriteArticleDetail';
    } else if (type === 'publish') {
      api = '/api/grass/userPublishedArticleDetail';
    } else {
      api = '/api/grass/columnArticleDetail';
    }
    const result = await this.$api.get(api, {
      articleId,
      limit,
      page,
      authorUid,
      authorType,
      columnType: 1001
    });

    if (result && result.code === 200) {
      if (!result.data.detailList) {
        result.data.detailList = [];
      }
      commit(Types.FETCH_ARTICLE_LIST_SUCCESS, {
        data: result.data.detailList,
      });
    } else {
      commit(Types.FETCH_ARTICLE_LIST_FAILD);
    }
    return result;
  },
  async fetchArticleListUpdate({commit}, {articleId}) {
    const result = await this.$api.get('/api/grass/columnArticleDetail', {
      articleId,
      limit: 1,
      page: 1,
      columnType: 1001
    });

    const articleData = get(result, 'data.detailList[0]');

    if (articleData) {
      commit(Types.FETCH_ARTICLE_LIST_UPDATE, {data: articleData});
    }
  },
  async fetchArticleTopicUpdate({commit}, {articleId}) {
    const result = await this.$api.get('/api/grass/columnArticleDetail', {
      articleId,
      limit: 1,
      page: 1,
      columnType: 1001
    });

    const articleData = get(result, 'data.detailList[0]');

    if (articleData) {
      commit(Types.FETCH_ARTICLE_TOPIC_UPDATE, {data: articleData});
    }
  },
  async fetchArticleProductFavs({commit}, {articles}) {
    const products = [], ufoProducts = [];

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

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

      if (result.code === 200) {
        if (!result.data) {
          result.data = [];
        }
        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 Promise.reject(result);
    }

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

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

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

    if (content && content.code === 200 && content.data) {
      const processContents = guangProcess.processArticleDetail(content.data);

      // 插入商品
      const [goodsList, favsList] = await Promise.all([
        this.$api.post('/api/guang/article/queryGoods', {
          query: processContents.allgoods.skn.join(','),
          order: 's_t_desc',
          limit: processContents.allgoods.skn.length || 1
        }),
        this.$api.post('/api/favorite/batchCheckIsFavorite', {
          favIds: processContents.allgoods.id.join(','),
          type: 'product'
        })
      ]).then(([res1, res2]) => {
        return [get(res1, 'data.product_list', []), get(res2, 'data', [])];
      });

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

      result.getRecommendProducts = processContents.recommends;
    }

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

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

      // author
      result.getAuthor.authorUid = zan.data.authorUid;
      result.getAuthor.name = zan.data.authorName;
      result.getAuthor.avatar = zan.data.authorHeadIco;
      result.getAuthor.authorType = zan.data.authorType;
      result.getAuthor.follow = get(zan, 'data.hasAttention', false) === 'Y';
    }

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

    // 等待动画完成后,提交数据
    await sleep.sleep(200);

    commit(Types.FETCH_GUANG_SUCCESS, result);
  }
};