get-background-fetch-data.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // 使用周期性数据的时候,需要先调用setBackgroundFetchToken, 可在 app.js 中查看具体例子
  2. Page({
  3. onShareAppMessage() {
  4. return {
  5. title: '周期性缓存',
  6. path: 'packageAPI/pages/storage/get-background-fetch-data/get-background-fetch-data'
  7. }
  8. },
  9. onShow() {
  10. // 获取缓存的周期性更新数据
  11. // this.getBackgroundFetchData()
  12. },
  13. data: {
  14. theme: 'light',
  15. openid: '',
  16. appid: '',
  17. canIUse: true,
  18. },
  19. // getBackgroundFetchData() {
  20. // console.log('读取周期性更新数据')
  21. // const that = this
  22. // if (wx.getBackgroundFetchData) {
  23. // wx.getBackgroundFetchData({
  24. // // 当type = 'periodic' 微信客户端会每隔 12 小时向服务器请求一次数据。
  25. // fetchType: 'periodic',
  26. // success(res) {
  27. // console.log(res)
  28. // const {fetchedData} = res
  29. // const result = JSON.parse(fetchedData)
  30. // that.setData({
  31. // appid: result.appid,
  32. // openid: result.openid,
  33. // })
  34. // console.log('读取周期性更新数据成功')
  35. // },
  36. // fail() {
  37. // console.log('读取周期性更新数据失败')
  38. // wx.showToast({
  39. // title: '无缓存数据',
  40. // icon: 'none'
  41. // })
  42. // },
  43. // complete() {
  44. // console.log('结束读取')
  45. // }
  46. // })
  47. // } else {
  48. // this.setData({
  49. // canIUse: false
  50. // })
  51. // wx.showModal({
  52. // title: '微信版本过低,暂不支持本功能',
  53. // })
  54. // }
  55. // },
  56. onLoad() {
  57. this.setData({
  58. theme: wx.getSystemInfoSync().theme || 'light'
  59. })
  60. if (wx.onThemeChange) {
  61. wx.onThemeChange(({theme}) => {
  62. this.setData({theme})
  63. })
  64. }
  65. }
  66. })