Wechat----Model 搜索 keyword

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/damys/article/details/88100148

book 搜索

import { HTTP } from "../utils/http_p.js"

class KeywordModel extends HTTP{
  key = 'q'
  maxLenght = 10    // 最多10条

  /**
   * 获取历史搜索
   */
  getHistory(){
    const words = wx.getStorageSync(this.key)
    if(!words){
      return []
    }

    return words
  }


  /**
   * 获取热门搜索
   */
  getHot(){
    return this.request({
      url:'weChatBl/BookHotKeyword'
    })
  }


  /**
   * 添加历史搜索
   */
  addToHistory(keyword){
    let words = this.getHistory()

    // 判断是否包含当前关键字, 使用ES6 includes
    const has = words.includes(keyword)

    // 存在不处理
    // 不存在,增加第一条, 并一起全部写入缓存
    if(!has){
      // 是最大条数的处理,如果大于10条,删除最后一条
      const length = words.length
      if(length >= this.maxLenght){
        words.pop()
      }
      
      // 增加第一条, 并一起全部写入缓存
      words.unshift(keyword)
      wx.setStorageSync(this.key, words)
    }
  }
}

export{
  KeywordModel
}

猜你喜欢

转载自blog.csdn.net/damys/article/details/88100148