index.js 5.28 KB
import {get} from 'lodash';

export default function() {
  return {
    namespaced: true,
    state: {
      productList: {
        showErrorPage: false,
        isFetching: false,
        error: null,
        page: 0, // 当前页
        page_size: 10, // 每页数量
        page_total: 0, // 总共多少页
        total: 0, // 总共多少条
        endReached: false, // 到达底部
        list: [], // 商品列表
        isEmpty: false,
      },
      searchParams: {
        type: 0, // type:0,推荐;1,热销;2,即将发售; 3,品类; 4,品牌;5,系列;6,搜索 7, 收藏
        order: 'sale_desc', // 指定排序
        productPool: null, // 商品池id
        sort: null, // 品类id
        brand: null, // 品牌id
        series: null, // 系列id
        gender: null, // 性别
        size: null, // 尺码id
        isSoonSale: null, // 是否是即将售卖
        query: null, // 搜索词
        limit: null, // 每页记录数
        page: null, // 当前页号
        coupon_token: null, // 优惠券token
      },
      filterParams: {
        sort: [], // 品类id
        brand: [], // 品牌id
        gender: [], // 性别
        size: [], // 尺码id
      },
      filterData: [],
      filterVisible: false,
      searchWord: [],
      defaultSearchWord: [],
      searchSuggestList: [],
    },
    mutations: {
      addProductList(state, {data}) {
        let {productList} = state;

        if (typeof data === 'object' && Object.keys(data).length) {
          for (let key in data) {
            if (key === 'product_list') {
              productList.list = data.page > 1 ? productList.list.concat(data.product_list) : data.product_list;
            } else {
              productList[key] = data[key];
            }
          }
          state.productList = productList;
        }
      },
      setSearchParams(state, {params}) {
        state.searchParams = Object.assign(state.searchParams, params);
      },
      addFilterData(state, {filter}) {
        state.filterData = filter;
      },
      setFilterParam(state, {filter}) {
        let {filterParams} = state;

        if (typeof filter === 'object' && Object.keys(filter).length) {
          for (let key in filter) {
            filterParams[key] = filter[key];
          }
          console.log(filterParams);
          state.filterParams = filterParams;
        }
      },
      setSearchQuery(state, {query}) {
        state.searchParams = Object.assign(state.searchParams, {query});
      },
      setSearchSuggest(state, {searchSuggestList}) {
        console.log(searchSuggestList);
        state.searchSuggestList = searchSuggestList.items;
      },
      setSearchWords(state, {searchWord}) {
        state.searchWord = searchWord;
      },
      setDefaultSearchWords(state, {defaultSearchWord}) {
        state.defaultSearchWord = defaultSearchWord;
      }
    },
    actions: {
      fetchProductList: async function({commit, state}, params) {
        let searchParams = state.searchParams;
        let list = state.productList;
        let pageSize = list.page_size;
        let isReset = false;

        if (params && params.isReset) {
          isReset = true;
          delete params.isReset;
        }
        if (params && Object.keys(params).length) {
          searchParams = Object.assign({...searchParams}, params);
          commit('setSearchParams', {params: searchParams});
        }
        if (!isReset && (list.endReached || (!list.endReached && list.page_total === 1))) {
          return;
        }
        let page = isReset ? 1 : (list.page + 1);

        for (let key in searchParams) {
          if (!searchParams[key]) {
            delete searchParams[key];
          }
        }
        let result = await this.$api.get('/api/ufo/list/productList', {
          ...searchParams,
          page,
          pageSize
        });

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

          data.endReached = (data.page === data.page_total) && (data.page_size !== 1);
          commit('addProductList', {data});
        }

        return result.data ? result.data.length : 0;
      },
      fetchFilterData: async function({commit, state}) {
        let searchParams = state.searchParams;

        let result = await this.$api.get('/api/ufo/list/filterData', {...searchParams});

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

          commit('addFilterData', {filter: data.filter});
        }
      },
      setFilterParam: function({commit}, params) {
        commit('setFilterParam', {filter: params});
      },
      setSearchQuery: function({commit}, query) {
        commit('setSearchQuery', {query});

      },
      fetchSearchWords: async function({commit}) {
        let result = await this.$api.get('/api/ufo/list/searchWords', { word_type: 0});

        commit('setSearchWords', {searchWord: result.data});
      },
      fetchDefaultSearchWords: async function({commit}) {
        let result = await this.$api.get('/api/ufo/list/searchWords', { word_type: 1});

        commit('setDefaultSearchWords', {defaultSearchWord: result.data});
      },
      fetchSearchSuggest: async function({commit}, query) {
        let result = await this.$api.get('/api/ufo/list/searchSuggest', {query});

        commit('setSearchSuggest', {searchSuggestList: result.data});

      }
    }
  };
}