reserve/routers/index.js
2023-09-24 14:36:14 +08:00

191 lines
5.2 KiB
JavaScript

const express = require('express')
const router = express.Router()
const fs = require('fs')
const data = require('../data')
const Lock = require('async-lock');
const lock = new Lock();
router.get('/', (req, res) => {
res.render('index', {})
})
router.get('/info', (req, res) => {
res.render('info', {})
})
router.get('/admin', (req, res) => {
res.render('admin', {})
})
router.get('/get_reserve_info', (req, res) => {
json = data.get_data()
res.write(JSON.stringify(json['current_week']))
res.end()
})
router.post('/clear', (req, res)=> {
lock.acquire('sharedVariable', (done) => {
result = {
code: 0,
message: ""
}
if(req.body.password != "wx6MKPBy") {
result.code = -1
result.message = "错误的管理密码!"
} else {
data.clear()
json = data.get_data()
result.code = 0;
result.message = `成功清除所有预约!\n干事预约密码: ${json.ganshi}\n乐队预约密码: ${json.yuedui}\n 社员预约密码: ${json.sheyuan}`
}
res.json(result).end()
done();
});
})
router.post('/unreserve', (req, res)=> {
lock.acquire('sharedVariable', (done) => {
result = {
code: 0,
message: ""
}
let day = req.body.day;
let time = req.body.time;
let errors = [];
if (!day || day < 1 || day > 7) {
errors.push('日期应该是1到7之间的数字');
}
if (!time || time < 1 || time > 6) {
errors.push('时间应该是1到6之间的数字');
}
if(errors.length != 0) {
error_message = ""
for(let i=0; i<errors.length; i++) {
error_message += (errors[i] + '\n')
}
result.code = -1;
result.message = error_message
} else {
if(req.body.password != "wx6MKPBy") {
result.code = -1
result.message = "错误的管理密码!"
} else {
result.code = 0;
result.message = `取消预约成功!\n`
write_data = data.get_data()
write_data['current_week'][`${day}-${time}`].name = "";
write_data['current_week'][`${day}-${time}`].phone = "";
write_data['current_week'][`${day}-${time}`].type = "";
fs.writeFileSync("./data/current.json", JSON.stringify(write_data))
}
}
res.json(result).end()
done();
});
})
router.post('/reserve', (req, res) => {
lock.acquire('sharedVariable', (done) => {
let day = req.body.day;
let time = req.body.time;
let password = req.body.password;
let name = req.body.name;
let phone = req.body.phone;
let errors = [];
if (!day || day < 1 || day > 7) {
errors.push('日期应该是1到7之间的数字');
}
console.log(req.body)
if (!time || time < 1 || time > 6) {
errors.push('时间应该是1到6之间的数字');
}
if (!password || password.trim().length === 0) {
errors.push('密码不能为空');
}
if (!name || name.trim().length === 0) {
errors.push('姓名不能为空');
}
if (!phone || phone.length !== 11 || !/^\d+$/.test(phone)) {
errors.push('手机号码应该是11位数字');
}
result = {
code: 0,
message: ""
}
if(errors.length != 0) {
error_message = ""
for(let i=0; i<errors.length; i++) {
error_message += (errors[i] + '\n')
}
result.code = -1;
result.message = error_message
} else {
let dt = data.get_data()
let json = data.get_data()['current_week'][`${day}-${time}`]
let type = ""
if(password == dt.ganshi) {
type = "干事"
}
if(password == dt.yuedui) {
type = "乐队"
}
if(password == dt.sheyuan) {
type = "社员"
}
if(json.name == "") {
if(type) {
result.code = 0;
result.message = `预约成功\n预约人: ${name}\n联系方式: ${phone}\n身份: ${type}`
write_data = data.get_data()
write_data['current_week'][`${day}-${time}`].name = name;
write_data['current_week'][`${day}-${time}`].phone = phone;
write_data['current_week'][`${day}-${time}`].type = type;
fs.writeFileSync("./data/current.json", JSON.stringify(write_data))
} else {
result.code = -1;
result.message = "预约密码错误"
}
} else {
result.code = -1;
result.message = "该时间段已经被预约"
}
}
res.json(result).end()
done();
});
});
module.exports = router