Blame view

apps/store/article/actions.js 10.7 KB
陈峰 authored
1
import * as Types from './types';
陈峰 authored
2
import { get } from 'lodash';
htoooth authored
3
import * as guangProcess from './guangProcess';
TaoHuang authored
4
import * as sleep from '../../utils/sleep';
陈峰 authored
5
import utils from './utils';
htoooth authored
6
陈峰 authored
7
export default {
陈峰 authored
8 9 10 11 12 13 14
  async fetchArticleList({ commit }, {
    articleId,
    authorUid,
    authorType,
    limit = 5,
    page = 1,
    thumb = false,
陈峰 authored
15
    local = false,
陈峰 authored
16
    columnType = 1001,
yyq authored
17 18
    singleDetail = 'N',
    fromPlatform = 'N'
陈峰 authored
19
  }) {
yyq authored
20 21 22
    const commitType = singleDetail === 'Y' ? 'ARTICLE_SINGLE_DETAIL' : 'ARTICLE_LIST';

    commit(Types[`FETCH_${commitType}_REQUEST`], { refresh: page === 1 });
陈峰 authored
23 24 25 26 27 28 29 30 31 32
    let result;

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

      if (preData) {
        result = {
          code: 200,
          data: {
            detailList: [utils.processArticleListData(preData.value)]
33 34
          },
          local
陈峰 authored
35 36 37 38 39 40 41 42 43 44 45 46
        };
      } else {
        return;
      }
    }
    if (!result) {
      result = await this.$predata.fetch('/api/grass/columnArticleDetail', {
        articleId,
        limit,
        page,
        authorUid,
        authorType,
陈峰 authored
47
        columnType,
yyq authored
48 49
        singleDetail,
        fromPlatform
陈峰 authored
50 51
      });
    }
陈峰 authored
52 53

    if (result && result.code === 200) {
陈峰 authored
54 55 56
      if (!result.data.detailList) {
        result.data.detailList = [];
      }
yyq authored
57 58

      commit(Types[`FETCH_${commitType}_SUCCESS`], {
陈峰 authored
59
        data: result.data.detailList,
yyq authored
60
        thumb,
61 62
        articleId,
        local: result.local
陈峰 authored
63 64
      });
    } else {
yyq authored
65 66 67 68
      commit(Types[`FETCH_${commitType}_FAILD`], {
        articleId,
        thumb,
        code: result.code,
69 70
        message: result.message,
        local: result.local
yyq authored
71
      });
陈峰 authored
72 73
    }
    return result;
陈峰 authored
74
  },
TaoHuang authored
75 76
  async fetchArticleListByTopic({ commit, state }, { labelId, limit = 5, page = 1 }) {
    commit(Types.FETCH_ARTICLE_TOPIC_REQUEST, { page });
陈峰 authored
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    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;
  },
陈峰 authored
97
  async fetchArticleUserList({ commit }, { articleId, authorUid, authorType, type, limit = 5, page = 1, thumb = false }) {
TaoHuang authored
98
    commit(Types.FETCH_ARTICLE_LIST_USER_REQUEST, { refresh: page === 1 });
陈峰 authored
99 100 101 102 103 104 105 106
    let api;

    if (type === 'fav') {
      api = '/api/grass/userFavouriteArticleDetail';
    } else if (type === 'publish') {
      api = '/api/grass/userPublishedArticleDetail';
    }
    const result = await this.$api.get(api, {
陈峰 authored
107
      articleId,
陈峰 authored
108 109 110 111
      limit,
      page,
      authorUid,
      authorType,
陈峰 authored
112 113 114
      columnType: 1001
    });
陈峰 authored
115 116 117 118 119 120
    if (result && result.code === 200) {
      if (!result.data.detailList) {
        result.data.detailList = [];
      }
      commit(Types.FETCH_ARTICLE_LIST_USER_SUCCESS, {
        data: result.data.detailList,
陈峰 authored
121
        thumb
陈峰 authored
122 123 124
      });
    } else {
      commit(Types.FETCH_ARTICLE_LIST_USER_FAILD);
陈峰 authored
125
    }
陈峰 authored
126
    return result;
陈峰 authored
127
  },
