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 { 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