-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
263 lines (252 loc) · 8.5 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//app.js
const RSA = require("utils/rsa/wx_rsa.js");
const config = require("config.js");
const login = require("/utils/login/login");
const log = require("utils/log");
import {getUserInfo} from './api'
App({
onLaunch: function (e) {
console.log("APP onLaunch", e);
// 初始化数据
this.initData();
// =========版本升级检测===============
this.checkUpdate();
this.loginClass = new login.DoLogin(this.globalData);
this.globalData.autoLoginProcess = Promise.resolve()
this.getUserInfo().then((res) => {
if("string" != typeof res.openid){
wx.showToast({
icon: 'none',
title: '用户数据获取异常,请重启程序'
})
}
this.globalData.openid = res.openid;
this.globalData.accountInfo.isAdmin = res.isAdmin;
if (this.globalData.accountInfo.isAutoLogin){
wx.showLoading({title: '自动登录中~'})
this.globalData.autoLoginProcess = this.loginClass.autoLogin();
}else{
this.globalData.autoLoginProcess = Promise.resolve()
console.log("未启用自动登录");
}
}).catch(e=>{
});
},
// 小程序发生脚本错误或 API 调用报错时触发。
onError: function (e) {
console.error('onError', e);
log.info("onError" + JSON.stringify(e));
},
onUnhandledRejection: function (e) {
log.info("onUnhandledRejection" + JSON.stringify(e));
console.error('onUnhandledRejection', e);
if("string" == typeof e.errMsg){
wx.showToast({
icon: "none",
title: e.errMsg,
});
}
},
// 初始化数据
initData: function () {
wx.getSystemInfo({
success: (res) => {
this.globalData.windowHeight = res.windowHeight;
this.globalData.windowWidth = res.windowWidth;
},
fail(err) {
console.log(err);
},
});
let ai = wx.getStorageSync("accountInfo");
if (ai) this.globalData.accountInfo = ai;
wx.getStorage({
key: "isADClose",
success: (res) => {
this.globalData.isADClose = res.data;
},
});
if (this.globalData.accountInfo.isAutoLogin) {
this.globalData.isUser = wx.getStorageSync("isUser");
this.globalData.sessionInfo.JSZX_cookie = wx.getStorageSync(
"JSZX_cookie"
);
this.globalData.sessionInfo.JWGL_cookie = wx.getStorageSync(
"JWGL_cookie"
);
this.globalData.sessionInfo.theolCookie = wx.getStorageSync(
"theolCookie"
);
this.globalData.sessionInfo.SSO_SESSION = wx.getStorageSync(
"SSO_SESSION"
);
this.globalData.sessionInfo.SSO_TGC = wx.getStorageSync("SSO_TGC");
this.globalData.sessionInfo.TWFID = wx.getStorageSync("TWFID");
} else {
wx.getStorage({
key: "isUser",
success: (res) => {
this.globalData.isUser = res.data;
},
});
wx.getStorage({
key: "JSZX_cookie",
success: (res) => {
this.globalData.sessionInfo.JSZX_cookie = res.data;
},
});
wx.getStorage({
key: "JWGL_cookie",
success: (res) => {
this.globalData.sessionInfo.JWGL_cookie = res.data;
},
});
wx.getStorage({
key: "theolCookie",
success: (res) => {
this.globalData.sessionInfo.theolCookie = res.data;
},
});
wx.getStorage({
key: "SSO_SESSION",
success: (res) => {
this.globalData.sessionInfo.SSO_SESSION = res.data;
},
});
wx.getStorage({
key: "SSO_TGC",
success: (res) => {
this.globalData.sessionInfo.SSO_TGC = res.data;
},
});
wx.getStorage({
key: "TWFID",
success: (res) => {
this.globalData.sessionInfo.TWFID = res.data;
},
});
}
// 课程表数据
this.globalData.start = wx.getStorageSync("start");
this.globalData.classtable = wx.getStorageSync("classtable");
this.globalData.location = wx.getStorageSync("location");
// 初始化数据end
},
/**
* 获取用户属性
*
* isAdmin, openid
*/
getUserInfo: function () {
return new Promise((resolve, reject) => {
wx.getStorage({
key: "UserInfo",
success: (res) => {
// 本地有存储信息就直接用
if (res.data) {
/**
* {
* isAdmin: boolean,
* openid: *****
* }
*/
resolve(res.data);
}
},
fail: (err) => {
console.error(err);
// 本地信息获取失败,远程获取
this.login().then((code) => {
getUserInfo(code).then((data)=>{
if ("object" == typeof data) {
wx.setStorage({
key: "UserInfo",
data: data.info,
});
/**
* {
* isAdmin: boolean,
* openid: *****
* }
*/
resolve(data.info);
} else {
reject(data);
}
})
});
},
});
});
},
// =========版本升级检测===============
checkUpdate: function () {
const updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
console.log(res.hasUpdate);
});
updateManager.onUpdateReady(function () {
wx.showModal({
title: "更新提示",
content: "新版本已经准备好,是否重启应用?",
success(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
},
});
});
},
login: function () {
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
if (res.code) resolve(res.code);
else reject(res);
},
fail: (err) => {
reject(err);
},
});
});
},
RSAEncrypt: function (unencrypt) {
let publicKey = config.RSAPubKey;
var encrypt_rsa = new RSA.RSAKey();
encrypt_rsa = RSA.KEYUTIL.getKey(publicKey);
let encStr = encrypt_rsa.encryptLong(unencrypt);
let encrypted = RSA.hex2b64(encStr);
return encrypted;
},
globalData: {
API_DOMAIN: config.API_DOMAIN,
start: null,
classtable: null,
checkInList: null,
location: null,
openid: "",
accountInfo: {
isAdmin: false,
userId: "",
userPass: "",
vpnPass: "",
isRemPass: false,
isAutoLogin: false,
},
isUser: false,
sessionInfo: {
JWGL_cookie: "",
SSO_TGC: "",
JSZX_cookie: "",
theolCookie: "",
},
config: {
github: "",
group: "",
},
isADClose: "undefined" !== typeof qq ? false : true,
autoLoginProcess: null,
},
});