actions.js 7.08 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, limit = 5, page = 1, thumb = false }) {
    commit(Types.FETCH_ARTICLE_LIST_REQUEST, { refresh: page === 1 });
    const result = await this.$api.get('/api/grass/columnArticleDetail', {
      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,
        thumb
      });
    } else {
      commit(Types.FETCH_ARTICLE_LIST_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.detailList,
        page
      });
    } else {
      commit(Types.FETCH_ARTICLE_TOPIC_FAILD);
    }
    return result;
  },
  async fetchArticleUserList({ commit }, { articleId, authorUid, authorType, type, limit = 5, page = 1, thumb = false }) {
    commit(Types.FETCH_ARTICLE_LIST_USER_REQUEST, { refresh: page === 1 });
    let api;

    if (type === 'fav') {
      api = '/api/grass/userFavouriteArticleDetail';
    } else if (type === 'publish') {
      api = '/api/grass/userPublishedArticleDetail';
    }
    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_USER_SUCCESS, {
        data: result.data.detailList,
        thumb
      });
    } else {
      commit(Types.FETCH_ARTICLE_LIST_USER_FAILD);
    }
    return result;
  },
  async fetchArticleUpdate({ commit }, { articleId }) {
    const result = await this.$api.get('/api/grass/columnArticleDetail', {
      articleId,
      limit: 1,
      page: 1,
      columnType: 1001
    });

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

    if (!articleData) {
      const userResult = await this.$api.get('/api/grass/minePublishedArticleDetail', {
        articleId,
        limit: 1,
        page: 1,
        authorType: 1,
      });

      articleData = get(userResult, 'data.detailList[0]');
    }

    if (articleData) {
      commit(Types.UPDATE_ARTICLE_STATE, { data: articleData });
    }
    return result;
  },
  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.productSkn);
        }
      });
    });

    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 getDetail({ commit }, { article_id, grass_id }) {
    let result = {
      getAuthor: {},
      getArticle: {},
      getArticleContent: [],
      getRecommendProducts: [],
      footer: {},
      zanBar: {},
      tagBar: [],
    };

    commit(Types.FETCH_GUANG_REQUEST);

    const content = await this.$api.post('/api/guang/article/content', { article_id });

    if (!content || content.code !== 200 || !content.data) {
      commit(Types.FETCH_GUANG_FAILED);
      return;
    }

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

    await sleep.sleep(400);
    commit(Types.GUANG_ARTICLE_CONTENT, processContents.finalDetail);
    commit(Types.GUANG_DETAIL_PRODUCT_LIST, processContents.allgoods);

    //再处理其他信息
    const [goodsList, zan, article] = 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/guang/article/zan', { articleId: grass_id }),
      this.$api.get('/api/guang/article/detail', { article_id })
    ]).then(([res1, res2, res3]) => {
      return [
        get(res1, 'data.product_list', []),
        get(res2, 'data', false),
        get(res3, 'data', false)
      ];
    });

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

    result.getRecommendProducts = processContents.recommends;

    if (zan) {
      result.footer = zan;
      result.footer.articleId = grass_id;

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

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

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

    // 等待动画完成后,提交数据
    commit(Types.FETCH_GUANG_SUCCESS, result);
  },
  async fetchProductFav({ commit, state }) {
    if (!state.articleProductList.id.length) {
      return;
    }
    const favsList = await this.$api.post('/api/favorite/batchCheckIsFavorite', {
      favIds: state.articleProductList.id.join(','),
      type: 'product'
    }).then(result => {
      return get(result, 'data', []);
    });

    commit(Types.GUANG_CHANGE_PRODUCT_FAV, favsList);
  },
  async reportArticle(actions, {articleId}) {
    const result = await this.$api.post('/api/grass/reportIllegalArticle', {articleId});

    return result;
  },
  async deleteArticle(actions, {articleId}) {
    const result = await this.$api.post('/api/grass/deleteGrassArticle', {articleId});

    return result;
  }
};