完成大作业服务端代码,新建管理端,管理端不可用
This commit is contained in:
42
Project/Manager/static/js/index.js
Normal file
42
Project/Manager/static/js/index.js
Normal file
@@ -0,0 +1,42 @@
|
||||
function validateForm() {
|
||||
var departure = document.getElementById('departure').value;
|
||||
var destination = document.getElementById('destination').value;
|
||||
var warning = document.getElementById('destination-warning');
|
||||
if (departure === destination) {
|
||||
warning.textContent = '出发地和目的地不能相同';
|
||||
return false;
|
||||
} else {
|
||||
warning.textContent = '';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function increment() {
|
||||
var passengers = document.getElementById("passengers");
|
||||
var value = parseInt(passengers.value, 10);
|
||||
if (value < 50) {
|
||||
passengers.value = value + 1;
|
||||
}
|
||||
}
|
||||
|
||||
function decrement() {
|
||||
var passengers = document.getElementById("passengers");
|
||||
var value = parseInt(passengers.value, 10);
|
||||
if (value > 1) {
|
||||
passengers.value = value - 1;
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// Set default date to tomorrow
|
||||
var departureDate = document.getElementById('departure-date');
|
||||
if (!departureDate.value) {
|
||||
var today = new Date();
|
||||
var tomorrow = new Date(today);
|
||||
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||
var month = ('0' + (tomorrow.getMonth() + 1)).slice(-2);
|
||||
var day = ('0' + tomorrow.getDate()).slice(-2);
|
||||
var year = tomorrow.getFullYear();
|
||||
departureDate.value = `${year}-${month}-${day}`;
|
||||
}
|
||||
});
|
||||
80
Project/Manager/static/js/login.js
Normal file
80
Project/Manager/static/js/login.js
Normal file
@@ -0,0 +1,80 @@
|
||||
window.onload = function() {
|
||||
autoLogin();
|
||||
};
|
||||
|
||||
var checkInfo = {};
|
||||
|
||||
checkInfo.checkUsername = function() {
|
||||
let username = document.getElementById('username').value;
|
||||
if (username.length < 1) {
|
||||
document.getElementById('usernameError').textContent = '用户名不能为空';
|
||||
return false;
|
||||
}
|
||||
document.getElementById('usernameError').textContent = '';
|
||||
return true;
|
||||
}
|
||||
|
||||
checkInfo.checkPassword = function() {
|
||||
let password = document.getElementById('password').value;
|
||||
let regexPassword = /^[A-Za-z0-9\W_]{6,20}$/;
|
||||
if (!regexPassword.test(password)) {
|
||||
document.getElementById('loginError').textContent = "密码须为长度为6-20位字母、数字或符号";
|
||||
return false;
|
||||
}
|
||||
document.getElementById('loginError').textContent = '';
|
||||
return true;
|
||||
}
|
||||
|
||||
function submitForm() {
|
||||
if (checkInfo.checkUsername() && checkInfo.checkPassword()) {
|
||||
document.getElementById('encryptedPassword').value = md5(
|
||||
document.getElementById('password').value
|
||||
);
|
||||
login();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 登录函数
|
||||
async function login() {
|
||||
const username = document.getElementById('username').value;
|
||||
const encryptedPassword = document.getElementById('encryptedPassword').value;
|
||||
try {
|
||||
const response = await fetch('/login', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ username: username, password: encryptedPassword }),
|
||||
credentials: 'include' // 确保请求包含凭据(cookies)
|
||||
});
|
||||
const data = await response.json();
|
||||
if (response.ok) {
|
||||
alert('登录成功');
|
||||
// 自动跳转到主页
|
||||
window.location.href = data.redirect;
|
||||
} else {
|
||||
document.getElementById('loginError').textContent = data.message;
|
||||
}
|
||||
} catch (error) {
|
||||
alert('数据库错误,请稍后再试');
|
||||
}
|
||||
}
|
||||
|
||||
// 自动登录函数
|
||||
async function autoLogin() {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
const response = await fetch('/index', {
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + token
|
||||
}
|
||||
});
|
||||
if (response.ok) {
|
||||
document.getElementById('content').innerText = '已自动登录';
|
||||
} else {
|
||||
document.getElementById('content').innerText = '自动登录失败';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user