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

export default {
  async fetchArticleList({ commit }, {
    articleId,
    authorUid,
    authorType,
    limit = 5,
    page = 1,
    thumb = false,
    local = false,
    columnType = 1001,
    singleDetail = 'N',
    fromPlatform = 'N'
  }) {
    const commitType = singleDetail === 'Y' ? 'ARTICLE_SINGLE_DETAIL' : 'ARTICLE_LIST';

    commit(Types[`FETCH_${commitType}_REQUEST`], { refresh: page === 1 });
    let result;

    if (local) {
      const preData = this.$predata.getpre(`native_grass_list_${articleId}`);

      if (preData) {
        result = {
          code: 200,
          data: {
            detailList: [utils.processArticleListData(preData.value)]
          },
          local
        };
      } else {
        return;
      }
    }
    if (!result) {
      result = await this.$predata.fetch('/api/grass/columnArticleDetail', {
        articleId,
        limit,
        page,
        authorUid,
        authorType,
        columnType,
        singleDetail,
        fromPlatform
      });
    }

    if (result && result.code === 200) {
      if (!result.data.detailList) {
        result.data.detailList = [];
      }

      commit(Types[`FETCH_${commitType}_SUCCESS`], {
        data: result.data.detailList,
        thumb,
        articleId,
        local: result.local
      });
    } else {
      commit(Types[`FETCH_${commitType}_FAILD`], {
        articleId,
        thumb,
        code: result.code,
        message: result.message,
        local: result.local
      });
    }
    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;
  },
  async fetchTopicSimpleInfo({ commit, state }, {topicId, thumb}) {
    if (state.fetchTopicInfo) {
      return Promise.resolve({});
    }

    commit(Types.FETCH_TOPIC_INFO_REQUEST);
    const result = await this.$api.post('/api/grass/getGrassTopicSimpleInfo', {topicId});

    if (result.code === 200) {
      commit(Types.FETCH_TOPIC_INFO_SUCCESS, {topicId, data: result.data, thumb});
    } else {
      commit(Types.FETCH_TOPIC_INFO_FAILD, {});
    }

    return result;
  },
  async fetchTopicRelatedArticles({ commit, state }, {topicId, page, lastedTime}) {
    commit(Types.FETCH_ARTICLE_TOPIC_REQUEST, { page });
    const result = await this.$api.post('/api/grass/topicRelatedArticles', {
      topicId,
      page,
      limit: 6,
      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 fetchDetailRecommendAricles({ commit }, { articleId }) {
    commit(Types.FETCH_DETAIL_RECOMMEND_REQUEST);
    const result = await this.$api.post('/api/grass/detailRecommendArticles', {
      articleId
    });

    if (result && result.code === 200) {
      commit(Types.FETCH_DETAIL_RECOMMEND_SUCCESS, {
        data: result.data || []
      });
    } else {
      commit(Types.FETCH_DETAIL_RECOMMEND_FAILD);
    }

    return result;
  },
  async fetchFindNiceGoodsArticles({ commit, state }, { page, limit, thumb}) {
    commit(Types.FETCH_NICE_GOODS_REQUEST, { page });
    const result = await this.$api.post('/api/grass/findGoodsList', {
      page,
      limit: limit || 6,
      lastedTime: state.articleLastedTimeByNice || void 0
    });

    if (result && result.code === 200) {
      if (!result.data.detailList) {
        result.data.detailList = [];
      }
      commit(Types.FETCH_NICE_GOODS_SUCCESS, {
        data: result.data,
        page,
        thumb
      });
    } else {
      commit(Types.FETCH_NICE_GOODS_FAILD);
    }

    return result;
  },
  async fetchTopicList({ commit, state }, {page, limit}) {
    if (state.fetchTopicList) {
      return {};
    }

    page = page || state.fetchTopicPage || 1;

    commit(Types.FETCH_TOPIC_LIST_REQUEST, { page });

    const result = await this.$api.post('/api/grass/getGrassTopicList', {
      page,
      limit: limit || 10,
      lastedTime: state.fetchTopicLastedTime || void 0
    });

    if (result && result.code === 200) {
      commit(Types.FETCH_TOPIC_LIST_SUCCESS, {
        data: result.data,
        page
      });
    } else {
      commit(Types.FETCH_TOPIC_LIST_FAILD);
    }

    return result;
  }
};