-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
148 lines (148 loc) · 4.77 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
let description_keywords = {
competition: {
time: 'publishdate'
},
lecture: {
content: 'place',
time: 'time',
speaker: 'speaker'
}
};
let parseDescriptions = (_post, _type) => {
let _result = {};
Object.keys(description_keywords[_type]).forEach(_key => _result[_key] = _post[description_keywords[_type][_key]]);
return _result;
};
let parsePost = (_post, _favorited, _type) => ({
id: _type + '_' + _post.id,
title: _post.title,
descriptions: parseDescriptions(_post, _type),
detail: _post.detail,
favorited: _favorited
});
let getFavsFromStorage = _type => {
let _favorites;
try{
_favorites = JSON.parse(wx.getStorageSync('favorited_' + _type + '_id'));
} catch(e) { _favorites = []; }
return new Map(_favorites);
};
let parsePosts = (_posts, _type) => {
let _favorites = getFavsFromStorage(_type);
return new Map(
_posts.map(_post => [
_type + '_' + _post.id,
parsePost(_post, !!_favorites.get(_type + '_' + _post.id), _type),
])
);
};
let mapToArray = _map => {
let _arr = [];
_map.forEach(_element => _arr.push(_element));
return _arr;
};
let compareTime = (_p0, _p1) => _p0.descriptions.time > _p1.descriptions.time;
App({
globalData: {
userInfo: null,
lectures: null,
competitions: null,
spider_apis: {
lecture: 'https://api.hackswjtu.com/lecture/lastweek',
competition: 'https://api.hackswjtu.com/competition/recent'
}
},
refreshUserInfo(_options) {
let _fail = () => {
console.log(1);
this.globalData.userInfo = null;
_options.fail && _options.fail();
};
wx.login({
success: () => {
wx.getUserInfo({
success: _res => {
this.globalData.userInfo = _res.userInfo;
_options.success(this.globalData.userInfo);
},
fail: _fail
});
},
fail: _fail
});
},
getUserInfo(_options) {
let _fail = () => this.refreshUserInfo(_options);
if(!this.globalData.userInfo)
!this.refreshUserInfo(_options);
},
refreshPostsByType(_options) {
wx.request({
url: this.globalData.spider_apis[_options.type],
success: _res => {
this.globalData[_options.type + 's'] = parsePosts(_res.data[_options.type], _options.type);
_options.success(mapToArray(this.globalData[_options.type + 's']).sort(compareTime));
},
fail: () => {
this.globalData[_options.type + 's'] = null;
_options.fail && _options.fail();
}
});
},
getPostsByType(_options) {
if(this.globalData[_options.type + 's'] !== null)
_options.success(mapToArray(this.globalData[_options.type + 's']).sort(compareTime));
else
this.refreshPostsByType(_options);
},
refreshPosts(_options) {
let _result;
this.refreshPostsByType({
type: 'lecture',
success: _lectures => {
this.refreshPostsByType({
type: 'competition',
success: _competitions => _options.success(_lectures.concat(_competitions).sort(compareTime)),
fail: _options.fail
});
},
fail: () => _options.fail && _options.fail()
});
},
getEachTypesOfPosts(_options, _types, _results = []) {
let _current_type = _types.shift();
let _current_options = {fail: _options.fail};
let _success = _res => {
_results = _results.concat(_res);
_types.length ? this.getEachTypesOfPosts(_options, _types, _results) : _options.success(_results.sort(compareTime));
};
this.getPostsByType({
type: _current_type,
success: _success,
fail: _options.fail
});
},
getPosts(_options) {
this.getEachTypesOfPosts(_options, [
'lecture',
'competition'
]);
},
toggleFav(_options) {
let _type = _options.id.slice(0, _options.id.indexOf('_'));
let _map = this.globalData[_type + 's'];
let _copy = _map.get(_options.id);
_copy.favorited = !(_copy.favorited);
_map.set(_options.id, _copy);
wx.setStorage({
key: 'favorited_' + _type + '_id',
data: JSON.stringify(
mapToArray(_map).map(_post => [_post.id, _post.favorited])
),
success: _options.success
});
},
onLaunch() {
wx.setNavigationBarTitle({title: 'Clawjtu'});
}
});