-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.local.js
174 lines (158 loc) · 4.84 KB
/
server.local.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
const express = require('express')
const bodyParser = require('body-parser')
const md5 = require('md5')
const menu = require('./data/menu.js')
const locale = require('./data/locale.js')
const plan = require('./data/plan.js')
const userInfo = require('./data/userInfo.js')
const ticket = require('./data/ticket.js')
const app = express()
// req.body包含在请求正文中提交的数据键值对。默认情况下,它是 undefined,
// 并在您使用 body-parser 和 multer 等正文解析中间件时填充
// for parsing application/json
app.use(bodyParser.json())
// for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: true
}))
// 公共返回
class Result {
static success(data) {
return {
success: true,
code: '200',
data,
message: '操作成功',
timestamp: Date.now()
}
}
static error(message) {
return {
success: false,
code: '200',
message,
timestamp: Date.now()
}
}
}
// 模拟后台验证码缓存,这里用数组不用map,便于模拟队列,移除时候先进先出
var captchaCache = []
app.get('/getMenu', (req, res) => {
res.send(Result.success(menu))
})
app.get('/getLocale', (req, res) => {
res.send(Result.success(locale))
})
app.get('/getPlan', (req, res) => {
res.send(Result.success(plan))
})
app.post('/login', (req, res) => {
let captchaObj;
captchaCache.forEach(item => {
if (item.key == req.body.captchaKey) {
captchaObj = item;
return;
}
});
// 验证码校验,验证码由后端生成,防止非法请求跳过校验,
// 注意:这里做的是简单版,实际上后端应该返回的是图片而不是文字,如果是文字轻易就被识别了
if (!captchaObj) {
res.send(Result.error('请刷新验证码'));
} else if (captchaObj.value.toUpperCase() !== req.body.captcha.toUpperCase()) {
res.send(Result.error('验证码错误'));
} else {
console.log(req.body.email, '上线了')
res.send(Result.success(createToken()))
}
})
app.get('/getUserInfo', (req, res) => {
res.send(Result.success(userInfo))
})
app.get('/getCaptcha/:captchaKey', (req, res) => {
// 获取query参数
// console.log(req.query);
// 获取params
// console.log(req.params);
const captch = createCaptcha();
const captchaKey = req.params.captchaKey;
captchaCache.push({
key: captchaKey,
value: captch
})
res.send(Result.success(captch));
})
app.post('/getTicket', (req, res) => {
const current = req.body.current;
const size = req.body.size;
// 简单模拟分页
const totalRecords = ticket.filter(item => {
if(req.body.subject && req.body.subject !== '' && item.subject.indexOf(req.body.subject) === -1){
return false;
}
if(req.body.workOrderLevel && req.body.workOrderLevel !== '' && item.workOrderLevel !== req.body.workOrderLevel){
return false;
}
if(req.body.workOrderStatus && req.body.workOrderStatus !== '' && item.workOrderStatus !== req.body.workOrderStatus){
return false;
}
return true;
})
let response = {
current: current || 1,
size: size || 10,
total: totalRecords.length,
pages: Math.floor(totalRecords.length / size) || 0,
// 简单分页处理
records: totalRecords.slice((current - 1) * size, current * size)
}
res.send(Result.success(response));
})
/**
* 随机token
*/
function createToken(leng) {
leng = leng == undefined ? 32 : leng
const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz23456789'
let token = ''
for (let i = 0; i < leng; ++i) {
token += chars.charAt(Math.floor(Math.random() * chars.length))
}
return md5(token)
}
/**
* 随机验证码
*/
function createCaptcha(leng) {
leng = leng == undefined ? 4 : leng
const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz23456789'
let captcha = '';
for (let i = 0; i < leng; i++) {
captcha += chars.charAt(Math.floor(Math.random() * chars.length));
}
return captcha;
}
/**
* 模拟移除过期验证码
*/
function allkeysTTL() {
// 一分钟
const interval = 60 * 1000;
setInterval(() => {
while (captchaCache.length > 0) {
let before = Date.now() - interval
if (captchaCache[0].key < before) {
captchaCache.shift();
continue;
}
break;
}
// -10000表示缓存处理时间
}, interval - 10000);
}
const server = app.listen(9000, err => {
if (!err) {
console.log('App running at:')
console.log(` - Local: http://localhost:${server.address().port}/`)
allkeysTTL();
}
})