index.js 1.44 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,
        storageId: 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', []);
      },
      recommend(state, getters) {
        const list = get(getters.productDetail, 'recommend', []);

        if (list && list.length !== 0) {
          return list;
        }

        return null;
      },
      selectedSize(state) {
        return state.selectedProductInfo.size;
      }
    }
  };
}