-
Notifications
You must be signed in to change notification settings - Fork 263
/
app.js
192 lines (191 loc) · 5.7 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//app.js
App({
version: 'v0.1.2', //版本号
onLaunch: function() {
var _this = this;
//读取缓存
try {
var data = wx.getStorageInfoSync();
if (data && data.keys.length) {
data.keys.forEach(function(key) {
var value = wx.getStorageSync(key);
if (value) {
_this.cache[key] = value;
}
});
if (_this.cache.version !== _this.version) {
_this.cache = {};
wx.clearStorage();
} else {
_this._user.wx = _this.cache.userinfo.userInfo || {};
_this.processData(_this.cache.userdata);
}
}
} catch(e) { console.warn('获取缓存失败'); }
},
//保存缓存
saveCache: function(key, value) {
if(!key || !value){return;}
var _this = this;
_this.cache[key] = value;
wx.setStorage({
key: key,
data: value
});
},
//清除缓存
removeCache: function(key) {
if(!key){return;}
var _this = this;
_this.cache[key] = '';
wx.removeStorage({
key: key
});
},
//后台切换至前台时
onShow: function(){
},
//判断是否有登录信息,让分享时自动登录
loginLoad: function(onLoad){
var _this = this;
if(!_this._t){ //无登录信息
_this.getUser(function(e){
typeof onLoad == "function" && onLoad(e);
});
}else{ //有登录信息
typeof onLoad == "function" && onLoad();
}
},
//getUser函数,在index中调用
getUser: function(response) {
var _this = this;
wx.showNavigationBarLoading();
wx.login({
success: function(res){
if(res.code){
//调用函数获取微信用户信息
_this.getUserInfo(function(info){
_this.saveCache('userinfo', info);
_this._user.wx = info.userInfo;
if(!info.encryptedData || !info.iv){
_this.g_status = '无关联AppID';
typeof response == "function" && response(_this.g_status);
return;
}
//发送code与微信用户信息,获取学生数据
wx.request({
method: 'POST',
url: _this._server + '/api/users/get_info.php',
data: {
code: res.code,
key: info.encryptedData,
iv: info.iv
},
success: function(res){
if(res.data && res.data.status >= 200 && res.data.status < 400){
var status = false, data = res.data.data;
//判断缓存是否有更新
if(_this.cache.version !== _this.version || _this.cache.userdata !== data){
_this.saveCache('version', _this.version);
_this.saveCache('userdata', data);
_this.processData(data);
status = true;
}
if(!_this._user.is_bind){
wx.navigateTo({
url: '/pages/more/login'
});
}
//如果缓存有更新,则执行回调函数
if(status){
typeof response == "function" && response();
}
}else{
//清除缓存
if(_this.cache){
_this.cache = {};
wx.clearStorage();
}
typeof response == "function" && response(res.data.message || '加载失败');
}
},
fail: function(res){
var status = '';
// 判断是否有缓存
if(_this.cache.version === _this.version){
status = '离线缓存模式';
}else{
status = '网络错误';
}
_this.g_status = status;
typeof response == "function" && response(status);
console.warn(status);
},
complete: function(){
wx.hideNavigationBarLoading();
}
});
});
}
}
});
},
processData: function(key){
var _this = this;
var data = JSON.parse(_this.util.base64.decode(key));
_this._user.is_bind = data.is_bind;
_this._user.openid = data.user.openid;
_this._user.teacher = (data.user.type == '教职工');
_this._user.we = data.user;
_this._time = data.time;
_this._t = data['\x74\x6f\x6b\x65\x6e'];
return data;
},
getUserInfo: function(cb){
var _this = this;
//获取微信用户信息
wx.getUserInfo({
success: function(res){
typeof cb == "function" && cb(res);
},
fail: function(res){
_this.showErrorModal('拒绝授权将导致无法关联学校帐号并影响使用,请重新打开We重邮再点击允许授权!', '授权失败');
_this.g_status = '未授权';
}
});
},
//完善信息
appendInfo: function(data){
var _this = this;
_this.cache = {};
wx.clearStorage();
_this._user.we.build = data.build || '';
_this._user.we.room = data.room || '';
},
showErrorModal: function(content, title){
wx.showModal({
title: title || '加载失败',
content: content || '未知错误',
showCancel: false
});
},
showLoadToast: function(title, duration){
wx.showToast({
title: title || '加载中',
icon: 'loading',
mask: true,
duration: duration || 10000
});
},
util: require('./utils/util'),
key: function(data){ return this.util.key(data) },
cache: {},
_server: 'https://we.cqu.pt',
_user: {
//微信数据
wx: {},
//学生\老师数据
we: {}
},
_time: {} //当前学期周数
});