9--微信小程序 先读缓存,读不到缓存调用接口(组件.js)

//获取应用实例
const app = getApp()
var util = require('../../utils/util.js')
Component({
  data: {
    cards: []
  },
  properties: {

  },
  lifetimes: {
    // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
    attached: function() {
      this.readdata()
    }
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function() {
      this.readdata()
    }
  },
  ready: function() {

  },
  methods: {
    getinfo: function() {
      let token = wx.getStorageSync('token')
      wx.request({
        url: app.globalData.b_info,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'cookie': token
        },
        data: {
          token
        },
        //成功后
        success: res => {
          console.log(res.data)
          let list = Array.prototype.slice.call(res.data);
          list.forEach(item => {
            let img = {
              url: "/static/images/my_banji.png",
              width: 80,
              height: 80,
              circle: true
            }
            let title = {
              text: item.classname,
              color: "#1C9ADC",
              size: 34
            }
            let tag = {
              text: util.formatTimeToYmd(item.jointime) + '加入',
              color: "#1C9ADC",
              size: 28
            }
            let header = {
              bgcolor: "F7F7F7",
              line: true
            }
            item['classid'] = item.classid
            item['creator'] = item.creator
            item['img'] = img
            item['title'] = title
            item['tag'] = tag
            item['header'] = header
            item['createtime'] = util.formatTimeToYmd(item.createtime)
          })
          this.setData({
            cards: list
          })
          // console.log(list)
          wx.setStorageSync('teacherinfo', list)
        }
      }) //结束
    },

    //读取数据
    readdata: function() {
      let data = wx.getStorageSync('teacherinfo')
      if (data) {
        this.setData({
          cards: data
        })
      } else {
        this.getinfo()
      }
    }
  },


})

猜你喜欢

转载自blog.csdn.net/xu_ze_qin/article/details/107103986