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

export default {
  ensureProduct({ commit, state }, { productId }) {
    commit(Types.ENSURE_PRODUCT_DETAIL, { productId });
    return state.products[productId];
  },
  async fetchProductInfo({ commit, state, rootGetters }, { productId }) {
    const queryUrls = ['', '/resource', '/activity', '/recommend']
    if(rootGetters.getLogin) {
      queryUrls.push('/limit/info')
    }
    const queryTasks =  queryUrls.map(path => {
      let params = { product_id: productId };

      if (path === '/resource') {
        params = {
          content_code: state.resourceContentCode,
        };
      }
      return this.$api.post(`/api/ufo/product${path}`, params).then(result => {
        if (result.code === 200) {
          return result.data;
        } else {
          return null;
        }
      });
    });

    let [detail, resource, activity, recommend, limitInfo] = await Promise.all(queryTasks);
    
    // 视频资源位
    const videoResourceInfo = resource.find(r=> /(\.mp4)/.test(r.data[0].url));
    const videoResource = get(videoResourceInfo, 'data[0]', {});

    resource = get(resource, '[0].data[0]', {});

    const {product_info = {}} = detail || {};
    commit(Types.UPDATE_PRODUCT_DETAIL, Object.assign(product_info, {
      resource,
      videoResource,
      activity: activity || [],
      recommend: recommend && recommend.product_list || [],
      limitInfo: limitInfo || {}
    }));
  },
  async fetchRecommendProduct({ commit }, { productId }) {
    const result = await this.$api.post('/api/ufo/product/recommend', { product_id: productId })

    return result?.data?.product_list ?? []
  },
  async fetchFav({ commit, rootGetters }, { productId }) {
    let isLogin = rootGetters.getLogin;

    if (!isLogin) {
      const user = await Vue.$sdk.getUser();

      isLogin = user && user.uid > 1;
    }

    if (!isLogin) {
      return false;
    }

    const isFav = await this.$api.post('/api/ufo/product/fav', { productId }).then(result => {
      if (result.code === 200) {
        return result.data;
      } else {
        return false;
      }
    });

    commit(Types.UPDATE_PRODUCT_FAV, { productId, isFav });
  },
  async fetchBrandTop({ commit }, { productId }) {
    const result = await this.$api.post('/api/ufo/product/top', { product_id: productId });

    if (result.code === 200) {
      const productList = result.data.product_list || [];

      commit(Types.UPDATE_BRAND_PRODUCT_TOP_LIST, { productId, topList: productList});
    }
  },
  async setupInitialProductInfo({ commit, state, dispatch }, payload) {
    const { id: productId } = payload;
    if (!productId) {
      return;
    }

    const productInfo = await dispatch('ensureProduct', {productId});

    if (productInfo.product_name === null) {
      commit(Types.SETUP_INITIAL_PRODUCT_INFO, payload);
    }
  },
  async toggleFav({ commit }, { productId, isFav }) {
    const result = await this.$api.post(`/api/ufo/product/favorite/${isFav ? 'add' : 'cancel'}`, { productId });

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

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

    commit(Types.UPDATE_SELECTED_PRODUCT_SIZE, { productId, storageId });

    return state.selectedProductInfo;
  },
  resetSelectedSize({commit}) {
    commit(Types.RESET_SELECTED_PRODUCT_SIZE);
  },
  async requestSize({ state }, { product_id, goods_id, size_ids }) {
    const selectedProduct = state.selectedProductInfo;

    await this.$api.post('/api/ufo/product/addsize', {
      product_id,
      goods_id,
      size_ids,
    });

    // 忽略错误
  },
  async payment(context, { skup }) {
    return this.$api.post('/api/ufo/product/order/payment', { skup, api_version: 1 });
  },
};