actions.js 2.4 KB
import * as Types from './types';
import { get } from 'lodash';

export default {
  async fetchProductInfo({commit}, {productId}) {
    const queryTasks = ['', '/resource', '/activity', '/recommend'].map(path => {
      return this.$api.get(`/api/ufo/product${path}`, {product_id: productId}).then(result => {
        if (result.code === 200) {
          return result.data;
        } else {
          throw new Error(result.message);
        }
      });
    });

    let [detail,resource, activity, recommend] = await Promise.all(queryTasks);
    resource = [];
    commit(Types.UPDATE_PRODUCT_DETAIL, Object.assign(detail.product_info, {
      resource: get(resource, '[0].data[0]', null),
      activity,
      recommend: recommend && recommend.product_list || [],
    }));
  },
  async toggleFav({commit}, {productId, isFav}) {
    const result = await this.$api.get(`/api/ufo/product/${isFav ? 'add' : 'cancel'}`, {product_id: productId});

    if (result && result.code === 200) {
      commit(Types.UPDATE_PRODUCT_FAV, {productId, isFav});
    }
  },
  async updateTradeInfo({commit, state}, {productId, sizeInfo, tradeTypeInfo}) {
    if (sizeInfo) {
    commit(Types.UPDATE_SELECTED_RPODUCT_SIZE, {productId, sizeId: sizeInfo.size_id});
    }

    if (tradeTypeInfo) {
      commit(Types.UPDATE_SELECTED_TRADE_TYPE, {productId, tradeTypeId: tradeTypeInfo.tradeTypeId});
    }

    return {
      productId,
      sizeId: state.selectedProductInfo.sizeId,
      tradeTypeId: state.selectedProductInfo.tradeTypeId,
    };
  },
  async getSelectedTradeProduct({state, commit, dispatch}, {productId, sizeId, tradeTypeId}) {
    productId = parseInt(productId, 10);
    sizeId = parseInt(sizeId, 10);
    tradeTypeId = parseInt(tradeTypeId, 10);
    if (!(state.selectedProductInfo && state.selectedProductInfo.productId == productId ||
      state.products.byId[productId])) {
      await dispatch('fetchProductInfo', {productId});
    }

    commit(Types.UPDATE_SELECTED_RPODUCT_SIZE, {productId, sizeId});
    commit(Types.UPDATE_SELECTED_TRADE_TYPE, {productId, tradeTypeId});

    return state.selectedProductInfo;
  },
  async requestSize({state}, { sizeIds }) {
    const selectedProduct = state.selectedProductInfo;

    await this.$api.get('/api/ufo/product/addsize', {
      product_id: selectedProduct.productId,
      goods_id: get(selectedProduct.product, 'goods_list[0].goods_id'),
      size_ids: sizeIds
    });

    // 忽略错误
  },
};