skupList.js 1.43 KB
import Vue from 'vue';

export default function() {
  return {
    namespaced: true,
    state: {
      page: 1,
      pageTotal: 0,
      skupList: {
        list: []
      },
      isMore: false,
      isShowEmpty: false
    },
    mutations: {
      addList(state, { data, isReset }) {
        if (data && data.skup_list) {
          let { page, skup_list = [], page_total } = data;

          if (page_total > page) {
            state.isMore = true;
            state.pageTotal = page_total;
            state.page = page + 1;
          } else {
            state.isMore = false;
          }

          if (isReset) {
            state.skupList.list = skup_list;
          } else {
            let list = state.skupList.list.concat(skup_list);

            Vue.set(state.skupList, 'list', list);
          }
        }
      },
      errorData(state) {
        state.isShowEmpty = true;
      }
    },
    actions: {
      async fetchSecondSkupList({ commit, state }, {isReset} = {}) {
        let page = 1;

        if (isReset) {
          page = 1;
        } else {
          page = state.page;
        }

        let limit = 10;
        const result = await this.$api.get('/api/ufo/home/favoriteProduct', {page, limit});

        if (result.code === 200) {
          let data = result.data;

          commit('addList', { data, isReset });
        } else {
          commit('errorData');
        }
        return result.data || [];
      },
    },
  };
}