-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
103 lines (82 loc) · 3.06 KB
/
index.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
// Global Login Button
var loginText = "Login";
var loginLink = "login/login.html";
function setLogin(callback) {
// Judge if login or myreview
var param = new URL(location)
if (param.searchParams.has("token")) {
localStorage.setItem("token", param.searchParams.get("token"))
if (new URL(document.referrer).pathname.endsWith("EmailConfirm"))
layui.use("layer", function () {
layui.layer.msg("Email Confirmed Successfully");
})
else if (param.get("pr") == 1) {
layui.use("layer", function () {
layui.layer.msg("Password Reset Successfully");
})
}
}
var token = localStorage.getItem("token")
if (token) {
console.log("检测到token")
try {
var user = JSON.parse(b64_to_utf8(token.split(".")[1]))
if (user.exp > Date.now() / 1000) {
console.log("token未过期, 已登录")
loginText = "My Review";
loginLink = "myreview/myreview.html";
} else {
console.log("token已过期, 请重新登录")
}
} catch (err) {
localStorage.removeItem("token")
console.log("token 无效")
console.log(err)
}
} else {
console.log("未检测到token, 请登录")
}
callback()
}
window.onload = function () {
// Load options of select
layui.use(['layer', 'jquery', 'form'], async function () {
var $ = layui.jquery;
await loadInfo;
teachers.sort((x, y) => (x.chineseName + x.englishName).localeCompare(y.chineseName + y.englishName)).forEach(teacher =>
$("#search").append(new Option([teacher.chineseName, teacher.englishName].join(" ").trim(), `1-${teacher.id}`))
)
Courses.sort((x, y) => x.courseCode.localeCompare(y.courseCode)).forEach(i => {
$("#search").append(new Option(i.courseName + " " + i.courseCode, "2-" + i.courseCode));
})
layui.form.render('select');
await waitInitial;
loadLayer();
})
//Load login button
setLogin(function () {
let loginDiv = document.getElementById("login-div");
let login = document.createElement("span");
login.setAttribute('href', loginLink);
login.innerHTML = ` <button class = "add-review" onclick="${loginText=="Login"?"toLogin()":"location='myreview/myreview.html'"}">
<text class = "add-review-text"> ` + loginText + ` </text> </button>`;
loginDiv.appendChild(login);
})
}
// Set submit button click
layui.use(['form', 'jquery'], function () {
var form = layui.form;
var $ = layui.$;
$(document).keydown(function (e) {
if (e.keyCode === 13) {
$("#submit").trigger("click");
return false;
}
});
form.on('submit(submit)', function (data) {
query = data.field.teacher;
link = "menu/menu.html?query=" + encodeURI(encodeURI(query)) + "";
window.location.href = link;
return false;
});
})