index.js 1.21 KB
import actions from './actions';
import mutations from './mutations';
import { get } from 'lodash';

export default function() {
  return {
    namespaced: true,
    state: {
      products: {
        byId: {},
      },
      currentId: null,

      /**
       * 用户在商品详情页选择的交易信息
       * productId: 商品id
       * product: 商品详情
       * sizeId: 尺寸id
       * size: 尺寸信息
       * tradeTypeId: 交易类型id
       * tradeType: 交易类型
       */
      selectedProductInfo: {
        productId: null,
        product: {},
        sizeId: null,
        size: {},
        tradeTypeId: null,
        tradeType: {},
      },
    },
    mutations,
    actions,
    getters: {
      productDetail(state) {
        return state.products.byId[state.currentId] || {};
      },
      imageList(state, getters) {
        return get(getters.productDetail, 'goods_list[0].image_list', []);
      },
      resource(state, getters) {
        return get(getters.productDetail, 'resource', {});
      },
      activity(state, getters) {
        return get(getters.productDetail, 'activity', []);
      },
      selectedSize(state) {
        return state.selectedProductInfo.size;
      }
    }
  };
}