热搜词缓存
This commit is contained in:
		
							parent
							
								
									dc08d545a2
								
							
						
					
					
						commit
						93ba1c134f
					
				@ -27,6 +27,7 @@
 | 
				
			|||||||
          <Tag
 | 
					          <Tag
 | 
				
			||||||
            v-for="(item, index) in promotionTags"
 | 
					            v-for="(item, index) in promotionTags"
 | 
				
			||||||
            :key="index"
 | 
					            :key="index"
 | 
				
			||||||
 | 
					            class="mr_10"
 | 
				
			||||||
          >
 | 
					          >
 | 
				
			||||||
            <span class="hover-color" @click="selectTags(item)">{{ item }}</span>
 | 
					            <span class="hover-color" @click="selectTags(item)">{{ item }}</span>
 | 
				
			||||||
          </Tag>
 | 
					          </Tag>
 | 
				
			||||||
@ -37,6 +38,7 @@
 | 
				
			|||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script>
 | 
					<script>
 | 
				
			||||||
 | 
					import storage from '@/plugins/storage.js'
 | 
				
			||||||
import {hotWords} from '@/api/goods.js'
 | 
					import {hotWords} from '@/api/goods.js'
 | 
				
			||||||
export default {
 | 
					export default {
 | 
				
			||||||
  name: 'search',
 | 
					  name: 'search',
 | 
				
			||||||
@ -61,7 +63,6 @@ export default {
 | 
				
			|||||||
  data () {
 | 
					  data () {
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
      searchData: '', // 搜索内容
 | 
					      searchData: '', // 搜索内容
 | 
				
			||||||
      promotionTags: [] // 热门搜索列表
 | 
					 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  methods: {
 | 
					  methods: {
 | 
				
			||||||
@ -79,22 +80,28 @@ export default {
 | 
				
			|||||||
      this.$emit('search', this.searchData)
 | 
					      this.$emit('search', this.searchData)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  mounted () {
 | 
					  computed:{
 | 
				
			||||||
 | 
					    promotionTags () {
 | 
				
			||||||
 | 
					      return JSON.parse(this.$store.state.hotWordsList)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  created () {
 | 
				
			||||||
    this.searchData = this.$route.query.keyword
 | 
					    this.searchData = this.$route.query.keyword
 | 
				
			||||||
    if (!this.hover) { // 首页顶部固定搜索栏不调用热词接口
 | 
					    if (!this.hover) { // 首页顶部固定搜索栏不调用热词接口
 | 
				
			||||||
      // 搜索热词每一小时请求一次
 | 
					      // 搜索热词每一小时请求一次
 | 
				
			||||||
      const reloadTime = this.Cookies.getItem('hotWordsReloadTime')
 | 
					      const reloadTime = storage.getItem('hotWordsReloadTime')
 | 
				
			||||||
      const time = new Date().getTime() - 60*60*1000
 | 
					      const time = new Date().getTime() - 60*60*1000
 | 
				
			||||||
      if (!reloadTime) {
 | 
					      if (!reloadTime) {
 | 
				
			||||||
        hotWords({count: 5}).then(res => {
 | 
					        hotWords({count: 5}).then(res => {
 | 
				
			||||||
          if (res.success) this.promotionTags = res.result
 | 
					          console.log(res);
 | 
				
			||||||
 | 
					          if (res.success) storage.setItem('hotWordsList', res.result)
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
        this.Cookies.setItem('hotWordsReloadTime',new Date().getTime())
 | 
					        storage.setItem('hotWordsReloadTime',new Date().getTime())
 | 
				
			||||||
      } else if (reloadTime && time > reloadTime) {
 | 
					      } else if (reloadTime && time > reloadTime) {
 | 
				
			||||||
        hotWords({count: 5}).then(res => {
 | 
					        hotWords({count: 5}).then(res => {
 | 
				
			||||||
          if (res.success) this.promotionTags = res.result
 | 
					          if (res.success) storage.setItem('hotWordsList', res.result)
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
        this.Cookies.setItem('hotWordsReloadTime',new Date().getTime())
 | 
					        storage.setItem('hotWordsReloadTime',new Date().getTime())
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -9,3 +9,6 @@ export const SET_NAVLIST = (state, data) => {
 | 
				
			|||||||
export const SET_CARTNUM = (state, data) => {
 | 
					export const SET_CARTNUM = (state, data) => {
 | 
				
			||||||
  state.cartNum = data
 | 
					  state.cartNum = data
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					export const SET_HOTWORDS = (state, data) => {
 | 
				
			||||||
 | 
					  state.hotWordsList = data
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -10,9 +10,10 @@ Vue.use(Vuex);
 | 
				
			|||||||
export default new Vuex.Store({
 | 
					export default new Vuex.Store({
 | 
				
			||||||
  state: {
 | 
					  state: {
 | 
				
			||||||
    category: [], // 全部分类
 | 
					    category: [], // 全部分类
 | 
				
			||||||
    navList: [],
 | 
					    navList: [], // 首页快捷导航
 | 
				
			||||||
    cartNum: storage.getItem('cartNum') || 0,
 | 
					    cartNum: storage.getItem('cartNum') || 0,
 | 
				
			||||||
    logoImg: require('@/assets/images/logo2.png')
 | 
					    logoImg: require('@/assets/images/logo2.png'),
 | 
				
			||||||
 | 
					    hotWordsList: storage.getItem('hotWordsList') || []
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  getters,
 | 
					  getters,
 | 
				
			||||||
  actions,
 | 
					  actions,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user