-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
175 lines (150 loc) · 4.44 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
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
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const Menu = require('./menu.js')
const emailValidator = require('email-validator')
const jsonfile = require('jsonfile')
const fs = require('fs')
app.use(express.static('public'))
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// cookie
app.use(cookieParser())
app.get('/hello', function (req, res) {
// res.send('Hello World!')
res.redirect('/?message=Hello World!')
})
app.post('/post_order', function (req, res) {
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
const email = req.body.email || req.cookies.email;
if (!emailValidator.validate(email)) {
// res.write('Email 格式错误!');
// res.end();
res.redirect('/?message=1&type= alert-danger')
return;
}
if (!email.endsWith('@bolorobo.com') && !email.endsWith('@belledu.com')){
// res.write('哪儿来的的奸细!');
// res.end();
res.redirect('/?message=2&type= alert-danger')
return;
}
// set email to cookie
res.cookie('email', email, { maxAge: 30 * 24 * 60 * 60 * 1000, httpOnly: false });
const menus = new Menu().getAll();
const fastfood = menus[0];
const curedfood = [];//menus[1];
const orders = [];
const extra = req.body.extra;
for (var key in req.body) {
if (key === 'email') {
continue
} else if (key.endsWith('_price')) {
// 更新浮动菜品的价格
for (var i = 0; i < curedfood.list.length; i++) {
if (req.body[key] != curedfood.list[i].price_min &&
curedfood.list[i].id === key.substring(0, key.length - 6)) {
curedfood.list[i].price = req.body[key];
// console.log('update: ' + curedfood.list[i].price);
}
}
} else {
if (req.body[key] > 0) {
var price = -1;
for (var i = 0; i < fastfood.list.length; i++) {
if (fastfood.list[i].id === key) {
price = fastfood.list[i].price;
break;
}
}
if (price < 0) {
for (var i = 0; i < curedfood.list.length; i++) {
if (curedfood.list[i].id === key) {
price = curedfood.list[i].price;
if (!price) price = curedfood.list[i].price_min;
break;
}
}
}
orders.push({
id: key,
count: parseInt(req.body[key]),
price: parseInt(price)
});
}
}
}
console.log(orders);
console.log(email + ' ' + extra);
if (orders.length === 0) {
// res.write('点餐失败!:(\n你确定你有选菜单吗?别逗我, - -||')
// res.end();
res.redirect('/?message=3&type= alert-danger')
return;
}
write(email, orders, extra, function () {
// res.write('点餐成功!');
res.redirect('/?message=4&type= alert-success')
res.end();
}, function () {
// res.write('点餐失败!服务器错误。。。');
res.redirect('/?message=5&type= alert-danger')
res.end();
});
})
app.get('/summary', function (req, res) {
res.setHeader('Content-Type', 'text/json; charset=utf-8');
read(function (arr) {
res.write(JSON.stringify(arr));
res.end();
});
})
app.listen(5000, function () {
console.log('app listening on port 5000...')
})
function write(email, orders, extra, ok, fail) {
const jsonFile = './data/' + today() + '.json';
// jsonfile.read
read(function (arr) {
let isContains = false;
for (var i = 0; i < arr.length; i++) {
if (arr[i].email === email) {
arr[i].email = email;
arr[i].menu = orders;
arr[i].extra = extra;
isContains = true; // update it
break;
}
}
if (!isContains) arr.push({
email: email,
menu: orders,
extra: extra
}); // insert it
jsonfile.writeFile(jsonFile, arr, function (err) {
if (!err) ok();
else fail(err)
});
});
}
function read(cb) {
const jsonFile = './data/' + today() + '.json';
// fs.closeSync(fs.openSync(jsonFile, 'r'));
jsonfile.readFile(jsonFile, function (err, obj) {
if (err) cb([]);
else {
obj = obj || [];
cb(obj);
}
});
}
function today() {
var dateObj = new Date();
var month = dateObj.getMonth() + 1; //months from 1-12
var day = dateObj.getDate();
var year = dateObj.getFullYear();
return year + "_" + month + "_" + day;
}