actions.js 1.94 KB
import * as Types from './types';

export default {
  /**
   * 获取社区文章瀑布流列表
   * @param commit
   * @param page
   * @param limit
   * @returns {Promise<void>}
   */
  async fetchArticleList({commit}, {page = 1, limit = 20, reload = false, lastedTime = null}) {
    commit(Types.FETCH_ARTICLE_LIST_REQUEST);
    let result = await this.$api.get('/api/grass/article/list', {
      page,
      limit,
      columnType: 1001,
      lastedTime
    }).catch(() => {
      commit(Types.FETCH_ARTICLE_LIST_FAILED);
    });

    if (result.code === 200 && result.data.list && result.data.list.length) {
      result.data.list.forEach((item) => {
        let imageHeight = item.imageHeight;
        let imageWidth = item.imageWidth;

        if (imageWidth > 350) {
          imageWidth = 350;
          imageHeight = parseInt(350 * item.imageHeight / item.imageWidth, 10);
        }

        if (item.dataType === 1) {
          item.authorHeadIco = item.authorHeadIco.replace(/{mode}/, 2).replace(/{width}/, 40).replace(/{height}/, 40);
          item.coverImage = item.coverImage.replace(/{mode}/, 2).replace(/{width}/, imageWidth).replace(/{height}/, imageHeight).replace(/\/format\/webp/, '');
        } else if (item.dataType === 2) {
          item.resourceSrc = item.resourceSrc.replace(/{mode}/, 2).replace(/{width}/, imageWidth).replace(/{height}/, imageHeight).replace(/\/format\/webp/, '');
        }

        item.imageNewWidth = imageWidth;
        item.imageNewHeight = imageHeight;
      });

      commit(Types.FETCH_ARTICLE_LIST_SUCCESS, {result, reload});
    } else {
      commit(Types.FETCH_ARTICLE_LIST_FAILED);
    }
  },

  // 点赞
  async updateArticlePraise({commit}, {articleId, status, index}) {
    let result = await this.$api.get('/api/grass/updateArticlePraise', {
      articleId,
      status
    });

    if (result.code === 200) {
      commit(Types.UPDATE_ARTICLE_PRAISE, {articleId, status, index});
    }

    return result;
  }
};