get-background-prefetch-data.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const app = getApp()
  2. // 使用预缓存数据的时候,需要先调用setBackgroundFetchToken, 可在 app.js 中查看具体例子
  3. Date.prototype.Format = function (fmt) {
  4. const o = {
  5. 'M+': this.getMonth() + 1, // 月份
  6. 'd+': this.getDate(), // 日
  7. 'h+': this.getHours(), // 小时
  8. 'm+': this.getMinutes(), // 分
  9. 's+': this.getSeconds(), // 秒
  10. 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
  11. S: this.getMilliseconds() // 毫秒
  12. }
  13. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear().toString()).substr(4 - RegExp.$1.length))
  14. for (const k in o) {
  15. if (new RegExp(`(${k})`).test(fmt)) {
  16. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((`00${o[k]}`).substr((o[k].toString()).length)))
  17. }
  18. }
  19. return fmt
  20. }
  21. Page({
  22. onShow() {
  23. // 获取缓存的预拉取数据
  24. // this.getBackgroundFetchData()
  25. },
  26. onShareAppMessage() {
  27. return {
  28. title: '预拉取',
  29. path: 'packageAPI/pages/storage/get-background-prefetch-data/get-background-prefetch-data'
  30. }
  31. },
  32. data: {
  33. theme: 'light',
  34. openid: '',
  35. appid: '',
  36. getDataTime: '',
  37. canIUse: true,
  38. },
  39. // getBackgroundFetchData() {
  40. // if (wx.getBackgroundFetchData) {
  41. // console.log('读取预拉取数据')
  42. // const res = app.globalData.backgroundFetchData
  43. // const {fetchedData} = res
  44. // const result = JSON.parse(fetchedData)
  45. // const systemInfo = wx.getSystemInfoSync()
  46. // const timeStamp = systemInfo.brand === 'iPhone' ? res.timeStamp * 1000 : res.timeStamp
  47. // const time = new Date(timeStamp).Format('yyyy-MM-dd hh:mm:ss')
  48. // this.setData({
  49. // appid: result.appid,
  50. // openid: result.openid,
  51. // getDataTime: time,
  52. // })
  53. // } else {
  54. // this.setData({
  55. // canIUse: false,
  56. // })
  57. // wx.showModal({
  58. // title: '微信版本过低,暂不支持本功能',
  59. // })
  60. // }
  61. // },
  62. onLoad() {
  63. this.setData({
  64. theme: wx.getSystemInfoSync().theme || 'light'
  65. })
  66. if (wx.onThemeChange) {
  67. wx.onThemeChange(({theme}) => {
  68. this.setData({theme})
  69. })
  70. }
  71. }
  72. })