forked from danbaixi/yunxiaozhi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
365 lines (349 loc) · 9.57 KB
/
app.js
File metadata and controls
365 lines (349 loc) · 9.57 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
const {
acceptTerms,
checkCourseInWeek
} = require('./utils/common')
const {
exitSaveData
} = require('./utils/util')
App({
/** 小程序入口 */
onLaunch: function () {
//设置导航栏数据
this.setNavgition()
//更新配置
this.updateConfigRequest()
this.setFileUrl()
//检查更新
this.checkVersion()
//test
// console.log(checkCourseInWeek(10,{course_weekly:'1-5,7,10-14',course_danshuang:1}))
},
/** 全局变量 */
globalData: {
isDebug: false,
isTest: false,
isLocal: false,
themeColor: '#1380ff',
xdebug: "?XDEBUG_SESSION_START=14473",
domain: 'https://www.yunxiaozhi.cn/v1/public/api/',
key: 'ihzoaixnuy4f8835032505e8a45ac102c52d58593e',
markers_json: "markers.json",
adTime: 24, //小时出现一次
},
updateConfigRequest: function () {
let time = parseInt((new Date()).getTime() / 1000)
this.promiseRequest({
url: 'config/getMiniConfig',
needLogin: false,
data: {
stu_id: wx.getStorageSync('user_id') || ''
}
}).then((data) => {
if (data.status == 0) {
wx.setStorageSync('config_update_time', time)
wx.setStorageSync('configs', data.data)
acceptTerms()
} else {
console.log('get config error')
}
}).catch((error) => {
console.log(error)
})
},
//设置存储文件地址
setFileUrl: function () {
let url = this.getConfig('fileUrl')
if (url) {
this.globalData.fileDomain = url + '/mini/'
this.globalData.fileUrl = url + '/mini/'
this.globalData.headImgUrl = url + '/user_imgs/'
} else {
this.globalData.fileDomain = 'http://file.yunxiaozhi.cn/mini/'
this.globalData.fileUrl = 'http://file.yunxiaozhi.cn/mini'
this.globalData.headImgUrl = 'http://file.yunxiaozhi.cn/user_imgs/'
}
},
//设置导航栏数据
setNavgition: function () {
wx.getSystemInfo({
success: e => {
this.globalData.statusBar = e.statusBarHeight;
let capsule = wx.getMenuButtonBoundingClientRect();
if (capsule) {
this.globalData.custom = capsule;
this.globalData.customBar = capsule.bottom + capsule.top - e.statusBarHeight;
} else {
this.globalData.customBar = e.statusBarHeight + 50;
}
}
})
},
//检查版本更新
checkVersion: function () {
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {})
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,请重启应用',
success: function (res) {
if (res.confirm) {
updateManager.applyUpdate()
}
}
})
})
},
//获取请求domain
getDomain: function () {
if (this.globalData.isDebug) {
return this.globalData.isTest ? 'https://www.yunxiaozhi.cn/test/public/api/' : 'http://danbaixi.cn.utools.club/yxz_v1/public/index.php/api/'
}
return 'https://www.yunxiaozhi.cn/v1/public/api/'
},
//封装request
httpRequest: function (datas) {
datas.data = datas.data == undefined ? {} : datas.data
datas.redirect = datas.redirect || ''
if (datas.needLogin == undefined || datas.needLogin == true) {
let session = this.getLoginStatus()
if (session == "") {
this.msg('请先登录')
return
}
datas.data.session = session
}
var that = this,
isDebug = this.globalData.isDebug,
domain = this.getDomain(),
contentType = 'application/json'
if (typeof datas.method == undefined) {
datas.method = "GET"
}
if (datas.method == "POST") {
contentType = 'application/x-www-form-urlencoded'
}
wx.request({
url: domain + datas.url + (isDebug ? this.globalData.xdebug : ""),
data: typeof datas.data == undefined ? '' : datas.data,
method: datas.method,
header: {
'content-type': contentType
},
success: function (res) {
switch (res.data.status) {
default:
datas.success(res);
break;
case 4001:
that.msg('请先登录');
break;
case 4002:
that.msg('请先绑定账号');
break;
case 4003:
//登陆已过期
that.msg(res.data.message)
exitSaveData()
setTimeout(() => {
wx.navigateTo({
url: '/pages/login/login?redirect=' + datas.redirect,
})
}, 1000)
break;
case 4004:
that.msg('您已修改了教务系统密码,请重新绑定账号');
setTimeout(() => {
wx.navigateTo({
url: '/pages/bind/bind?rebind=true',
})
}, 1000);
break;
}
},
fail: function (res) {
wx.showToast({
title: '请求超时',
icon: 'none',
duration: 2000
})
}
})
},
//toast
msg: function (text, type) {
var type = typeof type === "undefined" ? '' : type
var icon = 'none'
if (type != '') {
icon = type
}
wx.showToast({
title: text,
icon: icon,
duration: 2000
})
},
//分享
share: function (title, img, path) {
if (typeof title == "undefined") {
return {
title: "课表成绩考勤校历都在这里",
imageUrl: this.globalData.fileDomain + 'share/v1/index.png',
path: 'pages/index/index'
}
}
path = typeof path === "undefined" ? 'pages/index/index' : path
if (!img) {
return {
title,
path
}
}
return {
title,
path,
imageUrl: this.globalData.fileDomain + 'share/v1/' + img,
}
},
//强制需要登录
isLogin: function (route) {
let _this = this
var route = route || ''
return new Promise((resolve) => {
let session = wx.getStorageSync('login_session')
if (session == '') {
_this.msg('请先登录')
setTimeout(() => {
wx.reLaunch({
url: '/pages/login/login?redirect=' + route,
})
}, 1000);
return resolve(false)
}
return resolve(true)
})
},
//获取登录状态
getLoginStatus: function () {
let session = wx.getStorageSync('login_session')
return session == '' ? false : session
},
//获取绑定状态
getUserId: function () {
let user_id = wx.getStorageSync('user_id')
return user_id == '' ? false : user_id
},
//登录or绑定提示
isBind: function () {
let _this = this
return new Promise((resolve) => {
let session = wx.getStorageSync('login_session')
if (session == '') {
_this.msg('请先登录')
return resolve(false)
}
let user_id = wx.getStorageSync('user_id')
if (user_id == '') {
_this.msg('请先绑定教务系统账号')
return resolve(false)
}
return resolve(true)
})
},
//promise封装请求
promiseRequest: function (datas) {
datas.data = datas.data == undefined ? {} : datas.data
datas.redirect = datas.redirect || ''
if (datas.needLogin == undefined || datas.needLogin == true) {
let session = this.getLoginStatus()
if (session == "") {
this.msg('请先登录')
return
}
datas.data.session = session
}
var that = this,
isDebug = this.globalData.isDebug,
domain = this.getDomain(),
contentType = 'application/json'
if (typeof datas.method == undefined) {
datas.method = "GET"
}
if (datas.method == "POST") {
contentType = 'application/x-www-form-urlencoded'
}
let promise = new Promise((resolve, reject) => {
wx.request({
url: domain + datas.url + (isDebug ? this.globalData.xdebug : ""),
data: typeof datas.data == undefined ? '' : datas.data,
method: datas.method,
header: {
'content-type': contentType
},
success: function (res) {
wx.hideLoading()
if (res.data.status == 0) {
resolve(res.data)
} else if (res.data.status == 4003) {
//登陆已过期
that.msg(res.data.message)
exitSaveData()
setTimeout(() => {
wx.navigateTo({
url: '/pages/login/login?redirect=' + datas.redirect,
})
}, 1000)
} else {
reject(res.data.message || '服务器开小差了 ╯﹏╰')
}
},
fail: function (res) {
wx.showToast({
title: '请求超时,请重试',
icon: 'none',
duration: 2000
})
}
})
})
return promise
},
//获取配置,支持使用“.”
//key为空,返回全部
getConfig: function (key) {
let configs = wx.getStorageSync('configs')
if (key) {
let keyArr = key.split('.')
let result = ""
if (configs.hasOwnProperty(keyArr[0])) {
result = configs[keyArr[0]]
}
if (keyArr.length == 1) {
return result
}
for (let i = 1; i < keyArr.length; i++) {
if (result.hasOwnProperty(keyArr[i])) {
result = result[keyArr[i]]
} else {
return false
}
}
return result
}
return configs
},
back(delay = 1500, delta = 1) {
if (delay > 0) {
setTimeout(() => {
wx.navigateBack({
delta,
fail() {
wx.switchTab({
url: '/pages/index',
})
}
})
}, delay);
}
}
})