index.js 1.95 KB
export default function() {
  return {
    namespaced: true,
    state: {
      filterVisible: false,
      searchWord: [],
      defaultSearchWord: '',
      searchSuggestList: [],
    },
    mutations: {
      setSearchSuggest(state, {searchSuggestList}) {
        console.log(searchSuggestList);
        state.searchSuggestList = searchSuggestList.items;
      },
      setSearchWords(state, {searchWord}) {
        state.searchWord = searchWord;
      },
      setDefaultSearchWords(state, {defaultSearchWord = []}) {
        const [{search_word} = {}] = defaultSearchWord;

        state.defaultSearchWord = search_word;
      }
    },
    actions: {
      // 查询商品列表
      fetchProductList: async function({commit, state}, params) {
        let result = await this.$api.get('/api/ufo/list/productList', {
          ...params
        });

        return result;
      },

      // 查询筛选列表
      fetchFilterData: async function({commit, state}, params) {
        let result = await this.$api.get('/api/ufo/list/filterData', {...params});

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

          return data.filter;
        } else {
          return [];
        }
      },

      // 请求查询关键词
      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});
      },
    }
  };
}