惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
H
Heimdal Security Blog
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Schneier on Security
G
GRAHAM CLULEY
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
C
Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
P
Proofpoint News Feed
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
S
Security Affairs
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
N
News | PayPal Newsroom
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
NISL@THU
NISL@THU

博客园 - 孤峰皓月

OD 基础 Fiddler 抓取Https时 显示 Tunnel to 443 的解决方案2 Fiddler 抓取Https时 显示 Tunnel to 443 的解决方案 Fiddler 模拟器抓包,SSL抓包不到 新浪微博视频批量上传社区投稿教程 windows 2008 R2 设置 discuz 伪静态 微信小程序-布局 微信小程序,体验版加载不了网络数据 微信小程序 自定义组件. 小程序学习一些小技巧 微信小程序基础学习笔记4 微信小程序基础学习笔记3:API 微信小程序基础学习笔记2:数据绑定相关 微信小程序基础学习笔记1 HttpWebRequest.GetRequestStream方法timeout【第3次操作时就超时】的原因及解决办法 cad 已知拱高弦长求弧长 MySQL直接将数据库的文件夹从服务器复制到本地,打开表提示需要修复 “is marked as crashed and should be repaired ” 富文本编辑器,webbrowser控件 document.execCommand() 解析 Visual studio 2010 工具箱名称不显示的原因
微信小程序: 使用本地缓存
孤峰皓月 · 2021-03-17 · via 博客园 - 孤峰皓月

微信小程序: 使用本地缓存

Posted on 2021-03-17 21:01  孤峰皓月  阅读(582)  评论()    收藏  举报

参考:https://www.bilibili.com/video/BV1RK4y1v7zv?p=19&spm_id_from=pageDriver

import { request } from "../../request/index.js"; //必须全路径

Page({

  /**
   * 页面的初始数据
   */
  data: {
      leftMenuList:[],
      rightMenuList:[],
      //当前选中的菜单索引
      currentIndex:0
  },
  //接口返回数据
  Cates:[],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   /*
      web 和 微信小程序中的本地存储的区别:
      1.读取和设置的时候区别
      web: localStorage.setItem("key","value")    localStorage.getItem("key")
      小程序:wx.setStorageSync("key","value");    wx.getStorageSync("key");

      2.存储的时候区别
      web中存储的不管是什么类型的数据,读取的时候都先转换成字符串类型,再存储进去
      小程序:不存在数据转换过程
    */
    //this.getCates();
    //加载的时候先判断本地有没有缓存,如果有缓存而且没有过期,就用缓存的数据来加载页面。
    //先判断一下本地存储中 有没有旧的数据
    //{time:Date.now(),data:[...]}
    //没有旧数据,直接网络请求
    //有旧数据而且没有过期,就使用旧数据加载
    const Cates = wx.getStorageSync('cates');//这里的key用cates
    if(!Cates)
    {//不存在就请求数据
        this.getCates();
    }
    else
    {
      if(Date.now() - Cates.time > 60 * 60 * 1000)
      {
        this.getCates(); //如果数据缓存超过1小时,重新加载网络数据
      }
      else
      {
        this.Cates = Cates.data;
        console.log("加载数据:");
        console.log(this.Cates);

        //给左侧和右侧数据整理
        let leftMenuList = this.Cates.map(v => v.cat_name); 
        let rightMenuList = this.Cates[0].children;        
        //加载页面数据
        this.setData({
            leftMenuList,
            rightMenuList
        })
      }
      
    }
  },

  getCates() {
    request({  url:"https://api-hmugo-web.itheima.net/api/public/v1/categories" })
    .then (res=> {
      this.Cates = res.data.message;

      //把数据存储到本地缓存
      wx.setStorageSync('cates', {time:Date.now(),data:this.Cates});

      //构造左侧的大菜单数据
      let leftMenuList = this.Cates.map(v=>v.cat_name);
      //构造左侧的大菜单数据
      let rightMenuList = this.Cates[0].children;

      //加载页面数据
      this.setData({
        leftMenuList,
        rightMenuList
      })
    })
 },

 //左侧菜单的点击事件
 menuClick(e){
    let _index = e.currentTarget.dataset.index;
    //构造左侧的大菜单数据
    let rightMenuList1 = this.Cates[_index].children;

    //更新数据
    this.setData({ 
      rightMenuList: rightMenuList1,
      currentIndex : _index
    })
 },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})