search.vue 6.18 KB
<template>
  <LayoutApp class="" :show-back="true">
      <form action="javascript:void(0)">
        <div class="search-header middle">
          <div class="search-img"></div>
          <input class="search-input" type="search" v-model="query" :placeholder="defaultSearchWord || 'Search'" @input="suggest"
                 @keyup.13="searchGoods"/>
          <div class="search-clear" :class="query && 'search-clear-img'" @click="clear()"></div>
        </div>
      </form>
      <Scroll v-show="!query.length" :options="scrollOptions">
        <div class="recent title middle" v-if="searchWord && searchWord.length">热门推荐</div>
        <div class="content middle">
          <div class="item" v-if="searchWord && searchWord.length" v-for="(item, index) of searchWord" @click="goSearch({query : item.search_word} ,{type: 2, index})">
            {{item.search_word}}
          </div>
        </div>
        <div class="recent middle" v-if="localHistory && localHistory.length">
          <div class="title">最近搜索</div>
          <div class="search-clear search-clear-img" @click="clearLocalHistory()"></div>
        </div>
        <div class="content middle">
          <div class="item" v-if="localHistory && localHistory.length" v-for="(item, index) of localHistory" @click="goSearch({query : item} ,{type: 1, index})">{{item}}
          </div>
        </div>
      </Scroll>
      <Scroll v-show="query.length" :options="scrollOptions" :data="searchSuggestList">
        <div class="item-line middle" v-if="searchSuggestList.length" v-for="(item, index) of searchSuggestList" @click="goSearch({query : item.item} ,{type: 0, index: 0})">
          {{item.item}}
        </div>
      </Scroll>
  </LayoutApp>
</template>
<script>
import {Scroll} from 'cube-ui';
import {createNamespacedHelpers} from 'vuex';

const {mapState, mapActions} = createNamespacedHelpers('list');

export default {
  name: 'Search',
  components: {
    Scroll,
  },
  data() {
    return {
      scrollOptions: {
        bounce: {
          top: false
        }
      },
      query: '',
      localHistory: [],
      SEARCH_POS: 1,
    };
  },
  activated() {
    this.query = '';
    this.fetchSearchWords();
    this.fetchDefaultSearchWords();
    this.setLocalList();
  },
  beforeRouteEnter: function(to, from, next) {
    next(vm => {
      if (from.name === 'List') {
        vm.SEARCH_POS = 3;
      } else if (from.name === 'ChannelPage') {
        vm.SEARCH_POS = 1;
      }
    });
  },
  computed: {
    ...mapState(['searchWord','defaultSearchWord', 'searchSuggestList']),
  },
  methods: {
    ...mapActions(['fetchSearchWords', 'fetchDefaultSearchWords', 'fetchSearchSuggest']),

    clear: function() {
      this.query = '';
    },
    searchGoods: function() {
      if(!this.query && this.defaultSearchWord) {
        this.query = this.defaultSearchWord;
      }
      this.addLocalWord();
      this.yasInput({type: 0, index: 0, query: this.query});
      this.$router.push({
        name: 'List',
        query: {
          query: this.query,
          type: 6
        }
      });
    },
    suggest: function() {
      this.fetchSearchSuggest(this.query);
    },
    goSearch: function(parameters, param) {
      let query = parameters.query;

      this.addLocalWord(query);
      param.query = query;
      this.yasInput(param);
      this.$router.push({
        name: 'List',
        query: {
          query,
          type: 6
        }
      });
    },
    clearLocalHistory: function() {
      localStorage.setItem('@YohoUFOStore:searchHistory', '');
      this.setLocalList();
    },
    setLocalList: function() {
      let localHistory = localStorage.getItem('@YohoUFOStore:searchHistory');

      if (localHistory) {
        localHistory = localHistory.split(',');
      }
      this.localHistory = localHistory;
    },
    addLocalWord: function(query) {
      if (this.query || query) {
        let addQuery = query || this.query;
        let localHistory = localStorage.getItem('@YohoUFOStore:searchHistory');

        if (localHistory) {
          let localArr = localHistory.split(',');

          if (!localArr.includes(addQuery)) {
            localStorage.setItem('@YohoUFOStore:searchHistory', addQuery + ',' + localHistory);
          }
        } else {
          localStorage.setItem('@YohoUFOStore:searchHistory', addQuery);
        }
      }
    },
    yasInput: function(param) {
      this.$store.dispatch('reportYas', {
        params: {
          param: {
            SEARCH_POS: this.SEARCH_POS,
            KEYWORD: param.query,
            POS_ID: param.type,
            TYPE_ID: 1,
            FLR_INDEX: param.type ? param.index + 1 : 0,
          },
          appop: 'XY_UFO_HOME_KEYWORD_SEARCH_C'
        }
      });
    }
  }
};
</script>
<style scoped>
  .search-header {
    height: 72px;
    background: #f5f5f5;
    flex-direction: row;
    display: flex;
    font-size: 17px;
    color: #8e8e93;
    line-height: 44px;
    align-items: center;
    border-radius: 5px;
  }

  .search-input {
    background: #f5f5f5;
    height: 72px;
    font-size: 34px;
    color: #000;
    flex: 1;
    border-radius: 0;
    padding: 14px;
  }

  .middle {
    margin: 0 40px;
  }

  .search-img {
    width: 40px;
    height: 40px;
    margin-left: 20px;
    background: url(~statics/image/list/searchPage_icon@3x.png) no-repeat;
    background-size: cover;
  }

  .search-clear {
    width: 40px;
    height: 40px;
    margin-right: 20px;
  }

  .search-clear-img {
    background: url(~statics/image/list/search_clear@3x.png) no-repeat;
    background-size: cover;
  }

  .title {
    font-size: 40px;
    font-weight: 500;
    color: #000;
  }

  .item {
    line-height: 60px;
    height: 60px;
    font-size: 24px;
    color: #000;
    padding: 0 20px;
    border-radius: 30px;
    background: #f5f5f5;
    margin-right: 20px;
    margin-bottom: 30px;
  }

  .recent {
    flex-direction: row;
    justify-content: space-between;
    display: flex;
    align-items: center;
    margin-top: 60px;
  }

  .content {
    display: flex;
    flex-wrap: wrap;
    margin-top: 30px;
  }

  .clearSearch {
    font-size: 32px;
    color: #999;
  }

  .item-line {
    line-height: 88px;
    height: 88px;
    font-size: 28px;
    color: #333;
    border-bottom: 1px #eee solid;
  }

</style>