欢迎光临
感谢一路有你

小程序 | 缓存

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 
import {HTTP} from '../util/http.js';

class classicModel extends HTTP{

  getLatest(sCallback){
    this.request({
      url: 'classic/latest',
      success: (res) => {
        // console.log(res)
        sCallback(res)//回调返回值
        this._setLatestIndex(res.index)
        //写入缓存
        let key = this._getKey(res.index)
        wx.setStorageSync(key, res)
      }
    })
  }
  //上一期下一期切换
  //现在缓存中查看是否存在,如果不存在,查找服务器
  getClassic(index,nextorpre,sCallback){
    //确定key
    let key = nextorpre == 'next' ? this._getKey(index + 1) : this._getKey(index - 1)
    let classic = wx.getStorageSync(key)
    if(!classic){
      this.request({
        url: 'classic/' + index + '/' + nextorpre,
        success: (res) => {
          // console.log(res)
          //写入缓存
          wx.setStorageSync(this._getKey(res.index), res)
          sCallback(res)//回调返回值
        }
      })
    }else{
      sCallback(classic)
    }
    
  }
  // 第一期
  isFirst(index){
    return index == 1 ? true : false
  }
  // 最后一期
  isLast(index) {
    let latestIndex = this._getLatestIndex()
    return index == latestIndex ? true : false
  }
  // 写入缓存
  _setLatestIndex(index) {
    wx.setStorageSync('latest', index)
  }
  // 读取缓存
  _getLatestIndex() {
    let index = wx.getStorageSync('latest')
    return index
  }
  //获取期刊的key
  _getKey(index){
    let key = 'classic-'+index
    return key
  }
 

}
export{classicModel}

 

赞(0) 打赏
未经允许不得转载:王明昌博客 » 小程序 | 缓存
分享到: 更多 (0)

相关推荐

  • 暂无文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

×
订阅图标按钮