陈峰 authored
128
  async fetchArticleUpdate({ commit }, { articleId }) {
陈峰 authored
129 130 131 132 133 134 135
    const result = await this.$api.get('/api/grass/columnArticleDetail', {
      articleId,
      limit: 1,
      page: 1,
      columnType: 1001
    });
陈峰 authored
136 137 138 139 140 141 142 143 144 145 146 147
    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]');
    }
陈峰 authored
148 149

    if (articleData) {
TaoHuang authored
150
      commit(Types.UPDATE_ARTICLE_STATE, { data: articleData });
陈峰 authored
151
    }
陈峰 authored
152
    return result;
陈峰 authored
153
  },
TaoHuang authored
154
  async fetchArticleProductFavs({ commit }, { articles }) {
陈峰 authored
155 156 157 158 159
    const products = [], ufoProducts = [];

    articles.forEach(article => {
      get(article, 'productList', []).forEach(product => {
        if (article.articleProductType === 1) {
陈峰 authored
160
          products.push(product.productId);
陈峰 authored
161
        } else if (article.articleProductType === 2) {
陈峰 authored
162
          ufoProducts.push(product.productSkn);
陈峰 authored
163 164 165 166 167 168 169 170 171 172 173
        }
      });
    });

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

      if (result.code === 200) {
陈峰 authored
174 175 176
        if (!result.data) {
          result.data = [];
        }
TaoHuang authored
177
        commit(Types.FETCH_ARTICLE_PRODUCT_SUCCESS, { articles, favs: result.data, articleProductType: 1 });
陈峰 authored
178 179 180 181 182 183 184
      }
    }
    if (ufoProducts.length) {
      const result = await this.$api.get('/api/ufo/user/isFavoriteBatch', {
        productIds: ufoProducts.join(','),
      });
陈峰 authored
185 186 187 188
      if (result.code === 200) {
        if (!result.data) {
          result.data = [];
        }
TaoHuang authored
189
        commit(Types.FETCH_ARTICLE_PRODUCT_SUCCESS, { articles, favs: result.data, articleProductType: 2 });
陈峰 authored
190
      }
陈峰 authored
191 192
    }
  },
htoooth authored
193
  async getDetail({ commit }, { article_id, grass_id }) {
htoooth authored
194 195 196
    let result = {
      getAuthor: {},
      getArticle: {},
htoooth authored
197 198 199 200 201
      getArticleContent: [],
      getRecommendProducts: [],
      footer: {},
      zanBar: {},
      tagBar: [],
htoooth authored
202 203
    };
TaoHuang authored
204 205 206
    commit(Types.FETCH_GUANG_REQUEST);

    const content = await this.$api.post('/api/guang/article/content', { article_id });
htoooth authored
207
TaoHuang authored
208 209 210
    if (!content || content.code !== 200 || !content.data) {
      commit(Types.FETCH_GUANG_FAILED);
      return;
htoooth authored
211 212
    }
TaoHuang authored
213 214
    const processContents = guangProcess.processArticleDetail(content.data);
陈峰 authored
215
    await sleep.sleep(400);
TaoHuang authored
216
    commit(Types.GUANG_ARTICLE_CONTENT, processContents.finalDetail);
TaoHuang authored
217
    commit(Types.GUANG_DETAIL_PRODUCT_LIST, processContents.allgoods);
TaoHuang authored
218
陈峰 authored
219
    // 再处理其他信息
TaoHuang authored
220
    const [goodsList, zan, article] = await Promise.all([
TaoHuang authored
221 222 223 224 225 226 227
      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 })
TaoHuang authored
228
    ]).then(([res1, res2, res3]) => {
TaoHuang authored
229 230
      return [
        get(res1, 'data.product_list', []),
TaoHuang authored
231 232
        get(res2, 'data', false),
        get(res3, 'data', false)
TaoHuang authored
233 234
      ];
    });
htoooth authored
235
TaoHuang authored
236 237
    result.getArticleContent = guangProcess.pushGoodsInfo(
      processContents.finalDetail,
TaoHuang authored
238
      goodsList
TaoHuang authored
239
    );
