forked from xiandanin/LardMonkeyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
100 lines (97 loc) · 2.56 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//app.js
App({
onLaunch: function() {
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
} else {
wx.cloud.init({
env: 'magnetw-mini',
traceUser: true,
})
//初始化配置
let config = wx.getStorageSync("config")
if (typeof config == 'object') {
this.globalData.config = config;
}
//降低规则更新频率 超过24小时才更新一次规则
if (new Date().getTime() - this.globalData.config.lastRuleModified > 86400000) {
this.requestMagnetRule(function() {
console.log("初始化更新缓存成功-->")
}, function() {
console.log("初始化更新缓存失败-->")
})
} else {
console.log("未到更新时间-->")
}
}
},
/**
* 供页面调用 全局数据完成后
*/
onRulesComplete: function(callback, error) {
let rules = wx.getStorageSync("rules")
if (rules != null && rules.length > 0) {
console.log("从缓存加载规则-->")
this.globalData.rules = rules
callback(rules)
} else {
console.log("没有缓存,从网络获取规则-->")
this.requestMagnetRule(callback, error)
}
},
/**
* 请求规则列表 只更新缓存
*/
requestMagnetRule: function(callback, error) {
wx.showNavigationBarLoading()
let that = this
wx.cloud.callFunction({
name: 'requestGet',
data: {
"uri": "api/source"
},
success: res => {
wx.hideNavigationBarLoading()
if (res.result.success) {
console.log(res.result)
callback(res.result.data)
that.globalData.rules = res.result.data
//更新规则时间
that.globalData.config.lastRuleModified = new Date().getTime()
that.applyConfig(that.globalData.config)
//更新缓存
wx.setStorageSync('rules', res.result.data);
} else {
error("规则载入失败");
}
},
fail: err => {
error("规则载入失败");
},
})
},
setLastRule: function(e) {
wx.setStorage({
key: 'last-rule',
data: e
})
},
getLastRule: function(e) {
let last = wx.getStorageSync("last-rule")
return last != null && last.length <= 0 ? null : last;
},
applyConfig: function(newConfig) {
this.globalData.config = newConfig
wx.setStorage({
key: 'config',
data: newConfig
});
},
globalData: {
rules: [],
config: {
lastRuleModified: 0,
trackersEnabled: true
}
}
})