1
+ // 使用预缓存数据的时候,需要先调用setBackgroundFetchToken, 可在 app.js 中查看具体例子
2
+
3
+ Date . prototype . Format = function ( fmt ) {
4
+ var 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
+
14
+ if ( / ( y + ) / . test ( fmt ) ) fmt = fmt . replace ( RegExp . $1 , ( this . getFullYear ( ) + "" ) . substr ( 4 - RegExp . $1 . length ) ) ;
15
+ for ( var k in o ) {
16
+ if ( new RegExp ( "(" + k + ")" ) . test ( fmt ) ) {
17
+ fmt = fmt . replace ( RegExp . $1 , ( RegExp . $1 . length == 1 ) ? ( o [ k ] ) : ( ( "00" + o [ k ] ) . substr ( ( "" + o [ k ] ) . length ) ) ) ;
18
+ }
19
+ }
20
+
21
+ return fmt ;
22
+ }
23
+
24
+ Page ( {
25
+ onLoad ( ) {
26
+ // 获取缓存的周期性更新数据
27
+ this . getBackgroundFetchData ( ) ;
28
+ } ,
29
+ data : {
30
+ openid : '' ,
31
+ appid : '' ,
32
+ getDataTime : '' ,
33
+ } ,
34
+ getBackgroundFetchData ( ) {
35
+ console . log ( '读取周期性更新数据' )
36
+ const that = this ;
37
+ wx . getBackgroundFetchData ( {
38
+ fetchType : 'pre' ,
39
+ success ( res ) {
40
+ console . log ( res )
41
+ const { fetchedData } = res ;
42
+ const result = JSON . parse ( fetchedData )
43
+ // 在 Iphone 下返回的 timeStamp 单位是秒
44
+ const systemInfo = wx . getSystemInfoSync ( ) ;
45
+ const timeStamp = systemInfo . brand === 'iPhone' ? res . timeStamp * 1000 : res . timeStamp
46
+ const time = new Date ( timeStamp ) . Format ( "yyyy-MM-dd hh:mm:ss" ) ;
47
+ that . setData ( {
48
+ appid : result . appid ,
49
+ openid : result . openid ,
50
+ getDataTime : time ,
51
+
52
+ } )
53
+ console . log ( '读取周期性更新数据成功' )
54
+ } ,
55
+ fail ( ) {
56
+ console . log ( '读取周期性更新数据失败' )
57
+ wx . showToast ( {
58
+ title : '无缓存数据' ,
59
+ icon : 'none'
60
+ } )
61
+ } ,
62
+ complete ( ) {
63
+ console . log ( '结束读取' )
64
+ }
65
+ } )
66
+ }
67
+ } )
0 commit comments