htoooth authored
240
TaoHuang authored
241
    result.getRecommendProducts = processContents.recommends;
htoooth authored
242
TaoHuang authored
243 244
    if (zan) {
      result.footer = zan;
htoooth authored
245
      result.footer.articleId = grass_id;
htoooth authored
246
htoooth authored
247 248
      result.zanBar.praiseHeadIco = result.footer.praiseHeadIco;
      result.zanBar.praiseCount = result.footer.praiseCount;
htoooth authored
249 250

      // author
TaoHuang authored
251 252 253 254 255
      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';
htoooth authored
256 257
    }
htoooth authored
258 259
    if (article && article.tags) {
      result.tagBar = article.tags;
TaoHuang authored
260
      result.zanBar.publish_time = article.publish_time;
htoooth authored
261
    }
htoooth authored
262
TaoHuang authored
263
    // 等待动画完成后,提交数据
htoooth authored
264
    commit(Types.FETCH_GUANG_SUCCESS, result);
yyq authored
265
  },
TaoHuang authored
266 267 268 269 270 271 272 273 274 275 276 277
  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);
278
  },
yyq authored
279 280 281 282 283 284 285 286 287
  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;
yyq authored
288
  },
yyq authored
289
  async fetchTopicSimpleInfo({ commit, state }, {topicId, thumb}) {
yyq authored
290
    if (state.fetchTopicInfo) {
yyq authored
291 292 293 294 295 296 297
      return Promise.resolve({});
    }

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

    if (result.code === 200) {
yyq authored
298
      commit(Types.FETCH_TOPIC_INFO_SUCCESS, {topicId, data: result.data, thumb});
yyq authored
299 300 301 302 303 304
    } else {
      commit(Types.FETCH_TOPIC_INFO_FAILD, {});
    }

    return result;
  },
yyq authored
305
  async fetchTopicRelatedArticles({ commit, state }, {topicId, page, type}) {
yyq authored
306 307 308 309
    commit(Types.FETCH_ARTICLE_TOPIC_REQUEST, { page });
    const result = await this.$api.post('/api/grass/topicRelatedArticles', {
      topicId,
      page,
yyq authored
310
      type,
yyq authored
311
      limit: 6,
yyq authored
312 313
      lastedTime: state.articleLastedTimeByTopic || void 0
    });
yyq authored
314
yyq authored
315 316 317 318 319 320 321 322
    if (result && result.code === 200) {
      if (!result.data.detailList) {
        result.data.detailList = [];
      }
      commit(Types.FETCH_ARTICLE_TOPIC_SUCCESS, {
        data: result.data.detailList,
        page
      });
yyq authored
323
    } else {
yyq authored
324
      commit(Types.FETCH_ARTICLE_TOPIC_FAILD);
yyq authored
325 326 327
    }

    return result;
yyq authored
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  },
  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;
yyq authored
344 345 346 347 348
  },
  async fetchFindNiceGoodsArticles({ commit, state }, { page, limit, thumb}) {
    commit(Types.FETCH_NICE_GOODS_REQUEST, { page });
    const result = await this.$api.post('/api/grass/findGoodsList', {
      page,
.  
ityuany authored
349
      limit: limit || 5,
yyq authored
350 351 352 353 354 355 356 357
      lastedTime: state.articleLastedTimeByNice || void 0
    });

    if (result && result.code === 200) {
      if (!result.data.detailList) {
        result.data.detailList = [];
      }
      commit(Types.FETCH_NICE_GOODS_SUCCESS, {
yyq authored
358
        data: result.data,
yyq authored
359 360 361 362 363 364 365 366
        page,
        thumb
      });
    } else {
      commit(Types.FETCH_NICE_GOODS_FAILD);
    }

    return result;
yyq authored
367 368 369 370 371 372 373 374 375 376 377 378 379
  },
  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,
yyq authored
380
      filter: 'Y',
yyq authored
381 382 383 384 385 386 387 388 389 390 391 392 393
      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;
htoooth authored
394
  }
陈峰 authored
395
};