forked from Benjamin-Fairy/scu-duomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
356 lines (344 loc) · 10.2 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
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
var e = require("./utils/util.js");
App({
globalData: {
url: "https://duomi.chenyipeng.com/pennisetum/",
// url: "http://localhost:8001/pennisetum/",
jwurl: 'https://duomi.chenyipeng.com/scu/',
// https://duomi.chenyipeng.com/scu/
jwclogin: false,
user: null,
ticket: null,
unReadCount: 0,
chatMessageList: [],
limit: 0,
JSESSIONID_SCU: null,
completeLogin: false,
callback: function () { },
jscode: null,
xf: false
},
jwcLogin: function () {
let _this = this;
let a = this;
if (wx.getStorageSync("scu_jwc_username") && wx.getStorageSync("scu_jwc_password")) {
var t = function (t, a) {
wx.request({
url: a.globalData.jwurl,
header: {
cookie: a.globalData.JSESSIONID_SCU,
"content-type": "application/x-www-form-urlencoded"
},
success: res => {
let startIndex = res.data.indexOf('tokenValue')
let token = res.data.substring(startIndex + 37, startIndex + 69)
wx.request({
url: _this.globalData.jwurl + "j_spring_security_check",
header: {
cookie: a.globalData.JSESSIONID_SCU,
"content-type": "application/x-www-form-urlencoded"
},
method: "POST",
data: {
j_username: wx.getStorageSync("scu_jwc_username"),
j_password: (0, e.md5)(wx.getStorageSync("scu_jwc_password")).toLocaleLowerCase(),
j_captcha: t,
tokenValue: token
},
success: function (e) {
if (e.statusCode == 200) {
wx.hideLoading()
if (e.data.indexOf("验证码错误") == -1 && e.data.indexOf("用户密码错误") == -1) {
a.globalData.jwclogin = true
}
}
},
fail: function (e) {
console.log(e);
}
});
}
})
}
wx.request({
url: _this.globalData.jwurl + "img/captcha.jpg",
responseType: "arraybuffer",
header: {
cookie: this.globalData.JSESSIONID_SCU
},
success: function (e) {
! function (e, a) {
wx.request({
url: "https://duomi.chenyipeng.com/captcha",
// url: 'http://localhost:5005/captcha',
method: "POST",
header: {
"content-type": "application/x-www-form-urlencoded"
},
data: {
type: 0,
base64img: e
},
success: function (e) {
console.log(e.data)
if (200 == e.data.code) {
var o = e.data.captcha;
t(o, a);
}
}
});
}("data:image/png;base64," + wx.arrayBufferToBase64(e.data), a);
}
});
}
},
onLaunch: function () {
var _this = this;
wx.getSystemInfo({
success: e => {
this.globalData.StatusBar = e.statusBarHeight;
let capsule = wx.getMenuButtonBoundingClientRect();
this.globalData.safeBottom = e.screenHeight - e.safeArea.bottom
if (capsule) {
this.globalData.Custom = capsule;
this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
} else {
this.globalData.CustomBar = e.statusBarHeight + 50;
}
}
})
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager()
//1. 检查小程序是否有新版本发布
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
if (res.hasUpdate) {
//检测到新版本,需要更新,给出提示
wx.showModal({
title: '更新提示',
content: '检测到新版本,请更新后使用?',
success: function (res) {
if (res.confirm) {
//2. 用户确定下载更新小程序,小程序下载及更新静默进行
self.downLoadAndUpdate(updateManager)
} else if (res.cancel) {
//用户点击取消按钮的处理,如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了
wx.showModal({
title: '温馨提示~',
content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
showCancel: false, //隐藏取消按钮
confirmText: "确定更新", //只保留确定更新按钮
success: function (res) {
if (res.confirm) {
//下载新版本,并重新应用
self.downLoadAndUpdate(updateManager)
}
}
})
}
}
})
}
})
}
wx.request({
url: _this.globalData.url + "config",
success: res => {
for (var a in res.data.data) this.globalData[a] = res.data.data[a];
this.getSession(function () {
_this.jwcLogin();
})
this.Login();
},
})
},
onShow: function (info) {
if (info.scene == '1038' && info.referrerInfo.appId == 'wx2f80896fbecfdaa6' && info.referrerInfo.extraData) {
if (info.referrerInfo.extraData.type == 1) {
this.globalData.dxxcode = info.referrerInfo.extraData.code
}
}
if (info.scene == '1037' && info.referrerInfo.appId == 'wx2f80896fbecfdaa6' && info.referrerInfo.extraData) {
this.globalData.jscode = info.referrerInfo.extraData.jscode
}
},
onError: function (e) {
console.log(e);
},
onHide: function () { },
Login: function () {
var e = this;
wx.login({
fail: function (e) {
console.log(e);
},
success: function (t) {
wx.request({
url: e.globalData.url + "user/login/duomi",
method: "post",
data: {
JSCode: t.code
},
header: {
"content-type": "application/x-www-form-urlencoded;charset=utf-8"
},
success: function (t) {
console.log(t.data)
e.globalData.completeLogin = true
wx.setStorageSync("user", t.data.user)
e.globalData.user = t.data.user
e.globalData.ticket = t.data.ticket
if (e.loginReadyCallBack) {
e.loginReadyCallBack(t)
}
if (e.loginCallback) {
e.loginCallback(t)
}
},
fail: function (t) {
e.Login();
}
});
}
});
},
userRegister: function () {
if (this.globalData.ticket && this.globalData.user && this.globalData.user.uid != 0) {
return true
} else {
wx.showToast({
title: '需要完成注册环节',
icon: 'error'
})
setTimeout(() => {
wx.navigateTo({
url: '/pages/register/register',
})
}, 500)
return false
}
},
request: function (e) {
var t = {
method: "GET",
data: {},
url: "#",
faile: function (e) { },
};
for (var a in e) t[a] = e[a];
t['success'] = function (res) {
if (res.data.code == 400 || res.data.code == 401) {
wx.showToast({
title: '权限不足!',
icon: 'error'
})
return
}
if (res.data.code == 490) {
wx.showToast({
title: res.data.msg,
icon: 'none'
})
return
}
e['success'] && e['success'](res)
}
t['complete'] = function (res) {
if (res.data.code == 400 || res.data.code == 401) {
wx.showToast({
title: '权限不足!',
icon: 'error'
})
return
}
if (res.data.code == 490) {
wx.showToast({
title: res.data.msg,
icon: 'none'
})
return
}
e['complete'] && e['complete'](res)
}
t.header ? (t.header.pennisetum = this.globalData.ticket, t.header["content-type"] = "application/x-www-form-urlencoded;charset=utf-8") : t.header = {
pennisetum: this.globalData.ticket,
"content-type": "application/x-www-form-urlencoded;charset=utf-8"
}, wx.request({
url: t.url,
data: t.data,
type: t.method,
method: t.method,
header: t.header,
complete: t.complete,
success: t.success,
fail: t.faile
});
},
getSession: function (e) {
let _this = this;
e || (e = function () { });
var t = this;
wx.request({
url: _this.globalData.jwurl + "login",
header: {
cookie: t.globalData.JSESSIONID_SCU
},
success: function (a) {
var o = a.header["Set-Cookie"];
if (o) {
let sessionId = o.split(';')[0]
t.globalData.JSESSIONID_SCU = sessionId
t.globalData.JSESSIONID = sessionId.split('=')[1]
t.globalData.jwclogin = false
}
e();
},
fail: function (e) {
console.log('获取session失败')
console.log(e);
wx.showModal({
cancelColor: 'cancelColor',
content: JSON.stringify(e)
})
}
});
},
checkSCULogin: function () {
wx.showModal({
title: '提示',
content: '该功能需要完成登录才能使用(服务端将不会保存您的密码,仅保存于本地端)',
complete: (res) => {
if (res.cancel) {
wx.showToast({
title: '未登录将无法使用!',
icon: 'none'
})
}
if (res.confirm) {
wx.navigateTo({
url: "/pages/home/login/login"
});
}
}
})
},
requestPromise: function (e, t) {
var a = this,
o = this;
return new Promise(function (n, c, s) {
o.request({
url: e,
data: t,
header: {
"content-type": "application/x-www-form-urlencoded",
Cookie: "JSESSIONID=" + a.globalData.ticket
},
success: function (e) {
n(e);
},
fail: function (e) {
c(e);
}
});
});
}
});